Solved

Number to String

Posted on 1998-07-02
8
158 Views
Last Modified: 2010-05-03
I need to convert an iniger variable to a string.  Iv'e tried this

Dim S As String
Dim X As Integer

S=X

This works sometimes but no others. When i use it repeatedly i get a type thireen error. I looked into the help files and found a ToText function under  "String Functions", however when i try to use it i get an error Sub or "Function not defined"

Sorry i only have five points but this question should be fairly simple.
0
Comment
Question by:hess
  • 3
  • 3
  • 2
8 Comments
 

Author Comment

by:hess
ID: 1464838
hold up on the answers for a little whiile i think i've got it
0
 
LVL 1

Accepted Solution

by:
Staplehead earned 0 total points
ID: 1464839
Hess,

Assuming that the data in S is numeric, you could use:

dim S as String
dim X as Integer

S = "1234"
X = CInt (S)

There's a whole family of Cxxx functions for conversions...

        Larry
0
 

Expert Comment

by:wsanchez
ID: 1464840
You could also try using Str(number).  For example:

Dim ThisString

ThisString = Str(123)  'returns " 123"
ThisString = Str(-123) 'returns "-123"

When a number is converted to a string, a leading space is reserved for its sign. Hence the space in " 123" just before 1.  This does not only convert integers but also numbers with decimal points.
0
 

Expert Comment

by:wsanchez
ID: 1464841
You could also try using Str(number).  For example:

Dim ThisString

ThisString = Str(123)  'returns " 123"
ThisString = Str(-123) 'returns "-123"

When a number is converted to a string, a leading space is reserved for its sign. Hence the space in " 123" just before 1.  This does not only convert integers but also numbers with decimal points.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:hess
ID: 1464842
I siad hold up on the answers i think i've gott it and i did. It's fixed. i did it myself.
All i had to do was put the integer into a temp string and then i added the temp stings together. I guess i have to give up my five points though still cause you answered it any ways. Mabey you were writing your answer before i posted my comment but anyways, thankyou.
0
 
LVL 1

Expert Comment

by:Staplehead
ID: 1464843
hess,

yup, i didn't see your "hold on"... nonetheless, when i came back, i noticed that i typed the answer backward; it should have been:

s = CStr(x)

your comment confused me, though: what do you mean by "added temp strings together"?  if you did:
   s = "" & x
or the "less correct"
   s = "" + x
then vb did a Cstr for you, anyway...
0
 

Author Comment

by:hess
ID: 1464844
dim x as integer, y as integer
dim temp as string
dim temp2 as string
dim temp3 as string
x=6
y=4
temp=x
temp2=y
temp3=temp+temp2

this is similar to what i did
Why is string + string less correct.
It appears to work fine.
Is & faster than +
0
 

Expert Comment

by:wsanchez
ID: 1464845
+ and & can both be used in your situation but to eliminate ambiguity, avoid using + for string concatenations since it could also be used for addition of two or more expressions depending on the type of variables declared.

For example:

if + is used on two string Variants, the result is a concatenation;
if it is used on two numeric Variants, the result is addition;
if it is used on one numeric and one string Variant, the result is addition.

On the other hand,
if & is used and one expression is not a string, it is converted to a string Variant and the result is a string Variant;
if it is used and both expressions are string expressions, the result is a String;

In what you did, since temp and temp2 are both Strings, the result is also a string, other than the fact that temp3, the result, was declared as String.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now