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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble putting variables into one string

Status
Not open for further replies.

loll

Technical User
Jul 19, 2002
4
US
Hi Im very new to Visual BAsic. I am having problems putting two variables into a string

To test my code Im using a msgbox to show the results

The tests im doing is this

mytest = variable1
and
mytest = variable2

Followed by the msg box.. both are printing the correct infomation

However I want to do this:

mytest = variable1 & variable

When I do this it will only print out the first variable .. the second one seems to not exist and even if I make it like this:

mytest = variabl1 & "hello world"

the hello world is not printed out...

Can someone tell me what im doing wrong..

the two variables are defined as type string and so is mytest

So im really not sure what im doing wrong.. any help would be much appreciated!
 
The syntax
mytest = variable1 & "hello world"
is correct and should work. Did you misspell the variable name?

This code should work fine:
Dim variable1 as string
Dim variable2 as string

variable1="Hello "
variable2="world!"
msgbox variable1 & variable2

 
when I try the example you gave .. it worked fine

however here is a direct copy and paste so perhaps you can see my error in the code somewhere

Dim user As String * 30
Dim pass As String * 30
Dim mytest As String * 30

'gets user and pass from an ini file here

MsgBox (user)
MsgBox (pass)
mytest = user & pass
MsgBox (mytest)

this is outputting

theuser
thepass
theuser

where it should be displaying

theuser
thepass
theuserthepass

It doesnt make any sense to me why its doing this
 
The difference is in how the variable are declared. When you define the variables Dim user as String * 30, you are making user a fixed length variable of 30, and when you concatenate, it will begin concatenating at the 31st position. It doesn't show up in the message box because the variable has a maximum length of 30.

You could do either of the following to things:

remove the * 30 from the dimension statement or
trim the strings before concatenation
mytest = trim(user) & trim(pass)

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 

Dim StringVarialbe As String * 30

I believe (could be wrong) this initializes the string with EOL characters (End of Line) try putting a trim around each variable or

Dim StringVariable As String

StringVariable = Space(30)

This will initialize your string with blank spaces or

Dim StringVariable As String

StringVariable = String(30, Chr(32))

(same thing)

or

StringVariable = String(30, Chr(0))
 
Hi again! :)

I tried trimming the variables with no luck.. do I need to some how remove any character that isnt alphanumeric? somethign that would get rid of spaces and EOL characters etc? I thought trimming only removed white space.. so any other character (like EOL) would still be in it...

Is there a way to do that? or should I just give up on this :(
 
There is no EOL character in the string unless you put it there. I did the trim and it works.

I would make sure that you are trimming each string individually
mytest = trim(user) & trim(pass)
and not just the overall result

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
just a guess but this came up once or twice before in this forum. try checking for chr$(0).

Code:
Dim user As String
Dim pass As String
Dim mytest As String

user = "theuser" & String$(10, 0)
pass = "thepass" & String$(10, 0)
mytest = user & pass

MsgBox user
MsgBox pass
MsgBox mytest

debug.Print user
debug.Print pass
debug.Print mytest

If InStr(1, user, Chr$(0)) > 0 Then
  user = Left$(user, InStr(1, user, Chr$(0)) - 1)
End If
If InStr(1, pass, Chr$(0)) > 0 Then
  pass = Left$(pass, InStr(1, pass, Chr$(0)) - 1)
End If
mytest = user & pass

MsgBox user
MsgBox pass
MsgBox mytest

debug.Print user
debug.Print pass
debug.Print mytest
 
This line will both trim the trailing spaces, and remove a null:
User = Left$(User, InStr(User, Chr$(0)) - 1)

Do this with both variables, and it should resolve your problem.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top