[RESOLVED] [2005] Val("Age: 37.1") returns 0
My code:
VB Code:
label1.text="Age: 37.1"
msgbox Val(label1.text)
The messagebox displays 0. Isn't val supposed to extract all numbers and strip the text? I was wanting it to return 37.1
Thanks,
Corey
This is VS2005 .NET 2005... the program is written for PocketPC (CF 2) but I think this applies to any VB code.
Re: [2005] Val("Age: 37.1") returns 0
Val() does exactly what the MSDN help topic for that function say it does.
Quote:
The Val function stops reading the string at the first character it cannot recognize as part of a number.
Your string starts with "A" which is not recognised as part of a number, so nothing gets read. If your string was something like "37.1 years" then Val() would be useful.
I'm not sure where you're getting this string from but you should endeavour to keep your, which is the number, and your labels separate. Having a value in a string with some data describing that value is a bad situation and should be avoided if at all possible.
Re: [2005] Val("Age: 37.1") returns 0
Just a note, using VB6.0 I've also encountered a problem when the numeric value contains comma like 1,000 for example, though I am not sure if it is still applicable in .Net.
Re: [2005] Val("Age: 37.1") returns 0
Val, as already stated, will not work in that case. If you're just trying to find the value for this one instance, you could just do:
VB Code:
Label1.Text = "Age: 37.1"
MsgBox Trim(Mid(Label1.Text, InStr(Label1.Text, ":") + 1))
That will only work if the only text after "Age: " is what you are looking for. In other words, if you have "Age: 37.1, Name: John Doe" then the code will return "37.1, Name: John Doe" unless you set some sort of length boundaries to it. It would all depend on the complete text you are trying to get this information from. But if all you're trying to parse is "Age: 37.1" or some random number, then the above code will do just fine(although I haven't tested it, it should still work fine).
Re: [2005] Val("Age: 37.1") returns 0
Thank you for all of your suggestions. I was trying to take the easy way out, but I just seperate the label into two parts, the text and the value. When I originally created the label, I didn't think I would need to reference it later. Thank you for all of your help!
-Corey