MBaas
MBaas

Reputation: 7530

VB.net: CInt returning -1 on a (seemingly valid) string

I have a strange problem where CInt (as well as Convert.ToInt32 or even Val) return -1 for a string "500". Using Gurock's SmartInspect for logging, I've checked all values at runtime (SiAuto... logs something, I'm posting the log below the code). Would appreciate some hlp :)

Thanks

Michael

Si.Auto.Main.LogString("QueryString.q1",GetQueryStringParam("q1","0"))
' is it really "500" as it seems?
SiAuto.Main.LogBool("Is q1 = 500 in query-string?","500"=GetQueryStringParam("q1","0"))
q1 = CInt(GetQueryStringParam("q1","0"))
'don't worry abt that q1a-bit, the first string is just the title...
SiAuto.Main.LogInt("q1a",q1)

q1 = Convert.ToInt32(GetQueryStringParam("q1","0"))
SiAuto.Main.LogInt("q1b",q1)

q1 = val(GetQueryStringParam("q1","0"))
SiAuto.Main.LogInt("q1c",q1)

And here's the log:

QueryString.q1 = "500"
Is q1 = 500 in query-string? = True
q1a = -1
q1b = -1
q1c = -1

Upvotes: 0

Views: 978

Answers (2)

MBaas
MBaas

Reputation: 7530

Oh, too bad, sorry guys, I found the fault: someone had declared q1 as Boolean :(

Upvotes: 0

Adam Robinson
Adam Robinson

Reputation: 185663

Rather than making repeated calls to GetQueryStringParam() (what are you using, by the way?), store this value in a local string variable. This will make both your code and testing more reliable by eliminating the possibility that your function is behaving in a nondeterministic manner.

In any event, look into int.Parse() or int.TryParse() for your conversions as well, though what you have appears that it SHOULD work.

Edit

If none of that works, my only suggestion would be--and yes, this may actually do something--running the code under another account on your computer, or on another computer altogether. If your windows profile is corrupted you can have issues converting formattable data types to a from a string.

Upvotes: 4

Related Questions