Results 1 to 3 of 3

Thread: Constructor chaining, I guess...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Constructor chaining, I guess...

    I remember in Java, being able to do something like this...
    Code:
    public class SomeClass
    {
        public SomeClass() { this(0); }
        public SomeClass(int x) { this(x,""); }
        public SomeClass(int x, string s) { /* ... */ }
    }
    And that would work its way down the chain of constructors so that you didn't need a lot of repeat initializing code. That may not be the completely correct syntax, but I hope you get the idea.

    Anyway, this doesn't seem to work in C#. Is there any way to do something like the above in C#?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Try this:
    Code:
    public class SomeClass
    {
        public SomeClass(): this(0)
        public SomeClass(int x): this(x, "")
        public SomeClass(int x, string s) { /* ... */ }
    }

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Thanks.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width