为什么非要写成一行呢?如果strBit的值可能有几十个,我觉得写成几十行更明了好看

解决方案 »

  1.   

    如果strBit的值有几十个 
    我肯定分开写  不然 一行看着多类
      

  2.   

    哎,看来,c#中没有和vb.net对应的switch函数
    c#中的switch case语句对应的是vb.net中的select case
      

  3.   

    找到答案,还是微软新闻组的高手多,[email protected]给出了解决办法.  我也不知道是不是有很直接的好办法。但是自己曾经做过一个函数
    用来模拟。简单的重构了一下,如果你实在找不到更好的答案,看看
    这个能不能用。
      # 其实个人既不喜欢Switch函数的式样,也不提倡使用这样的构成。
      # 只是有的时候真的是蛮便利。:P  因为我的代码都是用其它语言做的注释,所以我就都给删掉了。
      并且Switch函数的式样与VB的并不相同。如没有参数和不存在符合
    条件的情况下,我都throw了一个异常。理由是我确信我不会书写违反
    这个规则的代码。如果你在使用上感觉有什么不方便,去掉也无妨?  另外,我还有模拟了Oracle中的Decode函数,不管用不用,反正塞
    进去。:P// 类的定义:
    using System;namespace StudySwitch
    {
     public class Switch
     {
      public static object SwitchVB(params object[] VarExpr)
      {
       int len = VarExpr.Length;   if ( len <= 0 )
       {
        throw ( new ArgumentException());
       }   if ( System.Math.IEEERemainder(len,2) != 0 )
       {
        throw ( new ArgumentException());
       }   for ( int i = 0; i < len ; i += 2 )
       {
        if ( VarExpr[i].Equals(true) )
        {
         return VarExpr[i+1];
        }
       }   throw ( new ArgumentException());  }  public static object DecodeOracle(params object[] VarExpr)
      {
       int len = VarExpr.Length;
       bool blnIsDefault = false;   if ( len <= 2 )
       {
        throw ( new ArgumentException());
       }
       object target = VarExpr[0];   if ( System.Math.IEEERemainder(len,2) == 0 )
       {
        blnIsDefault = true;
       }   for ( int i = 1; i < len ; i += 2 )
       {
        if ( target.Equals(VarExpr[i]) )
        {
         return VarExpr[i+1];
        }
       }   if ( blnIsDefault == true )
       {
        return VarExpr[len-1];
       }
       else
       {
        throw ( new ArgumentException());
       }
      }
     }
    }// 使用方法:
    using System;
    using StudySwitch;namespace DefaultNamespace
    {
     class MainClass
     {
      public static void Main(string[] args)
      {
       string strTest = "2";
       System.Console.WriteLine(
        StudySwitch.Switch.SwitchVB(
                                      strTest == "0","hoho",
                                      strTest == "1","heihei",
                                      strTest == "2" ,"hehe"));   strTest = "2";
       System.Console.WriteLine(
        StudySwitch.Switch.DecodeOracle(
                                      strTest,"0","hoho","1","heihei","2","hehe","wawa"));  }
     }
    }