Simple Array Question?

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

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
 
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")};
 
Peter said:
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.

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;
}
 
Jan Tielens said:
Try the following:
CARS c1 = new CARS();
CARS c2 = new CARS();
c1.sText = "Honda";
c2.sText = "Toyota";
CARS[] array = new CARS[] {c1,c2};

That wouldn't work with the struct as shown, as the members are private
(as they should normally be).
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")};

You can add constructors with parameters to structs as well as classes.
 
Peter said:
CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };

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.

http://code.acadx.com
(Pull the pin to reply)
 
Oh, ic, my mistake (blush)!

Thx for correctiong me Jon!

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

That wouldn't work with the struct as shown, as the members are private
(as they should normally be).
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")};

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