Solved

Copying a single dimension array into a structure array

Posted on 2004-10-26
188 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
Question by:SoCalChris
    4 Comments
     
    LVL 8

    Expert Comment

    by:AaronReams
    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
    also the members of your struct need to be public.
    0
     

    Author Comment

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

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    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…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now