要修改自定義結構的值,應該用Class而不是Struct

轉載文章http://blog.csdn.net/onlyou930/article/details/5568319

 

編譯錯誤 CS1612 
Cannot modify the return value of ‘expression‘because it is not a variable
 
無法修改“expression”的返回值,因爲它不是變量 
出現這種錯誤的最常見情況是: 
AnObject.AnStruct.Vaule = xxx; 
考慮如下程序:初看沒什麼問題,實際上根本無法編譯 
usingSystem; 
usingSystem.Collections.Generic; 
usingSystem.Text; 
namespaceConsoleApplication1 
{ 
publicstruct Rectangle 
{ 
privateint _Width; 
privateint _Height; 
publicint Width 
{ 
get{ return _Width; } 
set{ _Width = value; } 
} 
publicint Height 
{ 
get{ return _Height; } 
set{ _Height = value; } 
} 
publicRectangle(int width, int height) 
{ 
this._Width= width; 
this._Height= height; 
} 
} 
publicclass Draw 
{ 
privateRectangle _rect = new Rectangle(0, 0); 
publicRectangle MyRect 
{ 
get{ return _rect; } 
set{ _rect = value; } 
} 
publicDraw() { } 
publicDraw(int mapWidth, int mapHeight) 
{ 
_rect= new Rectangle(mapWidth, mapHeight); 
} 
} 
classProgram 
{ 
staticvoid Main(string[] args) 
{ 
Drawdraw = new Draw(); 
draw.MyRect.Width= 20;//《==編譯錯誤 
draw.MyRect.Height= 12;《==編譯錯誤 
} 
} 
} 
draw是引用類型,MyRect是struct,也就是值類型,draw.MyRect會調用 
publicRectangle MyRect 
{ 
get{ return _rect; } 
} 
而值類型默認是按值傳遞的,返回的是棧上_rect的一份臨時的、本地的拷貝,我們暫且稱之爲temp_rect, 
draw.MyRect.Width= 20 等同於temp_rect.Width = 20; 
因此即使能對它進行修改也無法反映到draw.MyRect本身(即_rect)的,即這是一個毫無何意義的操作,因此編譯器就從源頭上禁止了這樣的操作。 
解決方法: 
1)把struct替換成class,這樣,MyRect就成了一個對象(引用類型),draw.MyRect就會返回_rect在堆上的實際地址,修改也會反映到實際的對象 
2)如果非要用struct不可的話,需要設置一箇中間變量: 
Drawdraw = new Draw(); 
RectangletempRect = new Rectangle(); 
tempRect.Width= 20; 
tempRect.Height= 12; 
draw.MyRect= tempRect; 
這樣實際上是替換draw對象的整個_rect屬性。 
參考: 
MSDN: Compiler ErrorCS1612 編譯器錯誤 CS1612(C#)  
看一下就知道了,中文的比英文原版的遜多了 
C# - Struct in aclass.. can not access properties of thestruct 
延伸閱讀: 
CLR GenericsLimitation - Modifying Values In Situ In aContainer
 
C#: List ofstruct

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章