SoCalChris
asked on
Copying a single dimension array into a structure array
Hi, I've got a structure with multiple strings. I want to copy a string array into the structure array. Any suggestions? I don't want the string copied into all elements of the structure array, just the one I specify.
protected struct my_struct
{
protected string string1;
protected string string2;
protected string string3;
}
protected void doSomeWork()
{
string[] myStringArray = new string [5];
my_struct[] myStructureArray = new my_struct[5];
myStringArray[0] = "a";
myStringArray[1] = "b";
myStringArray[2] = "c";
myStringArray[3] = "d";
myStringArray[4] = "e";
//Here's basically where I'm stuck!
Array.Copy(myStringArray, myStructArray.string1, 1);
}
Any help would be appreciated. I have a feeling this is something simple that I'm missing.
protected struct my_struct
{
protected string string1;
protected string string2;
protected string string3;
}
protected void doSomeWork()
{
string[] myStringArray = new string [5];
my_struct[] myStructureArray = new my_struct[5];
myStringArray[0] = "a";
myStringArray[1] = "b";
myStringArray[2] = "c";
myStringArray[3] = "d";
myStringArray[4] = "e";
//Here's basically where I'm stuck!
Array.Copy(myStringArray, myStructArray.string1, 1);
}
Any help would be appreciated. I have a feeling this is something simple that I'm missing.
also the members of your struct need to be public.
ASKER
Do you know how to do this without having to loop through each array item?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
string[] myStringArray = new string [5];
myStringArray[0] = "a";
myStringArray[1] = "b";
myStringArray[2] = "c";
myStringArray[3] = "d";
myStringArray[4] = "e";
my_struct myStructArray = new my_struct;
for(int i=0; i<5; i++)
myStructArray.string1+=myS