【问题标题】:How to reference a class in another class C# Windows Forms如何在另一个类 C# Windows 窗体中引用一个类
【发布时间】:2018-02-04 01:23:36
【问题描述】:

我如何引用一个类?例如,我有一个 Price.cs 来计算项目费用,但我不能在我的 Price.cs 中使用任何 Windows 窗体控件,并且必须引用我的 Main.cs 才能使用属性和控件。对不起,如果我听起来很混乱。英语不是我的主要语言。

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;
    }

价格.cs:

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

namespace TicketingSystem
{
    class Price
    {

    }
}

【问题讨论】:

  • 无法清楚地理解您要做什么,但您可以在 Main.cs 中创建 class Price 的实例,例如 class Main{ private Price price; ... }
  • 你的应用程序中的依赖通常应该从你的实体开始(比如Price,它不依赖任何东西),继续所谓的“业务逻辑”(即在实体上运行的代码,无知任何 UI,仅依赖于实体),然后进入“演示文稿”(控制台应用程序、Windows 窗体、Web,取决于实体和业务逻辑)。因此,如果Price 需要了解 GUI,那么您就走错了方向。

标签: c# forms winforms visual-studio reference


【解决方案1】:

因此,我建议不要从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 属性,它将包含您想要在 Windows 窗体中的标签上显示的文本.

【讨论】:

    【解决方案2】:

    我的解决方案如下。

    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;
        }
    }
    

    }

    如果不清楚,我深表歉意。我仍然是一个初学者,只是想帮助别人。如果这可以帮助你欢呼!

    【讨论】:

      【解决方案3】:
      PricePlacement priceplacement = new PricePlacement();
      ...
      priceplacement.example = //write code here
      

      不用谷歌搜索就能记住这一点。如果我错了,请告诉我!

      【讨论】:

      • 我基本上按照你的建议做了,但改变了一些东西,所以我可以使用控件。
      【解决方案4】:

      如何在 Visual C# 中创建类和对象 https://support.microsoft.com/en-us/help/307368/how-to-create-classes-and-objects-in-visual-c

      例如

      BaseballTeam sf = new BaseballTeam("San Francisco Giants", "Candlestick Park");
      

      你的:

      Price p = new Price();
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多