2017-08-26 136 views
-1

我如何引用類?例如,我有一個Price.cs來計算項目的支出,但我不能使用任何Windows在我Price.cs形成控制和必須引用我Main.cs爲了使用屬性和控制。對不起,如果我聽起來很混亂。英語不是我的主要語言。如何引用類在另一個類C#Windows窗體

Main.cs:

private float? PricePlacement() 
    { 
     if (serviceDesc.Text != "Multiple") 
     { 
      if (serviceDesc.Text == "Phone Repair") 
      { 
       textBlock_Price.Text = "$20.00 + Parts"; 
       return 20.00f; 
      } 
      if (serviceDesc.Text == "Virus Removal") 
      { 
       textBlock_Price.Text = "$10.00"; 
       return 10.00f; 
      } 
      if (serviceDesc.Text == "Hardware Repair/Installation") 
      { 
       textBlock_Price.Text = "$10.00"; 
       return 10.00f; 
      } 
      if (serviceDesc.Text == "Software Installation") 
      { 
       textBlock_Price.Text = "$5.00"; 
       return 5.00f; 
      } 
      textBlock_Price.Text = "$0.00"; 
      return 0f; 
     } 
     else if (serviceDesc.Text == "Multiple") 
     { 
      //TODO: Implement a function to check if an item on the itemList is checked or not 

      textBlock_Price.Text = "$-.--"; 
      return null; 
     } 

     return 0f; 
    } 

Price.cs:

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

namespace TicketingSystem 
{ 
    class Price 
    { 

    } 
} 
+0

不能清楚地理解你正在嘗試做什麼,但是你可以在Main.cs中創建一個'class Price'實例,如 'class Main {private price price; ... } – AbdullahRazzaki

+0

您的應用中的依賴關係通常應該以您的實體(比如'Price',它不依賴於任何東西)開始,繼續所謂的「業務邏輯」(即對實體進行操作的代碼,無知任何用戶界面,只依賴於實體),然後進入「演示」(一個控制檯應用程序,Windows窗體,網頁,取決於實體和業務邏輯)。所以,如果一個'Price'需要知道一個GUI,那麼你的方向是完全錯誤的。 – Groo

回答

0

我的解決辦法如下。

Price.cs我加using static TicketingSystem.TicketingSystem;在設計

我改變private System.Windows.Forms.ComboBox serviceDesc;

  public System.Windows.Forms.ComboBox serviceDesc;`` 

所以完全是我的新的代碼爲:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using TicketingSystem; 
using System.Windows.Forms; 
using static TicketingSystem.TicketingSystem; 

namespace TicketingSystem 
{ 
class Price 
{ 
    TicketingSystem ticketingSystem = new TicketingSystem(); 
    public ComboBox serviceDesc = new ComboBox(); 
    public Label textBlock_Price = new Label(); 

    public float? PricePlacement() 
    { 
     if (ticketingSystem.serviceDesc.Text != "Multiple") 
     { 
      if (ticketingSystem.serviceDesc.Text == "Phone Repair") 
      { 
       textBlock_Price.Text = "$20.00 + Parts"; 
       return 20.00f; 
      } 
      if (ticketingSystem.serviceDesc.Text == "Virus Removal") 
      { 
       textBlock_Price.Text = "$10.00"; 
       return 10.00f; 
      } 
      if (ticketingSystem.serviceDesc.Text == "Hardware Repair/Installation") 
      { 
       textBlock_Price.Text = "$10.00"; 
       return 10.00f; 
      } 
      if (ticketingSystem.serviceDesc.Text == "Software Installation") 
      { 
       textBlock_Price.Text = "$5.00"; 
       return 5.00f; 
      } 
      textBlock_Price.Text = "$0.00"; 
      return 0f; 
     } 
     else if (serviceDesc.Text == "Multiple") 
     { 
      //TODO: Implement a function to check if an item on the itemList is checked or not 

      textBlock_Price.Text = "$-.--"; 
      return null; 
     } 

     return 0f; 
    } 
} 

}

我道歉,如果這是不明確的。我仍然是初學者,只是想幫助別人。如果這可以幫助你歡呼!

0
PricePlacement priceplacement = new PricePlacement(); 
... 
priceplacement.example = //write code here 

記住,把我的頭頂部沒有谷歌搜索。如果我錯了,讓我知道!

+0

我基本上做了你的建議,但改變了一些東西,所以我可以使用控件。 – Yuvraj

0

因此而不是從PricePlacement()方法我建議返回一個對象,可容納的價格和你的控件標籤文本都返回float?。您可以通過創建一個類如實現這一目標:

public class PriceAndLabel 
{ 
    public string Label { get; set; } 
    public float? Price { get;set; } 
} 

和修改功能如下返回PriceAndLabel對象,而不引用您的主(形式):

private PriceAndLabel PricePlacement(string serviceDescriptionText) 
    { 
     PriceAndLabel priceAndLabel = new PriceAndLabel(); 
     priceAndLabel.Price = 0f; 

     if (serviceDescText == "Phone Repair") 
     { 
      priceAndLabel.Label = "$20.00 + Parts"; 
      priceAndLabel.Price = 20.00f; 
     } 
     else if (serviceDesc.Text == "Virus Removal") 
     { 
      priceAndLabel.Label = "$10.00"; 
      priceAndLabel.Price = 10.00f; 
     } 
     else if (serviceDesc.Text == "Hardware Repair/Installation") 
     { 
      priceAndLabel.Label = "$10.00"; 
      priceAndLabel.Price = 10.00f; 
     } 
     else if (serviceDesc.Text == "Software Installation") 
     { 
      priceAndLabel.Label = "$5.00"; 
      priceAndLabel.Price = 5.00f; 
     } 
     else if (serviceDesc.Text == "Multiple") 
     { 
      priceAndLabel.Label = "$-.--"; 
      priceAndLabel.Price = null; 
     } 
     else 
     { 
      priceAndLabel.Label = "$0.00"; 
      priceAndLabel.Price = 0f; 
     } 

     return priceAndLabel; 
    } 

在窗體本身你可以調用以類似的方式方法,它應該回到你的PriceAndLabel對象:

PriceAndLabel priceAndLabel = PricePlacement(serviceDesc.Text);

priceAndLabel變量將有一個Price屬性,它是一個可空浮(這意味着它可能沒有任何價值),並且將要顯示在窗口的形式在標籤上的文本Label屬性。

相關問題