error very strange

  • Thread starter Thread starter Argentino Douglas
  • Start date Start date
A

Argentino Douglas

a function, whose parameter is a ref to a collection of class I
private void func(List<I> i){
for(int n=0;n<MAXVALUE;++n){

for(int m=0;m<MAXVALUE;++m){

Dosomething();
}

i[n].value=5; [******]Eror: can't modify the value of
System.Collections.Generic.List<program.Form1.I>.this[int] because it
is not a variable[/******]
}


Thanks
 
Argentino Douglas said:
a function, whose parameter is a ref to a collection of class I
private void func(List<I> i){
for(int n=0;n<MAXVALUE;++n){

for(int m=0;m<MAXVALUE;++m){

Dosomething();
}

i[n].value=5; [******]Eror: can't modify the value of
System.Collections.Generic.List<program.Form1.I>.this[int] because it
is not a variable[/******]
}

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
public struct I{
public int value;
}

private void ValuableHelper(List<I>i){

i=new List<I>();

for(int n=0;n<MAXVALUE;++n){

for(int m=0;m<MAXVALUE;++m){

PleaseHelpMe();

}

i[n].value=5; //Error is here

}
}
I am coding it as a window application,
Thanks

Argentino Douglas said:
a function, whose parameter is a ref to a collection of class I
private void func(List<I> i){
for(int n=0;n<MAXVALUE;++n){

for(int m=0;m<MAXVALUE;++m){

Dosomething();
}

i[n].value=5; [******]Eror: can't modify the value of
System.Collections.Generic.List<program.Form1.I>.this[int] because it
is not a variable[/******]
}

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Argentino Douglas said:
public struct I{
public int value;
}

private void ValuableHelper(List<I>i){

i=new List<I>();

for(int n=0;n<MAXVALUE;++n){

for(int m=0;m<MAXVALUE;++m){

PleaseHelpMe();

}

i[n].value=5; //Error is here

}
}
I am coding it as a window application,

Well, firstly, that's *not* a short but complete program. Please read
http://www.pobox.com/~skeet/csharp/incomplete.html

However, the cause of your problem is that you've got a struct - a
value type - and you're trying to change a value which is returned from
an indexer. You can't do that, and even if you did, it wouldn't do what
you wanted it to.

i[n] returns a value. If you modify that value, it won't change the
contents of the list at all.

Now, if I were a reference type, you'd be modifying the value *referred
to* by the reference in the list, and that would be a different matter.

This kind of problem is just one reason why mutable value types are
generally a bad idea.

Oh, and regards your other post: you can't expect replies *that*
quickly. You only left your original message for 9 minutes before
posting again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top