# [.net] Arraylist with objects (C#)

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok... i'm being really stupid here or something...

struct ListEntry
{
string d_name;
string d_data;
bool d_selected;
}

public void clearAllSelections()
{
ListEntry thisItem;
IEnumerator listEnumerator = d_theList.GetEnumerator();

while(listEnumerator.MoveNext())
{
thisItem = (ListEntry)listEnumerator.Current;
thisItem.d_selected = false;
}
}


But this won't work will it? Because listEnumerator.Current gives you a copy of the item. So how do i go about changed the objects within a Collection? I tried d_theList.Remove and d_theList.Insert but this throws an exception because the collection has changed and so the IEnumerator is no longer active. Any ideas?

##### Share on other sites
Im quite sure that Enumerators can only be used to read data.

Try using direct indexing:
ArrayList al;
al; //Yup this works because ArrayList has the necessary get/set rules defined.

Im pretty sure that allows for setting data aswell.

##### Share on other sites
Quote:
 Original post by CProgrammerIm quite sure that Enumerators can only be used to read data.Try using direct indexing:ArrayList al;al; //Yup this works because ArrayList has the necessary get/set rules defined.Im pretty sure that allows for setting data aswell.

Ok, so how do i set al.d_selected = true?

##### Share on other sites
you can either use a class instead of a struct for ListEntry and have no problems with copying/boxing/unboxing or you enumerate with a for (i ...) loop instead of foreach or while (you can change the item at i easily then).
If possible use generics, that makes enumerating with for a lot easier (no convertions or boxing issues).

##### Share on other sites
Ah... i think i get it now.

I might be wrong.. but are structs passed by value and objects by reference?

##### Share on other sites
>Ah... i think i get it now.
>
>I might be wrong.. but are structs passed by value and objects by reference?

Yes! That is the difference.

Cheers

• 10
• 13
• 14
• 18
• 15