How to reach an object that dont have name
-
Hi all I have a problem about how to reach an object of a class that is in the memory but dont have name, I'll write an example to discuss if there is a class called man public class man { string Name; int length; } now the user of the program makes a lot of objects of the class man by using this code in a button private void button1_Click(object sender,System.EventArgs e) { man m = new man; m.Name = textbox1.Text; m.length = int.parse(textbox2.text); } now there are a lot of objects of the class man in the memory but they dont have a name. How can I reach an object of them or in other words how to save objects of a class so that i can reach them Note: that in my real program the class is not simple like that it contains complex data types mego mego
-
Hi all I have a problem about how to reach an object of a class that is in the memory but dont have name, I'll write an example to discuss if there is a class called man public class man { string Name; int length; } now the user of the program makes a lot of objects of the class man by using this code in a button private void button1_Click(object sender,System.EventArgs e) { man m = new man; m.Name = textbox1.Text; m.length = int.parse(textbox2.text); } now there are a lot of objects of the class man in the memory but they dont have a name. How can I reach an object of them or in other words how to save objects of a class so that i can reach them Note: that in my real program the class is not simple like that it contains complex data types mego mego
You need to store a reference to the object somewhere. An ArrayList is a good choice:
ArrayList Men; . . . private void button1_Click(object sender,System.EventArgs e) { man m = new man; m.Name = textbox1.Text; m.length = int.parse(textbox2.text); Men.Add(m); }
You can then access the objects through the ArrayList, for example to find the man with a name stored in a string:private man Find(string key) { foreach (man m in Men) { if (m.Name == key) { return m; } } return null; // didn't find it }
There are more efficient ways to do it and different collection you can use depending on exactly what you want and how much coding you want to do up front. For example, you could create your own collection which keeps the names in order so you can search them more efficiently. -
Hi all I have a problem about how to reach an object of a class that is in the memory but dont have name, I'll write an example to discuss if there is a class called man public class man { string Name; int length; } now the user of the program makes a lot of objects of the class man by using this code in a button private void button1_Click(object sender,System.EventArgs e) { man m = new man; m.Name = textbox1.Text; m.length = int.parse(textbox2.text); } now there are a lot of objects of the class man in the memory but they dont have a name. How can I reach an object of them or in other words how to save objects of a class so that i can reach them Note: that in my real program the class is not simple like that it contains complex data types mego mego
the m object not accessable outside button1_Click at all even if they have separate name because its scope so you need to store them like other response suggest and access them later by index or by key depending on the collection type MCAD
-
Hi all I have a problem about how to reach an object of a class that is in the memory but dont have name, I'll write an example to discuss if there is a class called man public class man { string Name; int length; } now the user of the program makes a lot of objects of the class man by using this code in a button private void button1_Click(object sender,System.EventArgs e) { man m = new man; m.Name = textbox1.Text; m.length = int.parse(textbox2.text); } now there are a lot of objects of the class man in the memory but they dont have a name. How can I reach an object of them or in other words how to save objects of a class so that i can reach them Note: that in my real program the class is not simple like that it contains complex data types mego mego