Results 1 to 15 of 15

Thread: Str Function

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    Str Function

    Dim j As Integer
    Dim Hold_Number As String
    i = 2599
    i = i + 1 'at this line, i becomes 2600 as you would expect
    Hold_Number = Str(i)
    j = Len(Hold_Number)

    After the last line executes, j = 5, not 4 as you would expect. It is putting a space character in front of 2600. I guess I can use the trim() to get rid of that space, that that is sloppy coding; I want to know why the Str() function is giving me a blank character at the beginning of my number that I am converting to a string?

    adam

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Str Function

    The Str function is doing exactly what the documentation says. j=5 is the correct answer based on your code.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Str Function

    No idea. But we don't use Str() anymore.

    Hold_Number = i.ToString
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Str Function

    Quote Originally Posted by dunfiddlin View Post
    No idea. But we don't use Str() anymore.

    Hold_Number = i.ToString
    This will give the answer that the OP was expecting. I agree that using .ToString is the way to go.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: Str Function

    It is explained in the documentation:
    Code:
    When numbers are converted to strings, a leading space is always reserved for the sign of Number. If Number is positive, the returned string contains a leading space, and the plus sign is implied. A negative number will include the minus sign (-) and no leading space.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    Re: Str Function

    Cool. That explains it. I have a program that was written in vb5, and was converted using the cool code converter associated with vb 2008, which changed a bunch of stuff and introduced about a 1000 easy errors to fix such as variants, vb2008 doesn't support variants, so they got converted to objects and that didn't work. I went through and fixed all that, but now I am working on the 2000 or so logic errors that were introduced through the conversion. Str() is what they used back then. How about CStr()? i is not an object so will i.ToString even work?

    adam

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Str Function

    How was i decalred? Almost? everything has a .ToString.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Str Function

    Yup, .ToString will work on anything that can be represented with a string including any of the numeric variable types.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    Re: Str Function

    i is declared as an integer

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    Re: Str Function

    Well ain't that just fancy dancy! So back to my question about half way down in the thread, which would be better:

    use CStr()

    or i.ToString

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Str Function

    Even in VB6 I did not use STR(). I think I last used that function in GWBasic

    CSTR() does not leave a space, STR() will not leave a space if the result is negative but will if positive leaving room for the + but not adding it to the string.

    In VB.Net you should use .ToString

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Str Function

    Quote Originally Posted by Adam Stallcup View Post
    Well ain't that just fancy dancy! So back to my question about half way down in the thread, which would be better:

    use CStr()

    or i.ToString
    No difference, as far as I know but then I always use .ToString
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Str Function

    I seem to remember some MS document suggesting that .ToString is faster. I also seem to remember that there was one case where CStr would return a value where .ToString would not. I would guess that this case was where the object was Nothing. This would be because .ToString is a member of the object, and Nothing has no members, but CStr is not a member, so it could do something with Nothing. I'm not sure that my memory on this is correct, but the logic is pretty good.

    I believe that .ToString will ALWAYS return something, though what it returns is going to be kind of odd at times. I think that, if the type doesn't implement a more useful version, the default implementation is that .ToString will return the type name.
    My usual boring signature: Nothing

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Str Function

    Quote Originally Posted by Adam Stallcup View Post
    Well ain't that just fancy dancy! So back to my question about half way down in the thread, which would be better:

    use CStr()

    or i.ToString
    For most things I would use .ToString. If you move to other .Net languages you will find tostring, but not cstr.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Str Function

    Nothing.ToString will indeed give you a Null exception while CStr(Nothing) will give you, well, nothing! Since you probably don't want to be doing anything with Nothing that's really another argument in favour of .ToString though!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

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



Click Here to Expand Forum to Full Width