Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    283
    Thanks
    0
    Thanked 0 Times in 0 Posts

    converting data (VB.NET)

    I would like to know when I convert numeric data from a textbox and store it into a variable, should I use cdec(txtPrice.text) or val(txtPrice.text). I notice that if I use cdec and the user types in a non-numeric entry, such as the letter A, the program crashes. Using Val does not have this problem.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: converting data (VB.NET)

    What is Val("A"), zero? It might be best to check the contents of the textbox (e.g., is it numeric) or use a control that validates input.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: converting data (VB.NET)

    Why are you using VB functions in VB.Net? If you have declared the string variable, then you need to turn the text value of the textbox into a string object: It isn't like VB where you have string variables. In .Net, variables are objects of some type.

    Dim strResult as String

    strResult = CType(Me.txtPrice.Text, String())

    What are you trying to accomplish? If the value in the textbox is numeric, are you trying to store it in a numeric variable or convert it? If it is not numeric but is supposed to be, use a Try Catch block to handle the error that results.

    <pre> Dim intResult As Integer
    Try
    intResult = CType(Me.txtPrice.Text, Integer)
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try</pre>

    Charlotte

  4. #4
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: converting data (VB.NET)

    If you want to convert between data types, especially strings to numbers, you should always check if the string is a valid number first. That's just good prgamming practice. If only we could have users who never make a typing error. Dream on Kevin.

    Using the Val function is not the panacea it sounds like. The Val function returns zero for anything it decides is an invalid number meaning that if zero is a valid entry then you're SOL. cDec and the other dedicated functions do not. They will throw an InvalidCastException if the string is not convertable.

    The unfortunate consequence of this is that a lot of code is written to validate input text before it can be converted into numbers. To get around this I have created a Numeric Text Box control for .NET that only allows numeric, decimal point and negative signs to be input. The applications that use it still have to validate ranges but the drudgery of checking for valid numbers, is it negative, is it a whole number etc is taken away from the programmer. I have attached the .NET solution for this. Feel free to use it, modify it, whatever you like.

    As for the comment on the reason to use VB functions for data conversion, these functions are as much a part of VB .NET as they are of VB6. The fact that strings, and just about everything else in .NET, are objects (I won't mention Interfaces), doesn't negate the need to use functions to convert them. There are no methods of the String class that allow conversion to other data types other than CharacterArrays.

    I hope this helps.

    Regards,
    Kevin Bell

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: converting data (VB.NET)

    <hr>doesn't negate the need to use functions to convert them<hr>
    I wasn't suggesting otherwise, Kevin. However, many functions are supported for backward compatibility and there is no guarantee that support will continue into eternity. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •