2015-01-04 48 views
-2

我有這個類:無效令牌「=」類,結構或接口成員聲明

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ComputerGraphicsExercises 
{ 
    class Adamak 
    { 
     public int BodyX1; 
     public int BodyX2; 
     public int HeadX; 
     public int LeftArmX1; 
     public int LeftArmX2; 
     public int RightArmX1; 
     public int RightArmX2; 
     public int LeftLegX1; 
     public int LeftLegX2; 
     public int RightLegX1; 
     public int RightLegX2; 
     public int LeftEyeX; 
     public int RightEyeX; 
     public int PupilOfLeftEyeX; 
     public int PupilOfRightEyeX; 
     public int NoseX; 
     public int LipsX; 
     public int HairX; 
     public int LeftEarX; 
     public int RightEarX; 

     public int BodyY1; 
     public int BodyY2; 
     public int HeadY; 
     public int LeftArmY1; 
     public int LeftArmY2; 
     public int RightArmY1; 
     public int RightArmY2; 
     public int LeftLegY1; 
     public int LeftLegY2; 
     public int RightLegY1; 
     public int RightLegY2; 
     public int LeftEyeY; 
     public int RightEyeY; 
     public int PupilOfLeftEyeY; 
     public int PupilOfRightEyeY; 
     public int NoseY; 
     public int LipsY; 
     public int HairY; 
     public int LeftEarY; 
     public int RightEarY; 
    } 
} 

我宣佈這樣一個變量:

namespace ComputerGraphicsExercises 
{ 
    public partial class Main : Form 
    { 
     Adamak iMan2 = new Adamak(); 
    } 
} 

現在我想改變價值:

iMan2.BodyX1 = 50; 

,但我得到這個錯誤:

Error 1 Invalid token '=' in class, struct, or interface member declaration C:\vc\ComputerGraphicsExercises\ComputerGraphicsExercises\Main.cs 17 22 ComputerGraphicsExercises

我的錯誤是什麼?

+0

http://stackoverflow.com/questions/3766660/need-help-with-this-error-invalid-token-in-class-struct-or-interface-mem – Hayden 2015-01-04 14:08:13

+1

可能這[msdn]( http://msdn.microsoft.com/en-us/library/bb384062.aspx)幫助 – Grundy 2015-01-04 14:10:22

回答

1

您可以爲任何構造函數或方法外的類實例的字段賦值。這就是爲什麼

+0

謝謝,但我需要這個變量是全球性的,你還推薦我創建實例。這是一個Windows窗體項目。 – MostafaMohammadi 2015-01-04 14:11:37

+1

變量必須在方法,構造函數或類中定義。例如,您可以創建一個將此變量作爲靜態屬性的類,然後在全局範圍內對其進行訪問。請記住,擁有全局變量不是一個很好的做法,特別是對於較大的代碼庫。 – Balanikas 2015-01-04 14:15:02

相關問題