我有一本字典,我正在使用它來定義銅纜的額定值。我想使用這些值來計算需要多少電纜才能達到某個連接評級。連接的大小,電纜類型和系統類型由名爲cb_amperage,cb_cable_size和cb_system_type的兩個ComboBox選擇。等式運行後找到的答案將顯示在名爲tb6_cable_qty的文本框中。我歡迎任何和所有意見和建議。預先感謝您的幫助。C#字典應用:通過選擇鍵應用值
數學很簡單:
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);
EDIT 1: MMR;請看下面的代碼。我有一種感覺,我錯過了一些我會知道的事情,如果我稍微有些過期。以下是我對您建議的解決方案的第一部分的實施。我收到一個錯誤。我認爲這是因爲這兩個項目,字符串和字典不知道它們應該被鏈接。這是錯誤; *類,結構或接口成員聲明(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
這裏有什麼問題? – Gishu 2009-01-29 05:22:41
如何使用字典提供方程中的值是簡短版本。 – 2009-01-29 05:27:03