List Info

Thread: Collection was modified




Collection was modified
user name
2006-05-26 22:33:56
ahhh yeah ok....got it. problem is fixed now.

how would i copy the contents of one variable into another
one and not
have it just be a reference to the first variable but
independant from
it?

thanks for the help

ian

On 5/26/06, Joseph Bradvica <jbradvicasta-usa.com> wrote:
> The second variable is a 'reference' to the first. 
So you are really
> removing an item to the original.
>
> Joe B.
>
> -----Original Message-----
> From: merkimerk.com [mailto:merkimerk.com] On Behalf Of Ian Lipsky
> Sent: Friday, May 26, 2006 3:19 PM
> To: aspnetaspadvice.com
> Subject: [aspnet] RE: Collection was modified
>
>
> yeah i kinda realized that was probably was happening
about 2 minutes
> after i sent the email ;)
>
> however, i tried fixing it by using a 2nd variable to
store collection:
>
>                         ISet tmpcampaigns = campaigns;
>                         foreach (Campaign c in
campaigns)
>                         {
>                                 if (c.Disabled == true)
>                                 {
>                                        
tmpcampaigns.Remove(c);
>                                 }
>                         }
>
> but it still gives me the same error. I dont understan
why it would
> error now.
>
> thanks
>
> Ian
>
> On 5/26/06, Steven A Smith <steven.smithaspalliance.com> wrote:
> > The collection indexer can get confused if you
modify the collection
> in the
> > middle of a foreach.  The preferred way to do what
you're doing is to
> get
> > the Keys and loop through them, rather than
foreach-ing through the
> > collection directly.
> >
> > Steve
> >
> > -----Original Message-----
> > From: merkimerk.com [mailto:merkimerk.com] On Behalf Of Ian Lipsky
> > Sent: Friday, May 26, 2006 6:05 PM
> > To: aspnetaspadvice.com
> > Subject: [aspnet] Collection was modified
> >
> > can someone please tell me why this error is
occuring?
> >
> >
> > private ISet packages = new HybridSet(); private
ISet campaigns = new
> > HybridSet();
> >
> > private void Page_Load(object sender,
System.EventArgs e)
> >         {
> >                 packages = user.Packages;
> >                 campaigns = user.Campaigns;
> >                 BindControls();
> >         }
> >
> >
> > private void BindControls()
> >     {
> >
> >         foreach (Package p in packages)
> >                 {
> >                        
campaigns.RemoveAll(p.Campaigns);
> >                 }
> >
> >
> > ==>     foreach (Campaign c in campaigns)
> >                 {
> >                         if (c.Disabled == true)
> >                         {
> >                                
campaigns.Remove(c);
> >                         }
> >                 }
> >     }
> >
> > it fails on the 2nd foreach loop right on the
foreach line i marked.
> > I've tried wrapping the statements in lock()
which i found suggested
> on one
> > site but it makes no difference. The first foreach
loop works fine.
> >
> > ian
> >
> >
> > Need SQL Advice? http://sqladvice.com
> > Need RegEx Advice? http://regexadvice.com
Need XML Advice?
> > http://xmladvice.com
> >
> >
> >
> > Need SQL Advice? http://sqladvice.com
> > Need RegEx Advice? http://regexadvice.com
> > Need XML Advice? http://xmladvice.com
> >
> >
>
>
> Need SQL Advice? http://sqladvice.com
> Need RegEx Advice? http://regexadvice.com
> Need XML Advice? http://xmladvice.com
>
> Need SQL Advice? http://sqladvice.com
> Need RegEx Advice? http://regexadvice.com
> Need XML Advice? http://xmladvice.com
>
>


Need SQL Advice? http://sqladvice.com
Need RegEx Advice? http://regexadvice.com
Need XML Advice? http://xmladvice.com
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )