下面的条件判断语句,相比大家编写代码的时候都碰见过。
/// <summary> /// 根据规则设置物流号 /// </summary> /// <param name="expressCode"></param> private void SetExpressCode(string expressCode) { string traceExpressNo = this.txtExpressNo.Text.Trim(); if (expressCode == "CNSFEDEX" || expressCode == "HKFEDEX" || expressCode == "SHSFEDEX" || expressCode == "CNFEDEX" || expressCode == "CNSFEDEXZY") { if (traceExpressNo.Length > 12) { traceExpressNo = traceExpressNo.Substring(0, 12); } } else if (expressCode == "UPSNR") { if (traceExpressNo.Length > 26) { traceExpressNo = traceExpressNo.Substring(8, 26); } } else if (expressCode == "DHLRM") { if (traceExpressNo.Length > 22) { traceExpressNo = traceExpressNo.Substring(8, 22); } } else if (expressCode == "GBEXP") { if (traceExpressNo.Length == 19) { traceExpressNo = traceExpressNo.Substring(1, 18); } } else if (expressCode == "GBEXP") { if (traceExpressNo.Length == 19) { traceExpressNo = traceExpressNo.Substring(1, 18); } } else if (expressCode == "DEEXP") { if (traceExpressNo.Length == 28) { traceExpressNo = traceExpressNo.Substring(8, 14); } } txtExpressNo.Text = traceExpressNo; }
用switch
/// <summary> /// 根据规则设置物流号 /// </summary> /// <param name="expressCode"></param> private void SetExpressCode(string expressCode) { string traceExpressNo = this.txtExpressNo.Text.Trim(); int maxLength = 0; int fixedLength = 0; int beginIndex = 0; int endIndex = 0; switch(expressCode){ case "CNSFEDEX": case "HKFEDEX": case "SHSFEDEX": case "CNFEDEX": case "CNSFEDEXZY": maxLength = 12; beginIndex = 0; endIndex = 12; break; case "UPSNR": maxLength = 26; beginIndex = 8; endIndex = 26; break; case "DHLRM": maxLength = 22; beginIndex = 8; endIndex = 22; break; case "GBEXP": fixedLength = 19; beginIndex = 1; endIndex = 18; break; case "DEEXP": fixedLength = 28; beginIndex = 8; endIndex = 14; break; default: //TODO } int length = traceExpressNo.Length; if((maxLength > 0 && length > maxLength) || (fixedLength > 0 && length == fixedLength)){ traceExpressNo = traceExpressNo.Substring(beginIndex, endIndex); } txtExpressNo.Text = traceExpressNo;
这方法不错,至少比原始的写法看着简洁多了。
switch 是可以的 。在此基础上 ,如果数字都是固定的话 可以用 model + 枚举 容易修改和拓展
string traceExpressNo = this.txtExpressNo.Text.Trim(); int beginIndex = 0; int length = traceExpressNo.Length; switch(expressCode){ case "CNSFEDEX": case "HKFEDEX": case "SHSFEDEX": case "CNFEDEX": case "CNSFEDEXZY": if (traceExpressNo.Length > 12) { beginIndex = 0; length = 12; } break; case "UPSNR": //类推 break; case "DHLRM": //类推 break; case "GBEXP": //类推 break; case "DEEXP": //类推 break; } txtExpressNo.Text = traceExpressNo.Substring(beginIndex, length);
这种是比较少的情况,如果你code,长度这些都几百个用switch,model+枚举都不合适
建议用状态列表,二纬表配置每种code对应其截取规则,然后写一个跟据code去取规则的方法就好了。
二纬表可以使用配置文件维护,也可以使用数据库维护
使用委托和字典
public delegate string StringHandler(); public class Class1 { public static void SetExpressCode(string expressCode) { string traceExpressNo = "abcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopq"; var dic = new Dictionary<string, StringHandler>(); dic.Add("UPSNR", () => { return GetString(traceExpressNo, 26, 8, 26); }); dic.Add("DHLRM", () => { return GetString(traceExpressNo, 22, 8, 22); }); var str = dic[expressCode](); } static string GetString(string str, int length, int index, int length2) { if (str.Length > length) { str = str.Substring(index, length2); } return str; } }
string traceExpressNo = "abcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopq";
兄才,这个是什么意思啊?为什么是A-Q
@422159763:
随便写的个字符串,为了让编译通过