Re: initializer for array of struct

Tech-Archive recommends: Fix windows errors by optimizing your registry



ChrisA <ChrisA@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> I have an array of 'struct' with three string fields which I would like to
> initialize.
> Is this possible?
>
> public struct Role
> {
> string a; string b; string c;
> }
> ...
> Role[] roles = ?? ( {"1","2","3"}, {"4","5","6"} };

Not quite, no. You'd need:

public struct Role
{
string a, b, c;

public Role (string a, string b, string c)
{
this.a = a;
this.b = b;
this.c = c;
}
}

then

Role[] roles = new Role[] { new Role ("1", "2", "3"),
new Role ("4", "5", "6") };

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • initializer for array of struct
    ... I have an array of 'struct' with three string fields which I would like to ... initialize. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: pointers in Strucrure assignment problem
    ... This does NOT initialize your buffer with the given string. ... Prev by Date: ...
    (comp.os.msdos.djgpp)
  • Re: Record type flushing
    ... If you try to assign a new value to a string field in that ... reference, so the compiler's code tries to release that reference before ... The Initialize procedure can be used to solve that problem. ... If you're using the shell's memory manager, ...
    (alt.comp.lang.borland-delphi)
  • Re: Lets make it backwards compatible :D
    ... > Incorrect use of Initialize. ... it is a string. ... The pointer storage would point to the null terminator. ... Storage now holds a string reference. ...
    (alt.comp.lang.borland-delphi)
  • Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
    ... string to string variables" lazy because it prevents the developer from ... It's to always initialize it to something when I declare it. ... Anyhow, please describe "potential problems?" ...
    (microsoft.public.dotnet.languages.vb)