Solved

Integer to String conversion

Posted on 2011-02-27
8
688 Views
Last Modified: 2012-05-11
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?



0
Comment
Question by:NormaPosy
8 Comments
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 150 total points
ID: 34994283
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
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 34994289
>Question: is CStr() preferable to Str()?

No. See the above.

Kevin
0
 
LVL 12

Assisted Solution

by:geowrian
geowrian earned 150 total points
ID: 34994323
>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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Accepted Solution

by:
Paul Jackson earned 200 total points
ID: 34994343
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34994354
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 34994363
>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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34994376
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
 

Author Closing Comment

by:NormaPosy
ID: 34994383
Thank you all.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question