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
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With