Question

I have class, all of the fields in which have to be when an create object. My solution:

class MyClass
{
    private string Field1;
    private int Field2;

    public MyClass(string Field1, int Field2)
    {
        this.Field1 = Field1;
        this.Field2 = Field2;
    }
}

But the fields may be greater than 2, the code looks dirty. Is there a more elegant method?

Was it helpful?

Solution

Passing fields that are required to constructors should be fine. Your code doesn't look dirty yet, but if it started to grow say 10 required fields, then it is better to create an object that contains all the required fields and then use that object when creating the object (Constructor).

Right now this is not dirty

class MyClass
{
    private string Field1;
    private int Field2;

    public MyClass(string Field1, int Field2)
    {
        this.Field1 = Field1;
        this.Field2 = Field2;
    }
}

If it became like this

class MyClass
{
    private string Field1;
    private int Field2;

    public MyClass(string Field1, int Field2, int Field3, int Field4, int Field5, int Field6, int Field7, int Field8, int Field9, int Field10)
    {
        this.Field1 = Field1;
        this.Field2 = Field2;
        //Set them
    }
}

Then it is better to have this

    class RequiredFields
{
    //All required fields
}

class MyClass
{
    private string Field1;
    private int Field2;

    public MyClass(RequiredFields requiredFields)
    {
        this.Field1 = requiredFields.Field1;
        this.Field2 = requiredFields.Field2;
        //Set them
    }
}

OTHER TIPS

Can use System.ComponentModel.DataAnnotations.RequiredAttribute

[Required]
public string Field1{ get; set; }

You could use properties with setters rather than fields, and then have an IsValid() method to check that the object has been 'filled in' fully before it's used. However, if you are going to be setting all the values at the same time in code, that's not going to look any tidier.

With long constructor parameter lists, Named Arguments can help the call more readable.

There's really no other way to ensure the user provides a value for each and every field, but you could provide a default value yourself.

For example, you can provide an alternate constructor which constructs an object with certain default field values like this:

public MyClass() : this(string.Empty, -1)
{
}

public MyClass(string Field1, int Field2)
{
    this.Field1 = Field1;
    this.Field2 = Field2;
}

Or alternatively:

public MyClass(string Field1 = "", int Field2 = -1)
{
    this.Field1 = Field1;
    this.Field2 = Field2;
}

You could just have something like this and then reference the Object whenever you're trying to access the fields in it.

class MyClass
{
    private MyObject Test;

    public MyClass(MyObject Test)
    {
        this.Test = Test;
    }


}
class MyObject
{
    private string Field1;
    private int Field2;

    // Constructor / methods to set up fields
}

I think you need private contructor, factory method and named parameters, for example:

class MyClass
{
    private string Field1;
    private int Field2;

    private MyClass()
    {

    }
    public MyClass GetMyClassInstance(string Field1=string.Empty, int Field2=-1)
    {
        this.Field1 = Field1;
        this.Field2 = Field2;
    }
}

Now you can add any count of parameters to create object of MyClass.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top