Solved

Integer to String conversion

Posted on 2011-02-27
8
673 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now