Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign a list of values to a struct in C#?

I have a struct (.NET 3.5):

struct ColumnHeadings 
        { 
            public string Name ; 
            public int Width ; 
        } ;

And when I try to assign a list of values to that struct I get a 'cannot implicitly convert type string/int to ...':

private void doSomething()
{
    ColumnHeadings[,] ch = new ColumnHeadings[,]{{"column1",100},
                {"column2",100},{"column3",100}};
}

Can the struct values be assigned in the same way as a multi-dimensional array? Or do I need to assign the values by using?:

ch.Name = "column 1";

UPDATE:

Thanks to Marc's excellent feedback the correct solution is:

Struct:

struct ColumnHeadings
        {
            private readonly string name;
            private readonly int width;
            public string Name { get { return name; } }
            public int Width { get { return width; } }
            public ColumnHeadings(string name, int width)
            {
                this.name = name;
                this.width = width;
            }
        } 

Then in the method:

 var ch = new[]{new ColumnHeadings("column1",100),
            new ColumnHeadings("column2",100),
            new ColumnHeadings("column3",100)};

And the link to why mutuable structs aren't a good idea.

like image 272
John M Avatar asked Dec 01 '10 13:12

John M


People also ask

Can you assign values in a struct?

Accessing data fields in structs Although you can only initialize in the aggregrate, you can later assign values to any data fields using the dot (.) notation. To access any data field, place the name of the struct variable, then a dot (.), and then the name of the data field.

Can a struct contain a list?

Yes you can have a list in struct but you cannot initialise it with a field initialiser and instead you must use the constructor. Also note that you can not have a parameter-less constructor.

Can we assign value in structure in C?

You can also assign values to members of a structure variable at declaration time, in a single line.


1 Answers

firstly, that probably shouldn't be a struct at all

The syntax will be:

ColumnHeadings[] ch = new ColumnHeadings[]{
    new ColumnHeadings{Name="column1",Width=100},
    new ColumnHeadings{Name="column2",Width=100}
};

However, in addition you have the issue of public fields, and the fact that this is a mutable struct - both of which are dangerous. No, really.

I would add a constructor:

var ch = new []{
     new ColumnHeadings("column1", 100),
     new ColumnHeadings("column2", 100)
};

with:

struct ColumnHeadings
{
    private readonly string name;
    private readonly int width;
    public string Name { get { return name; } }
    public int Width { get { return width; } }
    public ColumnHeadings(string name, int width)
    {
        this.name = name;
        this.width = width;
    }
}
like image 123
Marc Gravell Avatar answered Sep 17 '22 01:09

Marc Gravell