Val(x)

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
hi
What's the .NET version of this legacy vb command?
Val(TextBox1.Text) ?
It should be something like TextBox1.Text.Val ...???!!!
 
If I remember correctly, Val() just took erouneous string data out of a value that was supposed to be a number?

So the way to do this in .NET is

C#:
string value = "1234";
int myInt;
myInt = int.Parse(value);

Visual Basic:
dim value as String = "1234"
dim myInt as Integer
myInt = Integer.Parse(value)
 
I'm not sure of the exact workings of the Val() function (does it throw an error in invalid values? does it ignore extraneous text?) but I am pretty sure that it can parse floating point numbers, so it is worth mentioning that each numeric type has a Parse and TryParse method (TryParse is only available on the Double type in version 1.x) so you should use the appropriate type. In other words, in addition to Integer.Parse there is Single.Parse and Double.Parse (all of which throw an error on an invalid value) and then there are corresponding TryParse methods that won't throw exceptions for invalid values.
 
I do not know the inner workings of Val(); however, I believe that a string of "a100" will evaluate to 100. I forgot to mention the other numeric data types that have the Parse() method. As marble_eater said, TryParse() returns a boolean upon a successful Parse of the data, telling you it will be safe to use the Parse() method. However, TryParse() is only available in Framework v2.0, with the exception, of the Double, like marble_eater said.
 
Thank you all, so let me explain what am I exactly going to do in .NET 2.0!
I am getting a number from user in a simple text box.
It should be between 1 and 10.
So I was using:

If Val(Text1.Text) < 1 And Val(Text1.Text) > 10 Then...

Still should TryParse in .NET?
 
Dim MyRes As Boolean, MyInt As Integer = Integer.TryParse(TextBox1.Text, MyRes)
---
Thank you all for your help:)
But the problem is that somewhere else in a Timer_Tick event I am changing the label of a text box by decreasing its number:
Visual Basic:
    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
        FourthLabel.Text = Val(FourthLabel.Text) - 1
    End Sub
For this case, what should I do?
I mean how should I change the label via new TryParse function?
It will become a little bit hard!
 
Last edited:
Try this:
Visual Basic:
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
    Dim temp As Integer = 0
    If Integer.TryParse(FourthLabel.Text, temp) Then
        FourthLabel.Text = temp - 1
    End If
End Sub

You can add an "else" if you want to handle the case when the label's text is not numeric.

-ner
 
Back
Top