【发布时间】:2009-01-29 03:35:25
【问题描述】:
我有一本字典,用来定义铜缆的额定值。我想使用这些值来计算满足特定连接等级所需的电缆数量。连接的大小、电缆类型和系统类型由名为 cb_amperage、cb_cable_size 和 cb_system_type 的两个 ComboBox 选择。等式运行后找到的答案将显示在名为 tb6_cable_qty 的文本框中。我欢迎任何和所有的 cmets 和建议。提前感谢您的帮助。
数学很简单:
decimal x, y, z;
x = decimal.Parse(cb_amperage.);
y = decimal.Parse();//<---- this value must come from the dictionary below
a = decimal.Parse();//<---- this value must also come from a dictionary below
z = (x / y) * a
tb6_cable_qty.Text = Math.Round(z,2).ToString();
void Cb_amperageSelectedIndexChanged(object sender, EventArgs e)
{
if (!String.!IsNullOrEmpty(cb_amperage) & !String.IsNullOrEmpty(cb_cable_size))
//not sure if the above is right but I think it coveys the idea
{
//function based on the values in the dictionary below
}
//Cable Dictionary 1 used for cable quantity calculation
Dictionary<string, int> cable_dictionary_1 = new Dictionary<string, int>();
cable_dictionary_1.Add ("#1", 130);
cable_dictionary_1.Add ("1/0", 150);
cable_dictionary_1.Add ("2/0", 175);
cable_dictionary_1.Add ("3/0", 200);
cable_dictionary_1.Add ("4/0", 230);
cable_dictionary_1.Add ("250", 255);
cable_dictionary_1.Add ("300", 285);
cable_dictionary_1.Add ("400", 355);
cable_dictionary_1.Add ("500", 380);
cable_dictionary_1.Add ("600", 720);
cable_dictionary_1.Add ("750", 475);
//System Type Dictionary used for cable quantity calculation
Dictionary<string, int> system_type_dictionary = new Dictionary<string, int>();
system_type_dictionary.Add ("3P 3W", 3);
system_type_dictionary.Add ("3P 4W", 4);
编辑 1: 毫米;请看下面的代码。我有一种感觉,我错过了一些如果我更有经验的话我会知道的东西。以下是我对您建议的解决方案的第一部分的实现。我得到一个错误。我认为这是因为这两个项目,字符串和字典不知道它们应该被链接。这是错误; *类、结构或接口成员声明 (CS1519) 中的标记 ',' 无效。看看你能看到什么。再次感谢。
//The following strings are used in the cable quantity calculation
private const string cblsize1 = "#1";
private const string cblsize2 = "1/0";
private const string cblsize3 = "2/0";
private const string cblsize4 = "3/0";
//Cable Dictionary 1 used for cable quantity calculation
Dictionary<string, int> cable_dictionary_1 = new Dictionary<string, int>();
cable_dictionary.Add(cblsize1, 130);//#1 Cable
cable_dictionary.Add(cblsize2, 150);//1/0 Cable
cable_dictionary.Add(cblsize3, 175);//2/0 Cable
cable_dictionary.Add(cblsize4, 200);//3/0 Cable
【问题讨论】:
-
如何使用字典在方程中提供值是简短的版本。
标签: c# dictionary combobox