Introduction
I'm missing an instruction in C# that allows to assign multiple variables (and/or properties) of an object to another. This article describes a solution that the 4GL language Clarion implements.
The Problem
When identical variables of similar objects are assigned or when cloning objects, the programmer has to assign every single variable from one object to the other object. I see two problems with this:
- Having a lot of variables increases the chance of typing errors.
- When a class or structure is expanded with a new variable, one might forget to add the assignment statement in the
Clone()
procedure or any other block of code that assigns the variables.
See the following example:
public class ClassA
{
private int i11, i12, i13;
private int i21, i22, i23;
private string strA, strB, strC;
public ClassA Clone()
{
ClassA newClass = new ClassA();
newClass.i11 = this.i11;
newClass.i12 = this.i12;
newClass.i13 = this.i13;
newClass.i21 = this.i21;
newClass.i22 = this.i22;
newClass.i23 = this.i23;
newClass.strA = this.strA;
newClass.strB = this.strB;
newClass.strC = this.strC;
return newClass;
}
}
public struct StructB
{
public int i11, i12;
public int i21, i23;
public string strA, strC;
public double dX;
}
public void TestProc1( ClassA a )
{
StructB b = new StructB();
b.i11 = a.i11;
b.i12 = a.i12;
b.i21 = a.i21;
b.i23 = a.i23;
b.strA = a.strA;
b.strC = a.strC;
}
Sure, variable names of "real" classes might be more accurate than in this example class, but the more variables a class has, the more typing errors are possible and the more annoying it is for the programmer to write all the assignment statements.
Introducing the Deep Assignment Instruction :=:
I'd appreciate an instruction that makes it very simple and safe to assign all variables of one object to another that have the same name and data type. The compiler would take care of everything. Just as the auto-implement property (introduced in C# 3.0) reduces writing a lot of (redundant) code, reduces typing errors and makes the code more compact.
public string Name { get; set; }
private string strName;
public string Name
{
get{ return strName; }
set{ strName = value; }
}
I recommend :=: as the deep assignment instruction.
Note: To be able to compile and/or format the code in CodeProject, the statements with the deep assignment instruction are commented out.
The deep assignment instruction would be used like this:
It assigns each variable of obj1
the value of the variable of obj2
that has exactly the same name and data type.
The Clone()
procedure of the above example would look like this and it would do exactly the same:
public ClassA Clone()
{
ClassA newClass = new ClassA();
return newClass;
}
Procedure TestProcA()
of the above example would be simplified to:
public void TestProcA( ClassA a )
{
StructB b = new StructB();
}
This would also have to work for nested classes (and structures) with the same name. For the nested class, only the name has to be the same, not the data type. The data type has to be the same for the variable that's assigned.
Assuming the following structures...
public struct AddressLong
{
public string strAddress;
public string strStreet;
public int iZip;
public string strCity;
}
public struct AddressShort
{
public string strAddress;
public int iZip;
public string strCity;
}
public struct PersonLong
{
public string strFirstName;
public string strLastName;
public AddressLong adr;
}
public struct PersonShort
{
public string strFirstName;
public string strLastName;
public AddressShort adr;
}
... the following two procedures would do the same:
public void TestProc2( PersonLong personLong )
{
PersonShort personShort = new PersonShort();
}
public void TestProc2( PersonLong personLong )
{
PersonShort personShort = new PersonShort();
personShort.strFirstName = personLong.strFirstName;
personShort.strLastName = personLong.strLastName;
personShort.adr.strAddress = personLong.adr.strAddress;
personShort.adr.iZip = personLong.adr.iZip;
personShort.adr.strCity = personLong.adr.strCity;
}
Points of Interest
I recommend that using the deep assignment instruction inside a structure or class on the same structure or class (as shown above in the Clone()
procedure) assigns private
, protected
and public
variables, whereas using it outside of a structure or class (as shown above in the TestProcX()
procedures) only assigns public
variables.
This article should open a discussion of my recommendation. Any comment is welcome.
Does anybody know if and how it's possible to recommend new features like this to Microsoft?
History
- 16.02.2008: First post to open the discussion
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.