我有我使用的學習C#結構這個非常簡單的例子:調用非默認的結構構造函數內的構造方法
struct ScreenPosition
{
// These are the two private members of the structure
private int x;
private int y;
private int RangeCheckedX(int xPos)
{
if (xPos < 0 || xPos > 1280)
{
throw new ArgumentOutOfRangeException("X");
}
return xPos;
}
private int RangeCheckedY(int yPos)
{
if (yPos < 0 || yPos > 1024)
{
throw new ArgumentOutOfRangeException("Y");
}
return yPos;
}
// Declaring the non-default constructor
public ScreenPosition(int X, int Y)
{
this.x = RangeCheckedX(X); // ERROR HERE
this.y = RangeCheckedY(Y); // ERROR HERE
}
// Declaring the property X - Follows a syntax. See the C# quick reference sheet
public int X
{
get
{
return this.x;
}
set
{
this.x = RangeCheckedX(value);
}
}
// Declaring the property X - Follows a syntax. See the C# quick reference sheet
public int Y
{
get
{
return this.y;
}
set
{
this.y = RangeCheckedY(value);
}
}
}
我得到這個錯誤在「ERROR HERE」註釋行:
的之前所有字段被分配到
不能使用「這個」對象是不是非法調用結構的方法在非默認構造函數爲結構成員賦值?
請檢查以下內容:http://www.jaggersoft.com/pubs/StructsVsClasses.htm 我在兩分鐘內就瞭解了更多關於結構的信息,這比我想象的更多。 – 2009-11-04 05:55:24
您正在創建一個可變結構。我會重新考慮這個設計。 – sisve 2009-11-04 06:42:18