2

i'm converting some vbscript from an asp app and ran across a line in the form of

If sCode > "" Then

where I expect sCode to contain a string. i know enough vbscript to plod through it but i'm not sure of the behavior of some quirkier statements. c# will not accept that as a valid condition check. what is an equivalent c# statement?

edit: extra thanks if someone can provide some documentation/reference for the vbscript behavior.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
lincolnk
  • 11,218
  • 4
  • 40
  • 61

5 Answers5

2

Since in C# a string can also be NULL, I would use the following:

if(!string.IsNullOrEmpty(sCode))
    //do something
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
1

I'm not a vbscript expert, but my hunch is vbscript overloaded > with strings to compare them ordinally. So if that is the case, then in C# sCode.CompareTo(string.Empty) will give you what you need, -1 if sCode less than the empty string (which is not possible in this case), 0 if they are equal, and 1 if sCode comes after.

In this particular case you can just check if sCode is the empty string though.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • since the original code was checking against an empty string, the c# check can be reduced to RedFilter's example. if it were non-empty then i think `CompareTo` would be appropriate, so +1. – lincolnk Aug 25 '10 at 14:44
0

I would just simply do !=, which appears to be the intent of the code:

if(sCode != String.Empty)
  Do();
Corey Coogan
  • 1,569
  • 2
  • 17
  • 31
0

In your particular case you simply compare to 'string.Empty' but the more generic answer is that it's (usually) a case insensitive alphanumeric comparison. E.g "ababa" < "z1asdf" is true. To represent that in C# you could do:

'string.Compare(A,B) < 0' which is equivallent of 'A

(usually) because it can be specified

Rune FS
  • 21,497
  • 7
  • 62
  • 96
0

Use the "<>" (not equal to) operator, like so:

dim string
string = "hello"
if (string <> "") then
    WScript.Echo "We're Ok" & VbCrLf
else
    WScript.Echo "Empty String" & VbCrLf
End if
gWaldo
  • 442
  • 1
  • 7
  • 23