ADO DOT NET Posted January 31, 2007 Posted January 31, 2007 hi What's the .NET version of this legacy vb command? Val(TextBox1.Text) ? It should be something like TextBox1.Text.Val ...???!!! Quote
Nate Bross Posted January 31, 2007 Posted January 31, 2007 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) Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Leaders snarfblam Posted January 31, 2007 Leaders Posted January 31, 2007 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. Quote [sIGPIC]e[/sIGPIC]
Nate Bross Posted February 1, 2007 Posted February 1, 2007 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
ADO DOT NET Posted February 1, 2007 Author Posted February 1, 2007 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? Quote
Administrators PlausiblyDamp Posted February 1, 2007 Administrators Posted February 1, 2007 Yep - if you are using .Net 2 then TryParse is the way to go, if you are using .Net 1 then Parse inside a Try ... Catch block will do the trick. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 2, 2007 Author Posted February 2, 2007 (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 February 2, 2007 by ADO DOT NET Quote
*Experts* Nerseus Posted February 2, 2007 *Experts* Posted February 2, 2007 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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.