2016-02-24 24 views
-1

所以,我在這裏不知所措。有一個小項目可以開展工作。必須創建一個旅程類,然後創建一個Windows窗體應用程序,並使用我創建的類來使用表單來計算每加侖使用的英里數和每英里成本。需要使用創建的類來創建一個實例並在Form.cs文件中使用它

已經完成了類:

namespace TripCalculator 
{ 
class Trip 
{ 
    //Data members of class 
    private string destination; 
    private double distTrav; 
    private double totalCostGas; 
    private double numGallonsGas; 


    //Default Constructor 
    public Trip() 
    { 
    } 

    //Constructor with all parameters 
    public Trip(string tripDest, double milesTrav, double ttlPriceGas, n double numGalls) 
    { 
     destination = tripDest; 
     distTrav = milesTrav; 
     totalCostGas = ttlPriceGas; 
     numGallonsGas = numGalls; 

    } 

    //Propery for destination data field 
    public string Destination 
    { 
     set 
     { 
      destination = value; 
     } 
     get 
     { 
      return destination; 
     } 


    } 

    public double DistTrav 
    { 

     set 
     { 
      distTrav = value; 
     } 
     get 
     { 
      return distTrav; 
     } 
    } 

    public double TotalCostGas 
    { 

     set 
     { 
      totalCostGas = value; 
     } 
     get 
     { 
      return totalCostGas; 
     } 
    } 

    public double NumGallonsGas 
    { 

     set 
     { 
      numGallonsGas = value; 
     } 
     get 
     { 
      return numGallonsGas; 
     } 
    } 

    public double CalculateMPG() 
    { 
     return (distTrav/numGallonsGas); 
    } 

    public double CalculateCPM() 
    { 
     return (totalCostGas/numGallonsGas); 
    } 

    public override string ToString() 
    { 
     return CalculateMPG().ToString(); 

    } 

} 
} 

我希望能夠輸入目的地,距離,成本和氣體加侖的形式。然後我希望每英里的英里數和成本在文本框中返回給我。 這裏的form.cs

namespace TripCalculator 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void calcBtn_Click(object sender, EventArgs e) 
    { 
     string destination; 
     double distTrav; 
     double totalCostGas; 
     double numGallonsGas; 

     destBox.Focus(); 
     distBox.Focus(); 
     gasBox.Focus(); 
     galBox.Focus(); 

     Trip aTrip = new Trip (destination, distTrav, totalCostGas, numGallonsGas); 

     mpgTxt.Text = aTrip.CalculateMPG().ToString(); 
     cpmTxt.Text = aTrip.CalculateCPM().ToString(); 
     destBox.Enabled = false; 


    } 
} 
} 

即時得到4個錯誤說「未分配的局部變量‘目的地’的使用(除了如對上述其他3個變量)。它會啓動程序,但沒有返回時我在文本框中鍵入並單擊按鈕。我在做什麼錯?請幫助!謝謝。

回答

0
private void calcBtn_Click(object sender, EventArgs e) 
{ 
    string destination; // Give value for these 
    double distTrav; // Give value for these 
    double totalCostGas; // Give value for these 
    double numGallonsGas; // Give value for these 
} 

分配一定的價值,無論是從用戶輸入或硬編碼值測試

有點像下面的代碼,你從哪裏得到值假設你有這樣的文本框,你的Winform文本框。

string destination = DestinationTextBox.Text; 
double distTrav = double.Parse(DistTravTextBox.Text); 
double totalCostGas = double.Parse(TotalCostGasTextBox.Text); 
double numGallonsGas = double.Parse(NumGallonsGasTextBox.Text); 
+0

爲雙分配文本值設置文本框的值? – Vinothkumar

+1

感謝您的反饋!有效!瘋狂如何創造這些問題,你就這樣滑倒。我非常感激! –

0

您需要爲變量

 string destination = txtDestination.Text; 
     double distTrav = double.Parse(txtTrav.Text); 
     double totalCostGas = double.Parse(txtCostGas.Text); 
     double numGallonsGas = double.Parse(GallonsGas.Text); 
相關問題