Jump to content
  • Advertisement
Sign in to follow this  

[.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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by CProgrammer
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.


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!