View Full Version : Simple visual basic question
Beaver
09-27-2002, 07:42 AM
Converting numeric variables to strings?
c = val(c$)
will convert a string to a number but how the hell do you convert a number to a string?
I tried
c = str$(c)
c = str(c)
both give me an error
keyman
09-27-2002, 07:52 AM
Dim strTest$, intI%
Let strTest = "20"
Let intI = cInt(strTest)
intI = IntI + 30
MsgBox "Integer: " & intI
strTest = Trim(cStr(intI))
MsgBox "String: " & strTest
Beaver
09-27-2002, 08:09 AM
Thanks keyman but I just figured out the problem but I'm not sure why VB don't like it
x = 123
c$ = str(x)
print c$
result = 123 just like you would expect BUT
c = 123
c$ = str(c)
print c$
Gives an error
---------------------
Compile error
Type-declaration does not match declared data type
---------------------
For some reason VB don't like using the same variable name when swapping from numeric to string!!
keyman
09-27-2002, 08:13 AM
Yeah, that is because a variable cannot change its type after it has been allocated. Unless you use a variant, which is capable of becomming multiple types.
Dim c As Variant
c = 123
c = Str(c)
MsgBox c
Don't use the type declaration characters with Variants.
sonicpuke
09-27-2002, 11:22 AM
Here ya go ;) Sometimes it's nice to be a nerd
Dim c As Variant
c = 123
d$ = CStr(c) ' << Heres how it's done
'Or you could just do this below
MsgBox CStr(c)
Cstr is the answer ;)
sonic
vBulletin® v3.7.3, Copyright ©2000-2012, Jelsoft Enterprises Ltd.