Anicho
Anicho

Reputation: 2667

Constructor overloads and class parameters

Is there a better way to do what I am doing:

class Sample {

int SampleValueX, SampleValueY;
string SampleFacing;    

public Sample(int samplevaluex, string samplefacing)
{
    SampleValueX = startpositionx;
    SampleValueY = 0;
    SampleFacing = samplefacing;
}

public Sample(int samplevaluey, string samplefacing)
{
    SampleValueX = 0;
    SampleValueY = startpositionx;
    SampleFacing = samplefacing;
}
}

Is this even known as constructor overloading? Correct me if I am wrong.

Upvotes: 3

Views: 191

Answers (4)

Steve
Steve

Reputation: 216273

This could not work because you have two constructor with the same parameters and inside the constructor you can't differentiate between one form to the other.

With Visual Studio 2008 and subsequent versions you can initialize your objects with the object initializer syntax

    Sample sampleInstance = new Sample
    {
        SampleValueX = 0,
        SampleValueY = 10,
        SampleFacing = "MySample"
    };

This will remove the need to initialize your internal members.
Of course this requires that your internal properties becomes public and this is not always the case.

class Sample
{
    public int SampleValueX {get;set;};
    public int SampleValueY {get;set;};
    public string SampleFacing {get; set;};

    ......

Upvotes: 4

Johan Larsson
Johan Larsson

Reputation: 17580

I agree that optional parameters is probably what you want here. An alternative could be:

class Sample
{
    int SampleValueX,  SampleValueY;
    string SampleFacing;

    public Sample(XSetting xSetting)
    {
        SampleValueX = xSetting.X;
        SampleValueY = 0;
        SampleFacing = xSetting.SampleFacing;
    }

    public Sample(YSetting ySetting)
    {
        SampleValueX = 0;
        SampleValueY = ySetting.Y;
        SampleFacing = ySetting.SampleFacing;
    }
}

public class XSetting
{
    public int X { get; set; }
    public string SampleFacing { get; set; }
}

public class YSetting
{
    public int Y { get; set; }
    public string SampleFacing { get; set; }
}

As your code example looks this is probably overkill.

Your code example will not compile btw.

Upvotes: 2

Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

In my humble opinion this kind of logic does not adds value to the class. If the class has 3 parameters and you want to set them from the constructor I would add a constructor with the 3 parameters and set to 0 the required one on the constructor call.

That way would be clearer for the class user what the class parameter values are.

Upvotes: 2

levelnis
levelnis

Reputation: 7705

You can use optional parameters

class Sample {

  int SampleValueX, SampleValueY;
  string SampleFacing;    

  public Sample(string samplefacing, int samplevaluex = 0, int samplevaluey = 0)
  {
    SampleValueX = samplevaluex;
    SampleValueY = samplevaluey;
    SampleFacing = samplefacing;
  }

}

Note that the optional parameters must appear after the mandatory ones

Upvotes: 10

Related Questions