Integer to String conversion

Using VB6:

I recently discussed this issue under a different question.
See: “VB automatically overrides my capitalization”

Briefly: I made the mistake of naming a variable with a reserved name;
Dim sTR As String.

Str() is a VB function that returns a number as a string.
So, where I had Str(), VB re-capitalized it as sTR (throughout the entire project), and the function stopped working. (Simply returns a blank.)

I renamed my variable, but the Str function stayed broken.
I saved and exited. The next day, upon reload, all the references to Str() were capitalized correctly. And they all worked.

EXCEPT for the specific subroutine where I had initially goofed. In this procedure (only),
sString = Str(iInteger) returned a blank, “”.

Three observations and related questions:

sString = iInteger makes the conversion without invoking the Str() function at all.
Question: Is this good programming practice?
 If VB does this, why bother with the Str() function at all?

sString = CStr(iInteger) works just fine.
Question: is CStr() preferable to Str()?
Seems redundant.

I presume the function Str$() exists for back-compatibility with older versions of Basic
(like Quick Basic).
Question: Is the “$” no longer necessary?



NormaPosyPhysicistAsked:
Who is Participating?
 
Paul JacksonSoftware EngineerCommented:
The difference between str() and str$() is the return types the first returning a variant and str$ returning a string.
CSTR is international aware and will properly format numbers with commas instead of decimal points if the users regional settings dictate it, it also has a return type of string.

sString = iInteger is not considered good practice as it is an implicit data conversion which potentially could cause data loss, if option strict was set to on in your project this line would be shown as an error.

CSTR sould be used over STR$ mainly because it is much more efficient permance wise

Worth having a look here : http://www.aivosto.com/vbtips/stringopt2.html
0
 
zorvek (Kevin Jones)ConsultantCommented:
Use the VB help for answers to many questions ;-)

From help for the Str function:

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.

Use the Format function to convert numeric values you want formatted as dates, times, or currency or in other user-defined formats. Unlike Str, the Format function doesn't include a leading space for the sign of number.

Note   The Str function recognizes only the period (.) as a valid decimal separator. When different decimal separators may be used (for example, in international applications), use CStr to convert a number to a string.

>sString = iInteger makes the conversion without invoking the Str() function at all. Question: Is this good programming practice? If VB does this, why bother with the Str() function at all?

The CStr is really designed to assign a string to a Variant. The $ modifier is used for efficiency. I've never used the $ modifier as I never realized any performance improvement. Machines are so fast these days it really doesn't matter any more.

Kevin
0
 
zorvek (Kevin Jones)ConsultantCommented:
>Question: is CStr() preferable to Str()?

No. See the above.

Kevin
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
geowrianCommented:
>sString = iInteger makes the conversion without invoking the Str() function at all.
>Question: Is this good programming practice?
 >If VB does this, why bother with the Str() function at all?

This is an implicit conversion from an integer to a string. This isn't bad, but I usually explicitly do the conversion as it makes it a little bit easier for another person reading it to tell that the line is reading a number into a string. There's not a definitive right or wrong answer here.

>sString = CStr(iInteger) works just fine.
>Question: is CStr() preferable to Str()?
>Seems redundant.

CStr() converts to a string. It abides by the system's regional settings (i.e. a period separator in the US). Str() converts assuming only a period separator, and has a leading space reserved for the sign of the character - a space for positive numbers and "-" for negative numbers.

>I presume the function Str$() exists for back-compatibility with older versions of Basic (like Quick Basic).
>Question: Is the “$” no longer necessary?

The "$" is no longer necessary. It explicitly specifies that it is a string, but it's not really used anymore. I would suggest just not using it.
0
 
Paul JacksonSoftware EngineerCommented:
0
 
zorvek (Kevin Jones)ConsultantCommented:
>sString = iInteger is not considered good practice as it is an implicit data conversion which potentially could cause data loss, if option strict was set to on in your project this line would be shown as an error.

There can be no data loss. The operation will either succeed if the string can be converted or fail of the string cannot be converted. And failure will occur with:

   sString = iInteger

with the same scenarios as with:

   sString = CStr(iInteger)

Kevin
0
 
Paul JacksonSoftware EngineerCommented:
Maybe data loss would not occur in this scenario shown, I was talking generally why it is not a good idea to have implicit data conversions.
0
 
NormaPosyPhysicistAuthor Commented:
Thank you all.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.