udaya726
udaya726

Reputation: 1020

Assign values for base class properties

I have a base class and derived class.

public class BaseClass
{

 public string property1{ get; set; }

 public string property2 { get; set; }

 public string property3{ get; set; } 

}

I have derived class follow.

public class DerivedClass:BaseClass
{

 public string property5 { get; set; }

}

After I create derived class object by

DerivedClass d1=new  DerivedClass();

I can access the properties of the base class by d1.property1. I want to assign values for all the base class properties once without setting each by each property. Is there any way to perform that.

If derived class has a property of base class as

public BaseClss baseClass { get; set; }

we can just assign a base to class that property. Its not the way that I need. I need flatter object

Upvotes: 0

Views: 7890

Answers (3)

Ajay Unnikrishnan
Ajay Unnikrishnan

Reputation: 129

Its not clear . I think you need to set the property only at base class.

 public class BaseClass
{
    private string  _property = "value";
    public string property1
    { 
        get
        {
            return _property;
        }
        protected set
        {
            _property = value;
        }
    }

    public string property2 { get; set; }

    public string property3 { get; set; }

}

Here we cannot set value to property1 from child or any other . It can be accessed from child class.

Upvotes: 1

B.K.
B.K.

Reputation: 10152

Your question is not 100% clear, so I'll provide you with some information that attempts to cover several basis.

If you want to set several properties during initialization, you may do it like so:

var d1 = new DerivedClass()
{
    prop1 = "value",
    prop2 = "value",
    prop3 = "value"
};

In a similar fashion, you may implement a non-default constructor:

public class BaseClass
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public string prop3 { get; set; } 

    public BaseClass(string p1, string p2, string p3)
    {
        prop1 = p1,
        prop2 = p2,
        prop3 = p3
    }
}

...and use it like so:

var d1 = new DerivedClass("val1", "val2", "val3");

...or like so (using named arguments):

var d1 = new DerivedClass(prop1: "val1", prop2: "val2", prop3: "val3");

You may also consider initializing properties within the default constructor:

public class BaseClass
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public string prop3 { get; set; } 

    public BaseClass()
    {
        prop1 = "value",
        prop2 = "value",
        prop3 = "value"
    }
}

I would even go as far as using optional arguments in those constructors, which covers the initial values, but it depends on your design:

public class BaseClass
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public string prop3 { get; set; } 

    public BaseClass(string p1 = "val1", string p2 = "val2", string p3 = "val3")
    {
        prop1 = p1,
        prop2 = p2,
        prop3 = p3
    }
}

You may use it like so:

// prop1 equals to "someVal1", prop2 equals to "someVal2" and prop3 equals to "someVal3"
var d1 = new DerivedClass("someVal1", "someVal2", "someVal3");

...or like so:

// prop1 equals to "val1", prop2 equals to "val2" and prop3 equals to "val3"
var d1 = new DerivedClass();

If you're talking about changing properties across the board (i.e. several instances share the same property instance), you would need to implement properties using the static keyword.

public class BaseClass
{
    public static string prop1 { get; set; } // this property gets shared among all instances
    public string prop2 { get; set; }
    public string prop3 { get; set; }     
}

Note that you would not be able to set a static property from an instance. You would set it and access it like so:

BaseClass.prop1 = "someValue";
Console.WriteLine(BaseClass.prop1);    // prints "someValue"
Console.WriteLine(DerivedClass.prop1); // prints "someValue"

DerivedClass.prop1 = "otherValue";
Console.WriteLine(DerivedClass.prop1); // prints "otherValue"
Console.WriteLine(BaseClass.prop1);    // prints "otherValue"

There are more ways, but I think it's enough of my brain dumping for now... at least until you clarify your intentions.

Upvotes: 2

kris123456
kris123456

Reputation: 501

Why not use the concept of constructor? Or a protected method, which can do the needful when invoked by any method of the derived class?

As far as i can see, your inheritance is not public? any specific reason for going for private inheritance? In case of private inheritance, i think you should go for public method. (Saying as per C++. A correction might be required if C# standards are different)

Upvotes: 1

Related Questions