Link to home
Start Free TrialLog in
Avatar of hess
hess

asked on

Number to String

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.
Avatar of hess
hess

ASKER

hold up on the answers for a little whiile i think i've got it
ASKER CERTIFIED SOLUTION
Avatar of Staplehead
Staplehead

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
Avatar of hess

ASKER

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.
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...
Avatar of hess

ASKER

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 +
+ 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.