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. Accessing members of a class in an ArrayList

Accessing members of a class in an ArrayList

Scheduled Pinned Locked Moved C#
learningcsharpalgorithmsdata-structuressales
8 Posts 6 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.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I am a beginner who is learning C# and have come across a problem that, while I have a workaround, would like to find out what I am doing wrong. I have tried searching, but I doubt I am using the correct terminology. Any assistance is very much appreciated! If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class? public class Customer { public Name; } ArrayList myArray = new ArrayList(); Customer c = new Customer(); ArrayList.Add(c); // Is there a way to do this? ArrayList[0].Name = "Bob Smith"; // This is how I've done the workaround Customer temp = new Customer(); // Create a new customer temp object temp = ArrayList[0]; // Assign the customer from the array MessageBox.Show(temp.Name); My understanding of why the first way doesn't work is because an ArrayList could hold different object types. So I need to box/unbox it before I can use it. Is there some other way to go about doing what I am attempting here withouth the boxing/unboxing(I think these are the right terms)? If I am not using the terminology correctly, please let me know as that may be why my searches are comming up with very little information on what I am trying to do. Thank you for your time and your patience with a beginner!

    L J J G 4 Replies Last reply
    0
    • H hpjchobbes

      I am a beginner who is learning C# and have come across a problem that, while I have a workaround, would like to find out what I am doing wrong. I have tried searching, but I doubt I am using the correct terminology. Any assistance is very much appreciated! If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class? public class Customer { public Name; } ArrayList myArray = new ArrayList(); Customer c = new Customer(); ArrayList.Add(c); // Is there a way to do this? ArrayList[0].Name = "Bob Smith"; // This is how I've done the workaround Customer temp = new Customer(); // Create a new customer temp object temp = ArrayList[0]; // Assign the customer from the array MessageBox.Show(temp.Name); My understanding of why the first way doesn't work is because an ArrayList could hold different object types. So I need to box/unbox it before I can use it. Is there some other way to go about doing what I am attempting here withouth the boxing/unboxing(I think these are the right terms)? If I am not using the terminology correctly, please let me know as that may be why my searches are comming up with very little information on what I am trying to do. Thank you for your time and your patience with a beginner!

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      hpjchobbes wrote:

      without having to first cast it to a new class?

      .NET 1.X collections require casting. .NET 2.0 introduced Generic Containers (System.Collections.Generic) that provide typed Collection capabilities.

      led mike

      1 Reply Last reply
      0
      • H hpjchobbes

        I am a beginner who is learning C# and have come across a problem that, while I have a workaround, would like to find out what I am doing wrong. I have tried searching, but I doubt I am using the correct terminology. Any assistance is very much appreciated! If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class? public class Customer { public Name; } ArrayList myArray = new ArrayList(); Customer c = new Customer(); ArrayList.Add(c); // Is there a way to do this? ArrayList[0].Name = "Bob Smith"; // This is how I've done the workaround Customer temp = new Customer(); // Create a new customer temp object temp = ArrayList[0]; // Assign the customer from the array MessageBox.Show(temp.Name); My understanding of why the first way doesn't work is because an ArrayList could hold different object types. So I need to box/unbox it before I can use it. Is there some other way to go about doing what I am attempting here withouth the boxing/unboxing(I think these are the right terms)? If I am not using the terminology correctly, please let me know as that may be why my searches are comming up with very little information on what I am trying to do. Thank you for your time and your patience with a beginner!

        J Offline
        J Offline
        Jimmanuel
        wrote on last edited by
        #3

        there's always

        List< Customer > myArray = new List< Customer > ();

        if you aren't tied to using an ArrayList

        1 Reply Last reply
        0
        • H hpjchobbes

          I am a beginner who is learning C# and have come across a problem that, while I have a workaround, would like to find out what I am doing wrong. I have tried searching, but I doubt I am using the correct terminology. Any assistance is very much appreciated! If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class? public class Customer { public Name; } ArrayList myArray = new ArrayList(); Customer c = new Customer(); ArrayList.Add(c); // Is there a way to do this? ArrayList[0].Name = "Bob Smith"; // This is how I've done the workaround Customer temp = new Customer(); // Create a new customer temp object temp = ArrayList[0]; // Assign the customer from the array MessageBox.Show(temp.Name); My understanding of why the first way doesn't work is because an ArrayList could hold different object types. So I need to box/unbox it before I can use it. Is there some other way to go about doing what I am attempting here withouth the boxing/unboxing(I think these are the right terms)? If I am not using the terminology correctly, please let me know as that may be why my searches are comming up with very little information on what I am trying to do. Thank you for your time and your patience with a beginner!

          J Offline
          J Offline
          J 0
          wrote on last edited by
          #4

          hpjchobbes wrote:

          My understanding of why the first way doesn't work is because an ArrayList could hold different object types. So I need to box/unbox it before I can use it

          Sounds about right to me. You could use a Generic List to achieve what you're trying to do. It is in the System.Collections.Generic namespace, and is similar to an array list, but the items contained in it are strongly typed (so you won't have to do the boxing/unboxing) You could use it as follows: List<Customer> myCustomerList = new List<Customer>(); Customer customer = new Customer(); myCustomerList.Add(customer); myCustomerList[0].Name = "Bob Smith"; ---- Edit: Doh! Too slow...

          1 Reply Last reply
          0
          • H hpjchobbes

            I am a beginner who is learning C# and have come across a problem that, while I have a workaround, would like to find out what I am doing wrong. I have tried searching, but I doubt I am using the correct terminology. Any assistance is very much appreciated! If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class? public class Customer { public Name; } ArrayList myArray = new ArrayList(); Customer c = new Customer(); ArrayList.Add(c); // Is there a way to do this? ArrayList[0].Name = "Bob Smith"; // This is how I've done the workaround Customer temp = new Customer(); // Create a new customer temp object temp = ArrayList[0]; // Assign the customer from the array MessageBox.Show(temp.Name); My understanding of why the first way doesn't work is because an ArrayList could hold different object types. So I need to box/unbox it before I can use it. Is there some other way to go about doing what I am attempting here withouth the boxing/unboxing(I think these are the right terms)? If I am not using the terminology correctly, please let me know as that may be why my searches are comming up with very little information on what I am trying to do. Thank you for your time and your patience with a beginner!

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            hpjchobbes wrote:

            If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class?

            You don't cast the object to a new class, you cast the reference of the object to the class. Creating a new class instance serves no purpose at all, that instance would be thrown away when you assign the new reference to the variable. Customer temp; // Declare a reference temp = (Customer)ArrayList[0]; // Cast the object from the list

            hpjchobbes wrote:

            So I need to box/unbox it before I can use it.

            Actually, boxing is only used for value types. If you for example store integers in an ArrayList, each integer is boxed inside an object which is stored on the heap. That way the integer becomes a reference type so that the reference can be stored in the ArrayList. If you are using framework 2, you can use a generic list instead. They are type safe, so there is no need for any casting: List<Customer> list = new List<Customer>(); list.Add(new Customer()); list[0].Name = "Eric Gently"; Customer temp = list[0]; Generic lists also works with value types, so there is no boxing if you store value types in a list.

            Experience is the sum of all the mistakes you have done.

            H 1 Reply Last reply
            0
            • G Guffa

              hpjchobbes wrote:

              If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class?

              You don't cast the object to a new class, you cast the reference of the object to the class. Creating a new class instance serves no purpose at all, that instance would be thrown away when you assign the new reference to the variable. Customer temp; // Declare a reference temp = (Customer)ArrayList[0]; // Cast the object from the list

              hpjchobbes wrote:

              So I need to box/unbox it before I can use it.

              Actually, boxing is only used for value types. If you for example store integers in an ArrayList, each integer is boxed inside an object which is stored on the heap. That way the integer becomes a reference type so that the reference can be stored in the ArrayList. If you are using framework 2, you can use a generic list instead. They are type safe, so there is no need for any casting: List<Customer> list = new List<Customer>(); list.Add(new Customer()); list[0].Name = "Eric Gently"; Customer temp = list[0]; Generic lists also works with value types, so there is no boxing if you store value types in a list.

              Experience is the sum of all the mistakes you have done.

              H Offline
              H Offline
              hpjchobbes
              wrote on last edited by
              #6

              Thanks for the answers everyone! I am using 2.0 so I looked at the generic List and it seems to do what I need! Looks like I have yet another topic to add to my list of things to learn: When to use ArrayList or List Thanks again for the information and assistance!!

              L G 2 Replies Last reply
              0
              • H hpjchobbes

                Thanks for the answers everyone! I am using 2.0 so I looked at the generic List and it seems to do what I need! Looks like I have yet another topic to add to my list of things to learn: When to use ArrayList or List Thanks again for the information and assistance!!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                hpjchobbes wrote:

                Looks like I have yet another topic to add to my list of things to learn: When to use ArrayList or List

                There's no need to use ArrayList in .NET 2.0 anymore.

                1 Reply Last reply
                0
                • H hpjchobbes

                  Thanks for the answers everyone! I am using 2.0 so I looked at the generic List and it seems to do what I need! Looks like I have yet another topic to add to my list of things to learn: When to use ArrayList or List Thanks again for the information and assistance!!

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  hpjchobbes wrote:

                  When to use ArrayList

                  That is very easy: Never. If you ever need a list of object references, use List<object> instead of ArrayList. Most of the time when you store objects of different types in a list, they still have a common base type that you can use rather than an object reference, like for example List<Control> for storing references to any type of controls.

                  Experience is the sum of all the mistakes you have done.

                  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