Convert Class Object to Struct?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Convert Class Object to Struct?

  1. #1
    Join Date
    Jul 2011
    Posts
    1

    Unhappy Convert Class Object to Struct?

    Hi all - is it possible to take a class object (ie, a list), and create a copy of it as a struct?

    i have a List<> object (a) that i want to add to another larger list (b), and then clear the list (a) to repeat the procedure.. ideally, getting the larger list of lists (b) containing a whole bunch of (a)s.... but as you probably already figured out, once i clear the list (a), all the data i passed to the larger list (b) gets cleared as well. there are placeholders in the larger list (b) but no data.

    is there a way to take a 'frozen snapshot' of the list data (a) to then pass to the larger list? perhaps copying the list as a struct object?

    any help is deeply appreciated.

    thanks

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Convert Class Object to Struct?

    I tested two ways, using three lists to add to a bigger list, then clearing one to see if the larger list still contained the added values, then by using a single small list in a loop to add to the bigger list which gets destroyed after each iteration... both tests seem to leave all the values added in the big list. Do these two test no accurately reflect what you are basically trying to do?

    Code:
            static void Test1()
            {
                List<int> BigList = new List<int>();
    
                List<int> smallOnes = new List<int>();
                List<int> smallTwos = new List<int>();
                List<int> smallThrees = new List<int>();
    
                for (int val = 10; val < 20; val++) smallOnes.Add(val);
                for (int val = 20; val < 30; val++) smallTwos.Add(val);
                for (int val = 30; val < 40; val++) smallThrees.Add(val);
    
                BigList.AddRange(smallOnes);
                BigList.AddRange(smallTwos);
                BigList.AddRange(smallThrees);
    
                smallTwos.Clear();
    
                foreach (int val in BigList)
                    Console.WriteLine(val);
            }
            static void Test2()
            {
                List<int> BigList = new List<int>();
    
                for (int i = 1; i < 4; i++)
                {
                    List<int> smallList = new List<int>();
                    for (int val = i*10; val < ((i*10) + 10); val++) smallList.Add(val);
                    BigList.AddRange(smallList);
                }
    
                foreach (int val in BigList)
                    Console.WriteLine(val);
            }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center