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.)
Printable View
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.)
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]
Hi
+ is only used with strings, while & can be used to add an Integer/Long/... variable to a string
hope this helpsCode:a = "string1"
b = "string 2"
c = a + " " + b 'returns "string1 string2"
d = 123.456
e = a & " = " & d 'returns "string1 = 123.456"
The first msgbox will display 12.Code:Dim strValue As String
strValue = "1"
MsgBox strValue & 2
MsgBox strValue + 2
The second msgbox will display 3.
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)
Oh and type a bit slower ;)Code:MsgBox "5" & 3 'Returns 53
MsgBox "5" + 3 'Returns 8
MsgBox "5" + "3" 'Returns 53
That's a bit confusing. It sound like you're saying the code below wouldn't work.Quote:
Originally posted by Nina
Hi
+ is only used with strings, while & can be used to add an Integer/Long/... variable to a string
Just thought I'd clear that up. :)Code:Dim A As Long
Dim B As Long
Dim C As Long
A = 500
B = 1000
C = A + B
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.