Hi.

I have this kind of common problem, but cant find a solution for it after many google searches.

What i want is this:

ArrayList items = new ArrayList();
items.add(item1); //the item object contains several properties
items.add(item2);
items.add(item3);

foreach (item i in items)
{
     Create and edit a new unique object
     based on the content in this 'item' object
}

Hope you can help.

Thanks alot!

Don't know what you mean by new unique object
Could this help?

namespace ConsoleApplication1
{
    class item
    {
        public int avalue = 1;
        public int bvalue = 2;
    }

    class Program
    {
        static void Main(string[] args)
        {
            ArrayList items = new ArrayList();
            item item1 = new item();
            items.Add(item1); //the item object contains several properties
            item item2 = new item(); 
            item2.avalue = 3;
            items.Add(item2);
            item item3 = new item();
            items.Add(item3);

            foreach (item i in items)
            {
                Console.WriteLine(i.avalue);
                 //Create and edit a new unique object
                 //based on the content in this 'item' object
            }
            
            Console.ReadKey();
        }
    }
}

It should print out 1 3 1
I also would advise you learn something about generic List class.

Thanks alot for your answer!

But It´s not what I am looking for. What i want is to create a new object, lets say a Label, in the foreach loop:

foreach (item i in items)
{        
      Label labelname = new Label(i.value);
}

As you can see, I cannot do this since it would create a new label with the same name. So how do i create a new Label in there, with a unique name to which i can refer to later on? Or is there another way around to solve this problem?

Thank you :)

Don't know what you mean by new unique object
Could this help?

namespace ConsoleApplication1
{
    class item
    {
        public int avalue = 1;
        public int bvalue = 2;
    }

    class Program
    {
        static void Main(string[] args)
        {
            ArrayList items = new ArrayList();
            item item1 = new item();
            items.Add(item1); //the item object contains several properties
            item item2 = new item(); 
            item2.avalue = 3;
            items.Add(item2);
            item item3 = new item();
            items.Add(item3);

            foreach (item i in items)
            {
                Console.WriteLine(i.avalue);
                 //Create and edit a new unique object
                 //based on the content in this 'item' object
            }
            
            Console.ReadKey();
        }
    }
}

It should print out 1 3 1
I also would advise you learn something about generic List class.

What about:

string labelName = "label_" = i.value;
Label temp = new Label();
Label.Name = labelName;

Yea i guess that would work. Not the most elegant solution I am looking for, but It would work.

Thanks :)

What about:

string labelName = "label_" = i.value;
Label temp = new Label();
Label.Name = labelName;

If you want to be able to access those labels after your loop, you can

(a) store them in an array
(b) store them in a List<Label>
(c) store them paired up with their corresponding Item objects in a Dictionary<Item, Label>
(d) access them via the form's Control collection (assuming you're adding them to a form)
(e) ...some other method...

Edited 6 Years Ago by apegram: n/a

Totally agree with Powerbox.

Web : Design Patterns
Book: Design Patterns in C#

However, you can create a list of generic objects

class MyObject
{
   public Label myLabel {get;set;}   
}
...
List<object> myObjects = new List<object>;
myObjects.Add(new Label());
myObjects.Add(new MyObject());
((Label)myObjects[0]).Text = "Hello World!"; 
((MyObject)myObjects[1]).myLabel.Text = "!dlroW olleH"

This is a low down and dirty way of doing things and you should use patterns to build something that is manageable and maintainable. Just saying if you know the basics, you can learn to do patterns as well.

This question has already been answered. Start a new discussion instead.