Click here to Skip to main content
Click here to Skip to main content

Recommending a "Deep Assignment" Instruction for C#

, 22 Feb 2008
Rate this:
Please Sign up or sign in to vote.
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:

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.

// 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:

//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:

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

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

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

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

    //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)

About the Author

Daniel M. Camenzind

Switzerland Switzerland
No Biography provided

Comments and Discussions

 
QuestionHey Dani - have you seen this? PinmemberAdam Langley2-Nov-11 16:12 
GeneralGreat Idea Pinmembermerlin98127-Feb-08 4:38 
AnswerStrongly Disagree! PinmemberChamadness26-Feb-08 9:57 
Generaldangerously implicit Pinmemberkoos4ever25-Feb-08 22:05 
GeneralIt hides intent PinmemberMaxGuernsey25-Feb-08 12:30 
QuestionIs this really necessary? PinmemberRob van der Veer25-Feb-08 12:00 
GeneralYou don't need this, and often don't want it PinmemberWilliam E. Kempf22-Feb-08 8:57 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Feb 2008
Article Copyright 2008 by Daniel M. Camenzind
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid