Initializing an Array of struct 
Author Message
 Initializing an Array of struct

I have a struct:

private struct Elements
{
    char Code;
    string Value;

Quote:
};

I want to initialize a static array of values for use in a lookup function:

private static Elements[] MyList = { {'a',"advance"},{"b","back"} };

I get the error:  Array initializers can only be used in variable or field
initializer.

How do I do this ?



Fri, 29 Oct 2004 08:56:49 GMT  
 Initializing an Array of struct
Add a constrcutor to the structure then you can do this

private struct Elements
{
    char Code;
    string Value;

    public Elements(char Code, string Value)
    {
        this.Code = Code;
        this.Value = Value;
    }

Quote:
};

private static Elements[] MyList = { new Elements("a","advance"),
                                                       new
Elements("b","back") };

nb In your example note you had single quotes around the a.

HTH
Colin B


Quote:
> I have a struct:

> private struct Elements
> {
>     char Code;
>     string Value;
> };

> I want to initialize a static array of values for use in a lookup
function:

> I get the error:  Array initializers can only be used in variable or field
> initializer.

> How do I do this ?



Fri, 29 Oct 2004 14:16:38 GMT  
 Initializing an Array of struct

Quote:
> I have a struct:

> private struct Elements
> {
>     char Code;
>     string Value;
> };

> I want to initialize a static array of values for use in a lookup
function:

> I get the error:  Array initializers can only be used in variable or field
> initializer.

> How do I do this ?

Colin is exactly correct for this!!

You need a constructor for the struct and you need to *new* them before
adding them to a list.

--
Peter Wu
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure?  For information about the Strategic Technology Protection
Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.



Fri, 29 Oct 2004 15:35:06 GMT  
 Initializing an Array of struct
Thanks.

Quote:
> Add a constrcutor to the structure then you can do this

> private struct Elements
> {
>     char Code;
>     string Value;

>     public Elements(char Code, string Value)
>     {
>         this.Code = Code;
>         this.Value = Value;
>     }
> };

> private static Elements[] MyList = { new Elements("a","advance"),
>                                                        new
> Elements("b","back") };

> nb In your example note you had single quotes around the a.

> HTH
> Colin B



> > I have a struct:

> > private struct Elements
> > {
> >     char Code;
> >     string Value;
> > };

> > I want to initialize a static array of values for use in a lookup
> function:

> > I get the error:  Array initializers can only be used in variable or
field
> > initializer.

> > How do I do this ?



Sat, 30 Oct 2004 02:20:18 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Initializing an array of structs

2. Initialize an array of structs

3. Initialize array from members of a struct: Legal ?

4. Initializing. a huge array of structs

5. Initializing. a huge array of structs

6. initialize arrays in struct...illegal?

7. Initialize struct with struct

8. Problem with struct/array inside a struct using fread/fwrite

9. struct.array works - struct->pointer not

10. Initialisation of structs containing arrays of structs...?

11. native dll calling with structs containing arrays of nested structs

12. Initializing const auto structs

 

 
Powered by phpBB® Forum Software