-
Jan 24th, 2013, 04:50 PM
#1
Thread Starter
Addicted Member
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
-
Jan 24th, 2013, 04:56 PM
#2
Re: Str Function
The Str function is doing exactly what the documentation says. j=5 is the correct answer based on your code.
-
Jan 24th, 2013, 05:00 PM
#3
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!
-
Jan 24th, 2013, 05:02 PM
#4
Re: Str Function
Originally Posted by dunfiddlin
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.
-
Jan 24th, 2013, 05:03 PM
#5
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 -
-
Jan 24th, 2013, 05:07 PM
#6
Thread Starter
Addicted Member
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
-
Jan 24th, 2013, 05:09 PM
#7
Re: Str Function
How was i decalred? Almost? everything has a .ToString.
-
Jan 24th, 2013, 05:12 PM
#8
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!
-
Jan 24th, 2013, 05:13 PM
#9
Thread Starter
Addicted Member
Re: Str Function
i is declared as an integer
-
Jan 24th, 2013, 05:18 PM
#10
Thread Starter
Addicted Member
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
-
Jan 24th, 2013, 05:20 PM
#11
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
-
Jan 24th, 2013, 05:54 PM
#12
Re: Str Function
Originally Posted by Adam Stallcup
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!
-
Jan 24th, 2013, 05:59 PM
#13
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
-
Jan 24th, 2013, 06:43 PM
#14
Re: Str Function
Originally Posted by Adam Stallcup
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.
-
Jan 24th, 2013, 07:09 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|