views:

102

answers:

3

I am in the process of converting some code from C# to Java. I have never used C# before, but it has been pretty easy up to this point.

I have a line that looks like this in the C# file:

 coverage.createMethod = delegate (Gridpoint gp){
 //Some method stuff in here, with a return objecct
 }

What exactly is this trying to do? It seems a little bit like an inline class but I am not sure how to go about converting htis to java

Edit: more on the specific problem

In a file called STKDriver.java i have the following

 CoverageDefinitionOnCentralBody coverage = new CoverageDefinitionOnCentralBody(...);
 .
 .
 .
 DClass value = new DClass(STKDriver.class, "invoke", CoverageGrid.class);
 coverage.setGridPointCreationMethod(value);

In the fill DClass.java, which extends CreateCoverageGridPointForAccess I have the following:

 public DClass(Class class1, String string, Class<CoverageGridPoint> class2{}
 .
 .
 .
 public IServiceProvider invoke(CoverageGridPoint gridPoint){
    return something; //of the classtype Platform
 }

Is this done correctly? The class definitions are linked here:

http://www.agi.com/resources/help/online/AGIComponentsJava/index.html?page=source%2FWhatsNewJava.html

Notice that the class CreateCoverageGridPointForAccess is abstract and extends that Delegate class.

Does this implementation I have created look correct? I Can write in more code if necessary

+3  A: 

This is an anonymous method in C#. It's technically the same thing as:

coverage.createMethod = new Func<Gridpoint, object>(SampleMethod);

public object SampleMethod(Gridpoint gp)
{
    return thingy; // Pseudo for return value
}

It's just a shortcut you can use to code less.

Tejs
So would the type have to be cast since the method returns an object?
Derek
coverage.createMethod would be a delegate. The 'coverage' object will execute that delegate somewhere later. So we're not assigning an object to createMethod, but a method to execute at a later point.
Tejs
I'd also suggest looking at this resource, a lot of people have recommended it: http://onjava.com/pub/a/onjava/2003/05/21/delegates.html
Tejs
I have seen that article before. I guess what is still confusing me is what is the equivalent in Java? Is the code posted in this post java or C#? I have never seen a line like coverage.createMethod = new Func<Gridpoint, object>(SampleMethod); but I am no java expert either
Derek
I've posted a C# translation of an anonymous method. A way to do this in Java would be to put in a simple class that contains this one method, and then when the C# class would have invoked the method, you just invoke the method on the class you assigned. Like Dependency Injection.
Tejs
Updated OP with more details
Derek
A: 

coverage.createMethod is a Delegate.

The following code creates an anonymous method and assigns it to the delegate:

coverage.createMethod = delegate (Gridpoint gb) {
}

so that when somebody calls coverage.createMethod, your anonymous method gets executed.

Justin Niessner
+3  A: 

Tejs' answer is correct. However be careful because anonymous functions can use closures which means using an existing local variable declared in the outer function, from the anonymous delegate. I'm no Java programmer so I don't know if Java supports this.

Julien Lebosquain
No, but it will in Java 7
BlueRaja - Danny Pflughoeft
Good point. Java doesn't support capturing arbitrary variables inside anonymous classes as far as I know, so this is definitely something to watch out for.
JulianR
Java supports capturing `final` variables or parameters.
Jordão
Updated OP with more details
Derek