fhnaseer
fhnaseer

Reputation: 7267

Assign base object of child object

I have a base class and a child class. Base class contains some variables and child contains some variables.

What I need to do is that when I create object of child class, I pass base class object in its constructor which will set base class variables of child object.

Code:

public class BaseClass
{
    public int BaseInteger;
    public double BaseDouble;
    public bool BaseBool;
}

public class ChildClass : BaseClass
{
    public ChildClass(BaseClass baseClass)
    {
        this.base = baseClass;     // I am trying to do this.
    }

    public int ChildInteger;
    public string ChildString;
}

So is it possible what I am trying to do here. How? When I tried this code I got this error.

Use of keyword 'base' is not valid in this context

Upvotes: 4

Views: 951

Answers (3)

haim770
haim770

Reputation: 49095

You have to realize that ChildClass does not contain BaseClass but rather inherits from it.

That is why you can access data and methods that were defined in the base class using the this keyword (this.BaseInteger for example).

And that is also why you cannot 'set' the BaseClass of your ChildClass, it doesn't contain one.

Nevertheless, there are some useful patterns to achieve what you're trying to do, for example:

public class BaseClass
{
    protected BaseClass() {}

    protected BaseClass(BaseClass initData)
    {
        this.BaseInteger = initData.BaseInteger;
        this.BaseDouble = initData.BaseDouble;
        this.BaseBool = initData.BaseBool;
    }

    public int BaseInteger;
    public double BaseDouble;
    public bool BaseBool;
}

public class ChildClass : BaseClass
{
    public ChildClass() {}

    public ChildClass(BaseClass baseClass) : base(baseClass)
    {
    }

    public int ChildInteger;
    public string ChildString;
}

Thanks to @svick for his suggestions.

Upvotes: 5

Erik Schierboom
Erik Schierboom

Reputation: 16636

I think the most elegant way is to create a second constructor in the BaseClass class that takes a BaseClass instance from which it should initialize its fields. Then you can simply call this base constructor in your ChildClass class:

public class BaseClass
{
    public int BaseInteger;
    public double BaseDouble;
    public bool BaseBool;

    public BaseClass()
    {
    }

    public BaseClass(BaseClass baseClass)
    {
        this.BaseInteger = baseClass.BaseInteger;
        this.BaseDouble = baseClass.BaseDouble;
        this.BaseBool = baseClass.BaseBool;
    }
}

public class ChildClass : BaseClass
{
    public int ChildInteger;
    public string ChildString;

    public ChildClass(BaseClass baseClass) : base(baseClass)
    {
    }

}

Upvotes: 1

Arie
Arie

Reputation: 5373

public class BaseClass
{
    public int BaseInteger;
    public double BaseDouble;
    public bool BaseBool;
}

public class ChildClass : BaseClass
{
    public ChildClass(BaseClass baseClass)
    {
        this.BaseInteger = baseClass.BaseInteger;
        this.BaseDouble = baseClass.BaseDouble;
        this.BaseBool = baseClass.BaseBool;
    }

    public int ChildInteger;
    public string ChildString;
}

Upvotes: 1

Related Questions