[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Copying a single dimension array into a structure array

Posted on 2004-10-26
6
Medium Priority
?
237 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:SoCalChris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 8

Expert Comment

by:AaronReams
ID: 12414084
Here's one way to do it...

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+=myStringArray[i];
0
 
LVL 8

Expert Comment

by:AaronReams
ID: 12414135
also the members of your struct need to be public.
0
 

Author Comment

by:SoCalChris
ID: 12414154
Do you know how to do this without having to loop through each array item?
0
 
LVL 8

Accepted Solution

by:
AaronReams earned 1000 total points
ID: 12414193
Here's the other option... but you're copying an array to an array, not consolidating an array to one string.  is that what you want?

protected struct my_struct
{
    public string[] string1;
    public string[] string2;
    public string[] string3;
}

string[] myStringArray = new string [5];
my_struct[] myStructArray = new my_struct[5];
myStringArray[0] = "a";
myStringArray[1] = "b";
myStringArray[2] = "c";
myStringArray[3] = "d";
myStringArray[4] = "e";

myStructArray[0].string1 = new string[5];

//Here's basically where I'm stuck!
Array.Copy(myStringArray, myStructArray[0].string1, 1);

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question