2011-01-20 88 views
0

誰猜猜誰回來! 程序運行(horrah),但現在變量不保存該值。 的主要形式有:C#(再次,再次?)變量不保存

Airplane plane = new Airplane();  

private void btnAccel_Click(object sender, EventArgs e) 
{ 
    lblStatus.Text = plane.speed.ToString(); 
    plane.speed = double.Parse(txtSpeed.Text); 
    plane.Accelerate(); 
    lblStatus.Text = plane.speed.ToString();   

} 

而且從類飛機:

class Airplane 
{ 
    private string name{get; set;} 
    private Position PlanePosition; 
    private static int numberCreated; 

    public Airplane() 
    { 
     this.PlanePosition = new Position(); 
    } 

    public void Accelerate() 
    { 
     // increase the speed of the airplane  
     if (PlanePosition.speed < Position.MAX_SPEED) 
     { 
      PlanePosition.speed +=1; // or speed += 1; 
     }//end of if 
     numberCreated++; // increment the numberCreated each time an Airplane object is created  
    } 

「位置」 是另一個類:

class Position 
{ 
    internal int x_coordinate; 
    internal int y_coordinate; 
    internal double speed; 
    internal int direction; 
    internal const int MAX_SPEED = 50; 

    public Position() 
    {     
    } 

    public string displayPosition() 
    { 
     return "okay"; 
    } 
} 

,由於某種原因,從文本框的變量主窗體進入「速度」變量,但飛機類中的速度變量沒有該變量。

+7

什麼是`plane.speed`?我看到你指定了它,但我沒有看到它的存儲位置或使用位置。 – 2011-01-20 16:53:11

+0

我在'飛機'上看不到'速度'屬性或者字段' – 2011-01-20 16:53:56

回答

0

一般來說,在我們看看你的問題之前,先看看你的對象模型。速度真的是飛機位置的一個屬性嗎?真?

你需要首先解決這個問題的原因是因爲,保持原樣,你將最終創建一個不可維護的代碼,編碼器必須知道具體存儲的位置。

此外,最高速度不應該固定爲一個常數值 - 某個地方最終會建立一個更快的飛機並毀掉你的整個一天。

我建議,在繼續之前,你會考慮看這些問題。如果你在課堂上和其他學生一起工作,同行評議可能是你想要考慮的。

至於問題本身,你是否已經通過調試程序遍歷代碼?一次只讀代碼一行可以經常揭示你寫的東西和你打算寫的東西之間的區別...

在btn_Accel_Click中粘貼一個斷點 - 它甚至是否正在觸發?我知道你認爲我很諷刺,但我真的不是 - 我試圖幫助你。這正是我在嘗試自己分析一切之前陷入困境之前所要做的。

Martin。

1

從文本框中設置速度後,您正在調用加速。因此自動將速度提高1 ...那不是問題嗎?如果不是,你能否爲那些不熟悉你在做什麼的人提供更多的信息?

2

要麼你的例子不完整,要麼你的代碼神奇地編譯。我猜你需要實現一個屬性才能訪問​​實例中包含的速度。

class Airplane 
{ 
    private string name{get; set;} 
    private Position PlanePosition; 
    private static int numberCreated; 

    public double speed 
    { 
     get { PlanePosition.Speed = value; } 
     set { return PlanePosition.speed; } 
    } 

    public Airplane() 
    { 
     this.PlanePosition = new Position(); 
    } 

    public void Accelerate() 
    { 
     // increase the speed of the airplane  
     if (PlanePosition.speed < Position.MAX_SPEED) 
     { 
      PlanePosition.speed +=1; // or speed += 1; 
     }//end of if 
     numberCreated++; // increment the numberCreated each time an Airplane object is created  
    } 
} 
0

您的飛機級沒有速度屬性。它將如何工作?

或者做個像格雷格Buehled aswered,或者你可以嘗試這樣的事:

設置你的PlanePositon公共

class Airplane 
{ 
    private string name{get; set;} 
    public Position PlanePosition; 
    private static int numberCreated; 

    public Airplane() 
    { 
     this.PlanePosition = new Position(); 
    } 

    public void Accelerate() 
    { 
     // increase the speed of the airplane  
     if (PlanePosition.speed < Position.MAX_SPEED) 
     { 
      PlanePosition.speed +=1; // or speed += 1; 
     }//end of if 
     numberCreated++; // increment the numberCreated each time an Airplane object is created  
    } 

,然後你改變它在你的btnAccel_Click事件。

private void btnAccel_Click(object sender, EventArgs e) 
{ 
    lblStatus.Text = plane.speed.ToString(); 
    plane.PlanePosition.speed = double.Parse(txtSpeed.Text); 
    plane.Accelerate(); 
    lblStatus.Text = plane.PlanePosition.speed.ToString();   

} 

這不是最好的,但也是一種方式。