2017-08-26 132 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属性。

相关问题