Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 

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

 

dim value as String = "1234"
dim myInt as Integer
myInt = Integer.Parse(value)

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]
Posted
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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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?

Posted (edited)

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:

   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!

Edited by ADO DOT NET
  • *Experts*
Posted

Try this:

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...