Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenating Numbers

Status
Not open for further replies.

jonathanmb

Technical User
Feb 19, 2003
93
US
I just ran across an interesting problem in an application I'm working on. Take this code for example...

Code:
Dim intTest As Integer

intTest = 2 & 3

Debug.Print intTest

In my mind this should NOT be allowed. However, VB6 nicely concatenates the numbers rather than complaining about an invalid operation.

Can anyone offer an explanation as to why concatenating numeric values is allowed?

--
Jonathan
 
Because the operations that you perform may cause Visual Basic to change the representation it is using for a particular variable. If you would have done intTest = 2 + 3, Visual Basic would have returned 6, and not 24.

For further information, check your MSDN.

Hope it was helpful.

Nunina [gorgeous]
 
The "&" operator does automatic type conversions when one of the expressions are not strings and then returns a variant, so 2 & 3 returns a variant type value of 23 and you can assign a variant value of 23 to intTest.
 
For once I was told to RTFM rather than telling someone else to do the same... heh. That serves me right. Should have checked the documentation first where it states the ampersand converts a non-string to a string variant.

Thanks to the both of you.

--
Jonathan
 
Nunina, when I went to school, we usually took 2+3 to be 5, and not 6...

You been at the good coffee again?

mmilan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top