initializing an array of structs

S

Steve Roberts

In C# is it possible to initialize the items of an array when the array is
an array of structures?



When I try to initialize the items of the array (as shown below) using the
method I would use in C++, I get the compiler error "Array initializers can
only be used in a variable or field initializer. Try using a new expression
instead.":



Is there a way to do this?

Thanks for any help,

Steve



public struct test

{

public int id;

public string name;

}



public test[] testArray = { {1,"name1"}, {2,"name2"} };
 
L

Larry Lard

Steve said:
In C# is it possible to initialize the items of an array when the array is
an array of structures?

When I try to initialize the items of the array (as shown below) using the
method I would use in C++, I get the compiler error "Array initializers can
only be used in a variable or field initializer. Try using a new expression
instead.":

Is there a way to do this?

Thanks for any help,

Steve

public struct test
{
public int id;
public string name;

First give test a constructor in here:

public test(int id, string name)
{
this.id = id;
this.name = name;
}

public test[] testArray = { {1,"name1"}, {2,"name2"} };

Then use this to make the tests in testArray:

public test[] testArray = { new test(1, "name1"), new test(2,
"name2") };
 
?

=?iso-8859-1?Q?Lasse=20V=e5gs=e6ther=20Karlsen?=

In C# is it possible to initialize the items of an array when the
array is an array of structures?

When I try to initialize the items of the array (as shown below) using
the method I would use in C++, I get the compiler error "Array
initializers can only be used in a variable or field initializer. Try
using a new expression instead.":
public test[] testArray = { {1,"name1"}, {2,"name2"} };

You'll need to provide a constructor in your struct and initialize using
that, like this:

public struct Test
{
public Int32 Id;
public String Name;

public Test(Int32 id, String name)
{
Id = id;
Name = name;
}
}

public Test[] testArray =
{
new Test(1, "name1"),
new Test(2, "name2")
};
 
T

Tom Porterfield

In C# is it possible to initialize the items of an array when the array is
an array of structures?

When I try to initialize the items of the array (as shown below) using the
method I would use in C++, I get the compiler error "Array initializers can
only be used in a variable or field initializer. Try using a new expression
instead.":

Is there a way to do this?

Thanks for any help,

Steve

public struct test
{
public int id;
public string name;
}

public test[] testArray = { {1,"name1"}, {2,"name2"} };

Give it a constructor and call that. Ex:

public struct test
{
public int id;
public string name;
public test (int id, string name)
{
this.id = id;
this.name = name;
}
}

public test[] testArray = { new test(1,"name1"), new test(2,"name2") };
 
N

Nick Hounsome

Steve Roberts said:
In C# is it possible to initialize the items of an array when the array is
an array of structures?



When I try to initialize the items of the array (as shown below) using the
method I would use in C++, I get the compiler error "Array initializers
can only be used in a variable or field initializer. Try using a new
expression instead.":



Is there a way to do this?

Thanks for any help,

Steve



public struct test

{

public test(int id,string name) { this.id = id; this.name = name; }
public int id;

public string name;

}



public test[] testArray = { {1,"name1"}, {2,"name2"} };

public test[] testArray = new test[] { new test(1,"name1"), new
test(2,"name2") };
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top