|
-
Jan 27th, 2007, 10:50 PM
#1
Thread Starter
Lively Member
[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.
-
Jan 27th, 2007, 11:12 PM
#2
Re: [2005] Val("Age: 37.1") returns 0
Val() does exactly what the MSDN help topic for that function say it does.
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.
-
Jan 28th, 2007, 12:31 AM
#3
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.
-
Jan 28th, 2007, 01:26 AM
#4
Hyperactive Member
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).
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
-
Jan 28th, 2007, 02:39 PM
#5
Thread Starter
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|