Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

error very strange

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Argentino Douglas - 13 Nov 2006 20:00 GMT
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 - 13 Nov 2006 20:09 GMT
Any questions or answers ? Looks like nobody is in now.

> a function, whose parameter is a ref to a collection of class I
> private void func(List<I> i){
[quoted text clipped - 11 lines]
>
> Thanks
Jon Skeet [C# MVP] - 13 Nov 2006 20:11 GMT
> a function, whose parameter is a ref to a collection of class I
> private void func(List<I> i){
[quoted text clipped - 9 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Argentino Douglas - 13 Nov 2006 20:24 GMT
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

Jon Skeet [ C# MVP ] wrote:
> > a function, whose parameter is a ref to a collection of class I
> > private void func(List<I> i){
[quoted text clipped - 20 lines]
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 13 Nov 2006 21:36 GMT
> public struct I{
>     public int value;
[quoted text clipped - 17 lines]
> }
> 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!

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.