Cannot change a property of a structure stored in another property

I have a small problem, I cannot change the structure property that is stored in another property, and I have no idea why:

the code:

    public struct InfoProTile
    {
        public string Nazev { get; set; }
        public double Hodnota { get; set; }
        public TypValue TypValue { get; set; }
        public NavigovatNa NavigovatNa { get; set; }
    }

        public InfoProTile BNDTileInfo { get { return bndTileInfo; } set { bndTileInfo = value; } } private InfoProTile bndTileInfo;

        LoadModel()
    {
        ...
        BNDTileInfo = new InfoProTile();
        BNDTileInfo.NavigovatNa = NavigovatNa.MainPage;
    }

Error:

Error: Error 3 Cannot modify the return value of '...ViewModel.TilesModel.BNDTileInfo' because it is not a variable

I do not understand, because if you just change it to:

bndTileInfo.NavigovatNa = NavigovatNa.MainPage;

It works. Can someone explain this to me?

+3
source share
2 answers

A structis the type of value, so when you access it from a property, you return a copy struct, not the direclty base field. Thus, C # will not allow you to change this copy of the structure, because it is just a temporary object that will be discarded.

So when you call:

BNDTitleInfo.NavigovatNa = ...;

get bndTitleInfo, NavigovatNa . # , , , .

:

BNDTitleInfo.NavigovatNa = ...;

, , , .

, ( ).

, (class), struct, , , .

MSDN, struct , - :

+7

BNDTileInfo , . , , .. :

var tmp = BNDTileInfo; // tmp is a completely separate clone
tmp.NavigovatNa = NavigovatNa.MainPage; // change the local clone
// discard the clone - our change was wasted

. , struct , .. , .

: struct. , , 99% , struct. , 98,5% , struct, : " struct, InfoProTile".

BNDTileInfo a class, , struct, , , struct.

+7

All Articles