Skip to main content
GameDev.net gamedev.net
🔒 Locked

ListStructure Problem in C#

Started by Zmurf Sep 25, 2006 at 7:07 AM 2 replies 400+ views
Original Post
Zmurf
Zmurf
I have a list variable of a structure type, and I want to change the value of a variable inside the structure of a part of the list at position 'index'. But I keep getting a compiler error:
Quote:
Error 1 Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable C:\Documents and Settings\Zmurf\My Documents\Visual Studio 2005\Projects\MarioGame\MarioGame\Sprites.cs 57 9 MarioGame
Heres my code:
    protected struct SpriteProperties
    {        
        public Texture2D m_sTexture;
        public int m_sHeight, m_sWidth, m_sX, m_sY;
        public int m_sFrames;
        public SpriteEffects m_sEffects;
        public SpriteBlendMode m_sBlendMode;

        public SpriteProperties(Texture2D texture,
            int width, int height, int x, int y, int frames,
            SpriteEffects effects, SpriteBlendMode blendmode)
        {
            m_sTexture = texture;
            m_sWidth = width;
            m_sHeight = height;
            m_sX = x;
            m_sY = y;
            m_sFrames = frames;
            m_sEffects = effects;
            m_sBlendMode = blendmode;
        }
    };
    List<SpriteProperties> m_Sprites;

public void SpriteEffect(int index, SpriteEffects effect)
    {
        m_Sprites[index].m_sEffects = effect;
    }
I've looked at the error help and did what they suggested but it did not work ,then I tried referencing the SpriteEffect in the array's structure but that just got me even more errors, I'm new to C# because I want to try the XNA Game Studio.
If it's possible for us to conceive it, than it must be possible.
SamLowry
SamLowry
If you're familiar with C++, you're trying to do something like

int f(){    return 0;}f()++;


In your C# code, you have a list of structs. When retrieving an element, you receive a (temporary) copy of the element (because structs are "value types"), and you're trying to modify this temporary variable. Theoretically, this should be possible, but it's a good thing the C# compiler considers this an error, since modifying a temporary value is seldom what you want to do.

In summary, modifying a value requires you to have access to its l-value, which you don't have when working with structs.
Zmurf
Zmurf
So how can I get around this?

EDIT:

Wait, I did it. I had to replace that index in the lsit with a whole new Structure instance identical to the previous one but changing only the value I want.
If it's possible for us to conceive it, than it must be possible.
SamLowry
SamLowry
Yes, that's one solution. Another way would be to turn the struct into a class, or use some sort of boxing (I'm not sure how it would work with the automatic boxing in C#, but you can certainly make it work if you do it manually).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.