|
-
Sep 7th, 2004, 03:41 AM
#1
Thread Starter
Hyperactive Member
Integer to string conversion
Hello everybody
What`s the easiest way to convert an Integer to a String. I was thinking of creating a variant variable and using it like shown below :
Dim int1 As Variant
Dim strText As String
int1 = 2
strText = "Some text..." & int1.
But this does not work. It crashes with a type validation error. Can such code work or do I need to use some integerTostring conversion function?
thx, in advance
George Papadopoulos
-
Sep 7th, 2004, 03:46 AM
#2
VB Code:
Dim int1 As Variant
Dim strText As String
strText = CStr(int1)
-
Sep 7th, 2004, 04:23 AM
#3
Lose the Variant if it's an integer declare it as such.
-
Sep 7th, 2004, 04:26 AM
#4
Fanatic Member
if you are are converting a Variant type to a string the use the
Cstr$ method that Deepak Sakpal shows but if you know that its a numeric you can use the str$() function.
Point of note if you miss the trailing $ from a function it will not convert to a string but to a varaiant and then VB will make the conversion from variant to sting for you thus is less effienct so always use the $ there are a few functions to use the $
mid$
left$
Right$
ucase$
LCase$
Just remeber if you want to return a string from a built-in function alwasy place $ at the end.
Hope this helped!!!!
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Sep 7th, 2004, 10:33 AM
#5
Dim int1 As Variant
Dim strText As String
int1 = 2
strText = "Some text..." & int1
This code will not produce an error. Are you using .NET?
-
Sep 7th, 2004, 12:36 PM
#6
Well, if divined is using .NET, then Variant would throw an error right off the bat as it doesn't exist in .NET.
I'm going to guess that the code sample is a simplified representation of the real problem and that it isn't the actual code. Moreover, the variable is probably getting set to some value other than a numerical 2 and it's that value that is causing the error. Prime example why variants should be outlawed.
TG
PS: Bombdrop - CStr$ doesn't exist as a function in VB. Since by it's very nature, CStr returns a string, if you put $ on the end of it VB takes it right off.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|