Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize base class in .NET

Tags:

c#

.net

oop

How do I go about if I need to initialize an object's base with existing object? For example, in this scenario:

public class A
{
    public string field1;
    public string field2;
}

public class B : A
{
    public string field3;
    public void Assign(A source)
    {
        this.base = source; // <-- will not work, what can I do here?
    }
}

Assign() method can, obviously assign values to the base class field-by-field, but isn't there a better solution? Since class B inherits from A, there must be a way to just assign A to the B.base

In C++ this would be a trivial thing to do, but I can't seem to grasp how to do this in .NET

like image 230
galets Avatar asked Jan 29 '09 18:01

galets


People also ask

What is initialize in C#?

Initialization sets the variable to a new instance. It must be to a type that is compatible with the declaration type. 1static void Main(string[] args) 2{ 3 string a = "Hello World"; 4 Console. WriteLine(a); 5} cs.

How do you initialize a class?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

How do you initialize a class object in C#?

In C#, a class object is created using the new operator. During creation, the internal data (variables) of a class can be initialized in one of the following ways: by assigning a class variable the desired value when it is declared (immediate initialization). This method is used only for variables of base types.

What does base () do in C#?

base (C# Reference) The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.


2 Answers

Unfortunately base is readonly.

[Edit]
Well perhaps not so unfortunate. The relationship between a base class and a child class is IS-A not HAS-A. By allowing a child class to change the instance of the base class you are allowing the child class to change its own reference since it IS-A base class. If you truly need this functionality then I would suggest you change your inheritance model to reflect what you truly want to do.

Something like this:

public class A
{
    public string field1;
    public string field2;
}

public class B
{
    public string field3;
    public A a;

    public void Assign(A source)
    {
        this.a = source;
    }
}

seems more appropriate and has clearer meaning and functionality.

like image 99
Andrew Hare Avatar answered Sep 19 '22 13:09

Andrew Hare


        public Assign(A a)
        {
            foreach (var prop in typeof(A).GetProperties())
            {
                this.GetType().GetProperty(prop.Name).SetValue(this, prop.GetValue(a, null),null);
            }
        }

Basically, it uses reflection to get all the properties of the base and assign the values of this, to all the values that exist in A.

EDIT: To all you naysayers out there, I quickly tested this now with a base class that had 100 integer variables. I then had this assign method in a subclass. It took 46 milliseconds to run. I don't know about you, but I'm totally fine with that.

like image 42
BFree Avatar answered Sep 21 '22 13:09

BFree