Accessing members of a class in an ArrayList
-
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! -
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! -
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! -
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!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... -
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!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.
-
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.
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!!
-
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!!
-
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!!
hpjchobbes wrote:
When to use ArrayList
That is very easy: Never. If you ever need a list of
object
references, useList<object>
instead ofArrayList
. 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 anobject
reference, like for exampleList<Control>
for storing references to any type of controls.Experience is the sum of all the mistakes you have done.