PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Why convert from String to Double?-VBForums
Results 1 to 9 of 9

Thread: Why convert from String to Double?

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Why convert from String to Double?

    Hello All,

    I am, at a minimum, new to VB but it has really gotten ahold of me. I am absorbing all aspects quite well accept one.

    Please keep in mind that as far as programming goes I am infantile at best. I do however understand why you would need to define what your variables are going to be according to what will be displayed or amount of memory needed to contain your information, BUT for the life of me I can not understand why my VB6 2005 edition requires me to convert this line of code from a string to a double. Any help will be greatly appreciated.

    VB Code:
    1. StrNumber1 = CStr (txtNumber1.Text)
    2. StrNumber2 = CStr(txtNumber2.Text)
    3. StrYourAnswer = CStr(CDbl(StrNumber1) * CDbl(StrNumber2))

    To pinpoint my question. I am converting to a string which, correct me if I'm wrong can be any number or letter, to a double. The way you see it formatted here is the way that VB made me correct it to thrawt any exceptions. Once again any help would be great at this point. I understand what changes are being made, I just don't understand thru all the text I read why I am making them.

  2. #2
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: Why convert from String to Double?

    Quote Originally Posted by Abrium
    Hello All,

    I am, at a minimum, new to VB but it has really gotten ahold of me. I am absorbing all aspects quite well accept one.

    Please keep in mind that as far as programming goes I am infantile at best. I do however understand why you would need to define what your variables are going to be according to what will be displayed or amount of memory needed to contain your information, BUT for the life of me I can not understand why my VB6 2005 edition requires me to convert this line of code from a string to a double. Any help will be greatly appreciated.

    VB Code:
    1. StrNumber1 = CStr (txtNumber1.Text)
    2. StrNumber2 = CStr(txtNumber2.Text)
    3. StrYourAnswer = CStr(CDbl(StrNumber1) * CDbl(StrNumber2))

    To pinpoint my question. I am converting to a string which, correct me if I'm wrong can be any number or letter, to a double. The way you see it formatted here is the way that VB made me correct it to thrawt any exceptions. Once again any help would be great at this point. I understand what changes are being made, I just don't understand thru all the text I read why I am making them.

    Because if you dont convert those strings to Double(or any numeric type) they will be concatenated .... not added... because in VB + operator is overloaded for Strings.. and if it founds a string on any side ...it will concatenate both sides... so in VB...

    "6" + "3" = "63" 'this is simple concatenation..

    so what you do is..

    CDbl("6") + CDbl("3") = 6 + 3 = 9


    and if you will try to convert any letter to Double.. VB will complain.... so make sure in txtNumber1 and txtNumber2.. its always a number which can be input...

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Why convert from String to Double?

    Just to ensure that I have this all laid out right... So you have to convert each individual variable or value during a mathmatical operation or it has the chance that it will be concantenated.

    In my example:
    StrNumber1 = CStr (txtNumber1.Text)
    StrNumber2 = CStr(txtNumber2.Text)
    StrYourAnswer = CStr(CDbl(StrNumber1) * CDbl(StrNumber2))

    Even with the lack of an ampersand?

    If thats how it has to be then I can accept that. It just seemed that I was throwing too many conversions in there.

  4. #4

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Why convert from String to Double?

    I am just asking about CDbl because if Option Strict is "On" I get A LOT of errors that require me to convert to a double.

  5. #5
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Why convert from String to Double?

    Quote Originally Posted by Abrium
    If thats how it has to be then I can accept that. It just seemed that I was throwing too many conversions in there.
    You are.
    Using a Cstr on txtNumber1.Text is useless as this property already is a string.

    Also, doing the Cstr before writing the result to the textbox isn't necessary either.
    A textboxt will happily accept a numerical type.

    Apart from the + concatenating two strings instead of adding the values they represent, doing calculations with stringvariables is very timeconsuming.

    If you expect to do a large amount of calculations then you should first convert all input variables to some numerical format, do the calculations and then convert the output back to string.

  6. #6

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Why convert from String to Double?

    Thats what I was thinking. I am already adding numerical values in the text field that are considered strings.

    I can mitagate the CStr from the Variables and just CDbl to mathmatics and add a .ToString like so:

    VB Code:
    1. StrYourAnswer = (CDbl(StrNumber1) * (CDbl(StrNumber2)).ToString

    I'm trying to accomplish/understand/learn and you are doing a great job helping out a beginner, I think I have found a long time new home on my way to becoming a great programmer.

    Thanks

  7. #7

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Why convert from String to Double?

    As far as large amounts of calculations, I don't see me entering that area for a while until I feel extremely comfortable with the basics, got to have a great foundation to build on

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,415

    Re: Why convert from String to Double?

    Thread moved from Classic Visual Basic forum, which is for VB6 and earlier

    (VB 2005 is not VB6 [aka VB 1998] or Classic VB - it is VB.Net)

  9. #9
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Why convert from String to Double?

    Use a numeric updown and turn off the up/down buttons. And if you are wondering why you must covert a string if you want to add the value, just think about what would happen if the string weren't a valid number. Sure "6" + "3" makes sense, but what about "asdf" + "3"? What is the answer to that? And if you tried to convert "asdf" to a number, you would get an error because it isn't possible. The easiest solution it to not use a textbox to store a number, use a numeric updown and turn the up/down buttons off. Then you could just use

    nud1.value + nud2.value

    You will probably come across the same kind of problem if the first time you try to accept a date from a textbox. You shouldn't use a textbox to accept a date either, you should use a datetimepicker. It looks very similar to a textbox, but it won't allow invalid dates as input.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width