C# function overloading question

Started by
10 comments, last by benryves 17 years, 1 month ago
javasirc: you changed the return type and left of the return (this);, it needs to return itself since the next SetN("...") is called on the value returned by the previous SetN("...")

I would advise you to use properties instead though like Telastyn suggested.
Advertisement
Another option is to rather than do this:

class Demo {    int a, b, c;    public Demo(int a, int b, int c) {        this.a = a;        this.b = b;        this.c = c;    }    public Demo(int a, int b) {        this.a = a;        this.b = b;        this.c = 3;    }    public Demo(int a) {        this.a = a;        this.b = 2;        this.c = 3;    }    public Demo() {        this.a = 1;        this.b = 2;        this.c = 3;    }}


...you could do this:

class Demo {    int a, b, c;    public Demo(int a, int b, int c) {        this.a = a;        this.b = b;        this.c = c;    }    public Demo(int a, int b) : this(a, b, 3) { }    public Demo(int a) : this(a, 2) { }    public Demo() : this(1) { }}


You still need to write multiple constructors, but they get easier and easier to write. [smile]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement