Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Feature from Java... is in C#? [modified]

Feature from Java... is in C#? [modified]

Scheduled Pinned Locked Moved C#
csharpjavaperformancequestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ika2
    wrote on last edited by
    #1

    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

    L N P N 4 Replies Last reply
    0
    • I ika2

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      C# has anonymous methods, but not anonymous classes. Maybe because you can use delegates, so there is no need for a whole class.. (according to the designers) Anonymous methods can access the variables of the place they were declared in as well

      1 Reply Last reply
      0
      • I ika2

        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

        N Offline
        N Offline
        Nicholas Butler
        wrote on last edited by
        #3

        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 1 Reply Last reply
        0
        • N Nicholas Butler

          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 Offline
          I Offline
          ika2
          wrote on last edited by
          #4

          Thanks to both. Nick, I have tried to do as you say, but I could't. Could you please paste a little example here? Thanks in advance

          1 Reply Last reply
          0
          • I ika2

            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

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            You could simulate this using dynamic methods. Have a look around this[^] topic.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • I ika2

              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

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups