Solved

Integer to String conversion

Posted on 2011-02-27
8
703 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month5 days, 13 hours left to enroll

626 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