Feature from Java... is in C#? [modified]
-
Hi everybody, I was looking a java code, and I would like to translate to C#, but I don't know if this feature exits in c#. The code is this:
public class Controller implements IController{
private State state; private Speed speed; private ProductHandler producthandler = new ProductHandler (){ @Override public void changeState(State newState) { state = newState; } @Override public Speed getSpeed() { return mds; } @Override public void send(Packet pk) { outputQueue.add(pk); } @Override public void setSpeed(Speed newSpeed) { speed = newSpeed; } };
ProductHandler is a class defined inside other class "dinamically". ProductHandler can access Controller attributes. Is this possible in C#? I have done it with events and so... but its very dirty. Is this annonymous classes, am i right? Is there something equivalent in .net 2.0? Regards
modified on Friday, January 8, 2010 2:50 PM
-
Hi everybody, I was looking a java code, and I would like to translate to C#, but I don't know if this feature exits in c#. The code is this:
public class Controller implements IController{
private State state; private Speed speed; private ProductHandler producthandler = new ProductHandler (){ @Override public void changeState(State newState) { state = newState; } @Override public Speed getSpeed() { return mds; } @Override public void send(Packet pk) { outputQueue.add(pk); } @Override public void setSpeed(Speed newSpeed) { speed = newSpeed; } };
ProductHandler is a class defined inside other class "dinamically". ProductHandler can access Controller attributes. Is this possible in C#? I have done it with events and so... but its very dirty. Is this annonymous classes, am i right? Is there something equivalent in .net 2.0? Regards
modified on Friday, January 8, 2010 2:50 PM
-
Hi everybody, I was looking a java code, and I would like to translate to C#, but I don't know if this feature exits in c#. The code is this:
public class Controller implements IController{
private State state; private Speed speed; private ProductHandler producthandler = new ProductHandler (){ @Override public void changeState(State newState) { state = newState; } @Override public Speed getSpeed() { return mds; } @Override public void send(Packet pk) { outputQueue.add(pk); } @Override public void setSpeed(Speed newSpeed) { speed = newSpeed; } };
ProductHandler is a class defined inside other class "dinamically". ProductHandler can access Controller attributes. Is this possible in C#? I have done it with events and so... but its very dirty. Is this annonymous classes, am i right? Is there something equivalent in .net 2.0? Regards
modified on Friday, January 8, 2010 2:50 PM
I don't know Java, so I'm guessing a bit! If you want to declare a new class nested inside another, you can and the nested class can access all members of the outer class. If you want to extend an existing class, you can derive from it. Another option is to use extension methods, although they can only access the public members of the original class. Nick
---------------------------------- Be excellent to each other :)
-
I don't know Java, so I'm guessing a bit! If you want to declare a new class nested inside another, you can and the nested class can access all members of the outer class. If you want to extend an existing class, you can derive from it. Another option is to use extension methods, although they can only access the public members of the original class. Nick
---------------------------------- Be excellent to each other :)
-
Hi everybody, I was looking a java code, and I would like to translate to C#, but I don't know if this feature exits in c#. The code is this:
public class Controller implements IController{
private State state; private Speed speed; private ProductHandler producthandler = new ProductHandler (){ @Override public void changeState(State newState) { state = newState; } @Override public Speed getSpeed() { return mds; } @Override public void send(Packet pk) { outputQueue.add(pk); } @Override public void setSpeed(Speed newSpeed) { speed = newSpeed; } };
ProductHandler is a class defined inside other class "dinamically". ProductHandler can access Controller attributes. Is this possible in C#? I have done it with events and so... but its very dirty. Is this annonymous classes, am i right? Is there something equivalent in .net 2.0? Regards
modified on Friday, January 8, 2010 2:50 PM
-
Hi everybody, I was looking a java code, and I would like to translate to C#, but I don't know if this feature exits in c#. The code is this:
public class Controller implements IController{
private State state; private Speed speed; private ProductHandler producthandler = new ProductHandler (){ @Override public void changeState(State newState) { state = newState; } @Override public Speed getSpeed() { return mds; } @Override public void send(Packet pk) { outputQueue.add(pk); } @Override public void setSpeed(Speed newSpeed) { speed = newSpeed; } };
ProductHandler is a class defined inside other class "dinamically". ProductHandler can access Controller attributes. Is this possible in C#? I have done it with events and so... but its very dirty. Is this annonymous classes, am i right? Is there something equivalent in .net 2.0? Regards
modified on Friday, January 8, 2010 2:50 PM
ika2 wrote:
Is there something equivalent in .net 2.0?
C# don't support anonymous classes. It has support for anonymous types, but it will not help you in this case. I guess what you need can be achieved using nested classes. Nested class will have access to all members of the class where it is written. Here is an example.
public class Controller : IController
{
private State state;
private Speed speed;public class ProductHandler { public void ChangeState(State newState) { } }
}
Best wishes, Navaneeth