I have a project where I need to convert the current code in VBScript to C#. The part I’m stuck on is the VBScript code compares an alphanumeric medical procedure code to see if it’s between two other alphanumeric procedure codes. Here’s the VBScript code:
If ProcCode >= "A4000" And ProcCode <= "A7999" Then
[do stuff here]
End If
If I have a procedure code A5150, I need a C# code that does the same as the VBScript code. I’m not finding what I’m looking for on the internet. Any advice or URL of where I can find this solution?
3
I don’t have enough rep to just comment yet. @Mike Christensen’s comment is the most direct answer. Use the built in String.Compare() method. You can find the documentation here:
Microsoft String.Compare
Note that String.Compare(x, y) is null safe — stringVar1.Compare(stringVar2) is not null safe. But just because it will compile and run without error, you will want to include test cases to ensure you get expected output.
1
You can compare strings in C# the same way you did in VB. All languages, at least those known to me, compare strings the same way by default.
If you are using some kind of complex alphanumeric encoding, then this library function may be useful for you:
using System.Runtime.InteropServices;
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
static extern int StrCmpLogicalW(string psz1, string psz2);
var ProcCode = "A5150";
if (StrCmpLogicalW(ProcCode, "A4000") >= 0 &&
StrCmpLogicalW(ProcCode, "A7999") <= 0)
/*do stuff here*/;
I would define my own type, with implicit conversion to string, so you can easily assign it and overload >=
and <=
operator (and you can override more operators if needed).
To compare two codes I assumed such logic – remove A
at the beginning and compare numeric values.
Here’s sample implementation:
public class MyCode
{
private string _code;
private MyCode(string code) => _code = code;
public static implicit operator MyCode(string code)
{
return new MyCode(code);
}
public static bool operator <=(MyCode code1, MyCode code2)
{
return GetIntFromCode(code1) <= GetIntFromCode(code2);
}
public static bool operator >=(MyCode code1, MyCode code2)
{
return GetIntFromCode(code1) >= GetIntFromCode(code2);
}
private static int GetIntFromCode(MyCode code) => int.Parse(code._code.TrimStart('A'));
}
Example usage:
MyCode code = "A4999";
var r1 = "A4000" <= code; // true
var r2 = "A4000" >= code; // false
var r3 = "A5000" <= code; // false
var r4 = "A5000" >= code; // true
Update
Alternatively, you can define some extension methods on string
(with the same comparison logic), if you would like this approach:
public static class StringCodeComparer
{
public static bool IsLessOrEqualTo(this string x, string y)
{
return GetIntFromString(x).CompareTo(GetIntFromString(y)) <= 0;
}
public static bool IsGreaterOrEqualTo(this string x, string y)
{
return GetIntFromString(x).CompareTo(GetIntFromString(y)) >= 0;
}
public static int CompareCode(this string x, string y)
{
return GetIntFromString(x).CompareTo(GetIntFromString(y));
}
private static int GetIntFromString(string code) => int.Parse(code.TrimStart('A'));
}
And then usage would be:
var result1 = "A4000".IsGreaterOrEqualTo("A4999"); // false
var result2 = "A5000".IsGreaterOrEqualTo("A4999"); // true
var result3 = "A4000".IsLessOrEqualTo("A4999"); // true
var result4 = "A5000".IsLessOrEqualTo("A4999"); // false
2