【发布时间】: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