Visual Basic 2005 Numerical Functions Support

Discussion in 'Software' started by Red_X_, Feb 4, 2007.

  1. Red_X_

    Red_X_ Private E-2

    I'm working with VB 2005. I'm rather new to the visual basic department, i'm not much of a fan of it either.

    Anyway, I have a very simple program. With 2 buttons, one labeled "btnCalculate" and one labeled "btnExit", and those titles are rather odvious as to what they do.

    I have 3 categories:

    Initial Quality:
    <insert text field>(txtQuality)

    Amount Sold
    <insert text field>(txtSold)

    Amount Purchased
    <insert text filed here>(txtPurchased)

    Current Quality:
    <insert label label>(lblinStock)

    What I want, is to calculate the current quanitity by subtracting the quantity sold from the inital quantity, and then adding the quantity purchased to the result. Then display it the current quantity in my lblInstock" control.

    I want to use the functions:
    CDbl
    CInt
    CSng
    CStr

    o.o But i'm unsure of how to use them, I've googled it but still don't know how to do it.

    Any support would be much appreciated.

    Thank You,

    Red_X_
     
  2. Kodo

    Kodo SNATCHSQUATCH

    Dim origAmnt as Integer=Integer.Parse(txtQuantity.Text)
    Dim soldAmnt as Integer=Integer.Parse(txtSold.Text)
    Dim purchAmnt as Integer=Integer.Parse(txtPurchased.Text)

    Dim currentQuantity as Double= (origAmnt-soldAmnt)+purchAmnt

    lblInstock=currentQuantity.ToString()


    The following
    CDbl
    Cint
    CSng
    CStr

    are all deprecated functions from VB6. In other words. don't use them as it's goes against the way of doing things in .NET
     
  3. Red_X_

    Red_X_ Private E-2

    Hmm. I was told it would making things easier to read. As they are simple functions.

    But if I was to use those, how would they be used? If its not to much trouble.
     
  4. Kodo

    Kodo SNATCHSQUATCH

    don't use them, they are deprecated and anyone who tells you to use them doesn't understand .NET

    BUT, you would use them like CDbl([value to convert]) or CStr([value to convert]) etc etc.
     
  5. matt.chugg

    matt.chugg MajorGeek

    lblInstock.text=currentQuantity.ToString() ;)
     
  6. Kodo

    Kodo SNATCHSQUATCH

    crap.. I always do that. ;)
     
  7. icarus8

    icarus8 Private E-2

    Remarks from the Microsoft help files under Type Conversion Functions

    As a rule, you should use the Visual Basic type conversion functions (like CStr, CInt, etc.) in preference to the .NET Framework methods such as ToString(), either on the Convert class or on an individual type structure or class. The Visual Basic functions are designed for optimal interaction with Visual Basic code, and they also make your source code shorter and easier to read. In addition, the .NET Framework conversion methods do not always produce the same results as the Visual Basic functions, for example when converting Boolean to Integer.
     
  8. Kodo

    Kodo SNATCHSQUATCH

    They are legacy function names that are "shorthand" for ctype; Cstr essentially calls Ctype(). MS Docs also suggest that type names should not be included in function names, variable names etc. They broke their own rules with this because people didn't want to type Ctype(variable, object) or god forbid object.tostring(). While there are vb functions that do have some optimizations, these are not among them.

    Casting a string to an int should be done
    Dim i as integer=0
    Dim x as string="*#"
    Dim successOrFailure as Boolean=integer.TryParse(x, i)

    using Cint(x) will result in an invalid cast. So much for validation and while you're worried about your code "looking" clean, it won't work and you're left wondering "WTF".

    So I re-iterate. Use the .NET methods please and steer clear of the easy button.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds