C#如何实现VB.net中的switch函数那种根据变量的值返回不同的结果 为什么非要写成一行呢?如果strBit的值可能有几十个,我觉得写成几十行更明了好看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果strBit的值有几十个 我肯定分开写 不然 一行看着多类 哎,看来,c#中没有和vb.net对应的switch函数c#中的switch case语句对应的是vb.net中的select case 找到答案,还是微软新闻组的高手多,[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")); } }} 如何在listview上添加一个button 100求助!如何使得Button上的文字垂直显示!在线等!!! 在父窗体中,打开多个子窗体的问题 ??? DataGrid的问题 如何动态的创建Timer控件 一个用c#网络编程 征求数据库通信组件 想请教一下,大家现在一般用什么语言,什么构架,什么工具来开发eHR ? ImagesList我的漂亮ICON为什么加不进去? 不好意思还是有关结构的问题 关闭窗口用Close()好还是Dispose()好? 高分求救!!!关于Remoting,如何调用两个远程对象
我肯定分开写 不然 一行看着多类
c#中的switch case语句对应的是vb.net中的select case
用来模拟。简单的重构了一下,如果你实在找不到更好的答案,看看
这个能不能用。
# 其实个人既不喜欢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")); }
}
}