Converting the Select Case with To and Is to c# problems
I am convertng vb.net to c# 2010 as my job, and none of the automatic tools I have can succeed completely. In special example, this case:
'searchString is a string paramter from a long method
Select Case searchString
Case "paid"
'Do something long here
Case "oaaaaa" To "ozzzzzz", "maaaaaa" To "mzzzzzz"
'Do other long code
Case Else
'other long code
End Select
I am mostly java developer before this, so not great with c# and none with vb.net. I do not understand the "oaaaa to ...." part and this part is not converting. Can you please point me to rig开发者_Python百科ht place to find the c# version of this?
There isn't a direct equivalent in C# but you can easily achieve the same semantics (with more readable code!) using the following:
if(searchString == "paid") {
// do something here
}
else if(
searchString.IsInRange("oaaaaa", "ozzzzzz") ||
searchString.IsInRange("maaaaa", "mzzzzzz")
) {
// do other long code
}
else {
// other long code
}
public static class StringExtensions {
public static bool IsInRange(this string s, string lower, string upper) {
if(String.Compare(lower, upper) > 0) {
throw new InvalidOperationException();
}
return String.Compare(s, lower) >= 0 && String.Compare(s, upper) <= 0
}
There's no direct C# equivalent of the Case "xxx" To "yyy"
syntax. I suppose the closest translation will probably be an if
/else if
/else
stack:
if (seachString == "paid")
{
// do something long here
}
else if (((searchString.CompareTo("oaaaaaa") >= 0) && (searchString.CompareTo("ozzzzzz") <= 0))
|| ((searchString.CompareTo("maaaaaa") >= 0) && (searchString.CompareTo("mzzzzzz") <= 0)))
{
// do other long code
}
else
{
// other long code
}
C# doesn't seem to have the concept of Case ... To. See http://msdn.microsoft.com/en-us/library/cy37t14y(VS.80).aspx. The C# example says "This language is not supported".
bitwise has the answer, but here is the translated code (it's just like javascript):
switch (searchString){
case: "paid"
'Do something long here
break;
case: "oaaaaa" To "ozzzzzz", "maaaaaa" To "mzzzzzz"
'Do other long code
break;
default:
'other long code
break;
}
精彩评论