Solved

Integer to String conversion

Posted on 2011-02-27
8
685 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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