Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

Recommending a "Deep Assignment" Instruction for C#

Rate me:
Please Sign up or sign in to vote.
4.65/5 (6 votes)
22 Feb 2008CPOL2 min read 27.5K   7   7
This article recommends a "deep assignment" instruction to add to the C# language.

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:

  1. Having a lot of variables increases the chance of typing errors.
  2. 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:

C#
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();

        // lots of assignment statements
        // --> the chance of typing errors is high
        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();

    // assigning identical variables of different objects
    // --> the chance of typing errors is high
    // --> the chance to forget to add the assignment of new variables 
    //     when expanding an object is high
    b.i11 = a.i11;
    b.i12 = a.i12;
    b.i21 = a.i21;
    b.i23 = a.i23;
    b.strA = a.strA;
    b.strC = a.strC;
    // do something...
}

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.

C#
// auto-implement property
//
// this C# 3.0 code...
public string Name { get; set; }

// ...is the same as this...
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:

C#
//obj1 :=: obj2;

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:

C#
public ClassA Clone()
{
    ClassA newClass = new ClassA();
    //newClass :=: this;
    return newClass;
}

Procedure TestProcA() of the above example would be simplified to:

C#
public void TestProcA( ClassA a )
{
    StructB b = new StructB();
    //b :=: a;
    // ...do something...
}

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...

C#
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:

C#
public void TestProc2( PersonLong personLong )
{
    PersonShort personShort = new PersonShort();

    //personShort :=: personLong;
}

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHey Dani - have you seen this? Pin
Adam Langley2-Nov-11 16:12
Adam Langley2-Nov-11 16:12 
GeneralGreat Idea Pin
merlin98127-Feb-08 4:38
professionalmerlin98127-Feb-08 4:38 
AnswerStrongly Disagree! Pin
Chamadness26-Feb-08 9:57
Chamadness26-Feb-08 9:57 
Generaldangerously implicit Pin
koos4ever25-Feb-08 22:05
koos4ever25-Feb-08 22:05 
GeneralIt hides intent Pin
MaxGuernsey25-Feb-08 12:30
MaxGuernsey25-Feb-08 12:30 
QuestionIs this really necessary? Pin
Rob van der Veer25-Feb-08 12:00
Rob van der Veer25-Feb-08 12:00 
GeneralYou don't need this, and often don't want it Pin
William E. Kempf22-Feb-08 8:57
William E. Kempf22-Feb-08 8:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.