Simple Array Question?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    #1

    Simple Array Question?

    Hello,

    Thanks for reviewing my question. I am new to C# and
    would like to know why I am receiving an error with the
    following code:

    struct CARS
    {
    string sText;
    int x;
    };

    CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };

    Error:
    Array initializers can only be used in a variable or
    field initializer. Try using a new expression instead.

    It seems that it doesn't like the enclosing curely braces.

    Many Thanks
    Peter
  • Jan Tielens

    #2
    Re: Simple Array Question?

    Try the following:
    CARS c1 = new CARS();
    CARS c2 = new CARS();
    c1.sText = "Honda";
    c2.sText = "Toyota";
    CARS[] array = new CARS[] {c1,c2};

    If you use a Class instead of a struct, you can add constructors with
    parameters so you could do something like this:
    CARS[] a = new CARS[] {new CARS("Toyota"), new CARS("Honda")};


    --
    Greetz,
    Jan
    _______________ _______________ ____
    Read my weblog: http://weblogs.asp.net/jan


    "Peter" <pnkonstant@hot mail.com> schreef in bericht
    news:08da01c3ba 98$fff382d0$a00 1280a@phx.gbl.. .[color=blue]
    > Hello,
    >
    > Thanks for reviewing my question. I am new to C# and
    > would like to know why I am receiving an error with the
    > following code:
    >
    > struct CARS
    > {
    > string sText;
    > int x;
    > };
    >
    > CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };
    >
    > Error:
    > Array initializers can only be used in a variable or
    > field initializer. Try using a new expression instead.
    >
    > It seems that it doesn't like the enclosing curely braces.
    >
    > Many Thanks
    > Peter[/color]


    Comment

    • Frank Oquendo

      #3
      Re: Simple Array Question?

      Peter wrote:
      [color=blue]
      > CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };[/color]

      CARS[] array = {new CARS("Honda", 0), new CARS("Toyota", 0)};

      --
      There are 10 kinds of people. Those who understand binary and those who
      don't.


      (Pull the pin to reply)


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Simple Array Question?

        Peter <pnkonstant@hot mail.com> wrote:[color=blue]
        > Thanks for reviewing my question. I am new to C# and
        > would like to know why I am receiving an error with the
        > following code:
        >
        > struct CARS
        > {
        > string sText;
        > int x;
        > };
        >
        > CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };
        >
        > Error:
        > Array initializers can only be used in a variable or
        > field initializer. Try using a new expression instead.
        >
        > It seems that it doesn't like the enclosing curely braces.[/color]

        No - you can't initialise it like that. You need to provide a
        constructor to your struct which initialises the values within it
        appropriately, and then use something like:

        CARS[] array = new CARS[]
        {
        new CARS ("Honda", 0),
        new CARS ("Toyota", 0)
        };

        The constructor might look something like:

        public CARS (string sText, int x)
        {
        this.sText = sText;
        this.x = x;
        }

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Simple Array Question?

          Jan Tielens <jan@no.spam.pl ease.leadit.be> wrote:[color=blue]
          > Try the following:
          > CARS c1 = new CARS();
          > CARS c2 = new CARS();
          > c1.sText = "Honda";
          > c2.sText = "Toyota";
          > CARS[] array = new CARS[] {c1,c2};[/color]

          That wouldn't work with the struct as shown, as the members are private
          (as they should normally be).
          [color=blue]
          > If you use a Class instead of a struct, you can add constructors with
          > parameters so you could do something like this:
          > CARS[] a = new CARS[] {new CARS("Toyota"), new CARS("Honda")};[/color]

          You can add constructors with parameters to structs as well as classes.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jan Tielens

            #6
            Re: Simple Array Question?

            Oh, ic, my mistake (blush)!

            Thx for correctiong me Jon!

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> schreef in bericht
            news:MPG.1a3997 2e7af619ce989ba 9@msnews.micros oft.com...[color=blue]
            > Jan Tielens <jan@no.spam.pl ease.leadit.be> wrote:[color=green]
            > > Try the following:
            > > CARS c1 = new CARS();
            > > CARS c2 = new CARS();
            > > c1.sText = "Honda";
            > > c2.sText = "Toyota";
            > > CARS[] array = new CARS[] {c1,c2};[/color]
            >
            > That wouldn't work with the struct as shown, as the members are private
            > (as they should normally be).
            >[color=green]
            > > If you use a Class instead of a struct, you can add constructors with
            > > parameters so you could do something like this:
            > > CARS[] a = new CARS[] {new CARS("Toyota"), new CARS("Honda")};[/color]
            >
            > You can add constructors with parameters to structs as well as classes.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            Working...