Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there i have a question about moving code from JAVA to C#.
But i got problem about making function call and variable.

What I have tried:

in JAVA i gor this:
C#
public abstract class VisibleObjectController<T extends VisibleObject> {}

and in C# i did this:
C#
public abstract class VisibleObjectController<T> where T : VisibleObject {}


NOW i dont know about variable and function call this VisibleObjectController class in c#
this is JAVA:
C#
private final VisibleObjectController< ? extends VisibleObject > controller;

    public VisibleObjectController< ? extends VisibleObject > getController() {
        return controller;
    }




So how can i do the same in c#?
Posted
Updated 25-Nov-16 4:49am
v10
Comments
Richard Deeming 25-Nov-16 13:57pm    
AFAIK, Java's wilcard type parameters aren't supported in C#.

If it was just the method, you'd add a generic parameter to the method:
public VisibleObjectContainer<T> GetController<T>() where T : VisibleObject { ... }


But since you're using a field as well, you'd probably need a generic class to contain it:
public class MyContainer<T> where T : VisibleObject
{
    private VisibleObjectController<T> controller;
    
    public VisibleObjectController<T> GetController()
    {
        return controller;
    }
}


Perhaps if you could update your question with more information about how you're using the getController method, someone might be able to come up with another suggestion.
EADever 26-Nov-16 0:53am    
I want to use these function call and variable inside VisibleObject class it self.

1 solution

You need to create a generic method, same way you did in the class declaration using where keyword with T in the following syntax:

C#
private VisibleObjectController<t> controller;
 
    public VisibleObjectController<t> getController<t>() where t : VisibleObject {
        return controller;
    }
 
Share this answer
 
v5
Comments
EADever 25-Nov-16 11:45am    
`private VisibleObjectController<t> controller;` have error: t doesn't exist and function getController<t>() can't return to controller
Ehsan Sajjad 25-Nov-16 11:58am    
is the code in your generic abstract class defined above?
EADever 26-Nov-16 4:31am    
i mean these function call and variable using from another class, not in VisibleObjectController.
eg. need to use in public abstract class VisibleObject.
Ehsan Sajjad 26-Nov-16 9:43am    
That method can be in in a non generic class but the field cannot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900