I'm having trouble creating instances of classes through a GUI. I've only manually created objects before where I've named them myself, how do I do it so for eg through the click of a button where I can automatically create the instance of a class(automatically naming the object)??

Recommended Answers

All 3 Replies

I'm still not quite sure what you mean. Could you give an example please?

private void button1_Click(object sender, EventArgs e)
    {
        Item item1 = new Item("123", "MBE");     
        items.Add(item1);
    }

Every time I call this button I want to create a unique instance of a class, so that it's not overwriting item1, instead it'll create e.g. item2, item3 etc...

But that's what you're doing when you add it to the list. That list now has a reference to item1. When you leave the method, item1 as a reference, is destroyed by the GC. So when you create a new item1 on the next click, your list still contains the old item1 as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.