|
-
Jul 14th, 2000, 04:48 AM
#1
Thread Starter
Lively Member
What is commonly used when working with strings?
Do one use + or & to put two string together?
& can be annoying when you type fast and get
string1& string2 ---> error (string1& is not a Long parameter.)
-
Jul 14th, 2000, 04:54 AM
#2
Fanatic Member
I always use &.
I'm not sure of this but I think if you use the + and both strings contain numbers then you will get the result of them being added.
Nope just checked that and that doesn't happen.
[Edited by Stevie on 07-14-2000 at 05:56 AM]
-
Jul 14th, 2000, 04:57 AM
#3
Lively Member
Hi
+ is only used with strings, while & can be used to add an Integer/Long/... variable to a string
Code:
a = "string1"
b = "string 2"
c = a + " " + b 'returns "string1 string2"
d = 123.456
e = a & " = " & d 'returns "string1 = 123.456"
hope this helps
-
Jul 14th, 2000, 05:01 AM
#4
Fanatic Member
Code:
Dim strValue As String
strValue = "1"
MsgBox strValue & 2
MsgBox strValue + 2
The first msgbox will display 12.
The second msgbox will display 3.
-
Jul 14th, 2000, 05:03 AM
#5
PowerPoster
You should always use the & coz if you add a number to a string VB tries to convert the first strig to a number and add the second instead of just putting them together. That only happens if you do something like this:
(Where the values can also be variables of course)
Code:
MsgBox "5" & 3 'Returns 53
MsgBox "5" + 3 'Returns 8
MsgBox "5" + "3" 'Returns 53
Oh and type a bit slower 
-
Jul 14th, 2000, 05:04 AM
#6
Addicted Member
Originally posted by Nina
Hi
+ is only used with strings, while & can be used to add an Integer/Long/... variable to a string
That's a bit confusing. It sound like you're saying the code below wouldn't work.
Code:
Dim A As Long
Dim B As Long
Dim C As Long
A = 500
B = 1000
C = A + B
Just thought I'd clear that up. 
To answer the original question, I always use '&' for adding two strings together. Someone once said that using '+' to add strings together is not very reliable for some reason... I forget now.
Visual Basic 6 Enterprise Edition + SP4
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
|