Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList in C#, ASP.NET

Status
Not open for further replies.

ludmann

Programmer
Apr 14, 2004
49
GB
I wonder if anyone can help, although this is not specifically an ASP.NET question, but I am programming ASP.NET (c#) and had so many great answers on this forum.

I have an ArrayList which has elements of type Address
eg.
ArrayList address;
address.Add(new Address("Boston","N453"));
address.Add(new Address("London", "NW8 E89"));

public class Address
{
private string city;
private string postcode;

public address(string ct, string pc)
{
city = ct;
postcode =pc;
}

public string City
{
get { return city; }
}

public string Postcode
{
get { return postcode}
}
}

At another point in the application I need to pass an ArrayList as a parameter

someMethod(string a, ArrayList b)
{
???
}

I want to pass my address ArrayList to this method and get the values (in string format)

How would I do this?


Marika

 
Hi Marika

Because the ArrayList is a reference type when you pass it to a method in reality you are creating a new reference to the same ArrayList object. Hence if you read or modify the list within the method you will be reading or modifying from the original list like this
Code:
void Foo(){
  ArrayList list = new ArrayList
  list.Add("string1");
  list.Add("string2");
  Bar(list);
  Response.Write(list.Count.ToString());
}

string Bar(ArrayList list){
  list.Add("string3");
}

output: 3

So really you have the method already you just need to call it passing in your list. What do you mean by the values in string format? Do you want all the values in the ArrayList returned as a concatenated string like this?
Code:
string ParseList(ArrayList list){
  string values;
  foreach(string s in list) values += s;
  return values;
}
Hopefully that will help if not can you clarify what you wanted SomeMEthod to do?

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Thanks, I have since figured it out.
I wanted to know how to get to the values of the ArrayList.
The difficulty was that the elements of the ArrayList were Objects (Addresses)

So I realised I need to cast the ArrayList
Like
ArrayList b;
((Address)b[1]).City;
((Address)b[1]).Postcode;

and if it isn't a string I should use ToString()

I guess I didn't explain properly, sorry
 
The 2005 version of the .NET framework will support Generics, where you won't have to do this anymore. Your collection types will hold objects of the correct type, and not of type Object.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top