Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to reach an object that dont have name

How to reach an object that dont have name

Scheduled Pinned Locked Moved C#
tutorialperformancehelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mego_z20
    wrote on last edited by
    #1

    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

    W M M 3 Replies Last reply
    0
    • M mego_z20

      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

      W Offline
      W Offline
      Wjousts
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M mego_z20

        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

        M Offline
        M Offline
        Mohamad Al Husseiny
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M mego_z20

          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

          M Offline
          M Offline
          mego_z20
          wrote on last edited by
          #4

          thanx to u Wjousts and thanx to mohamed also i think that the array list is a good choice i ll try it

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups