3. String Variables
3. String Variables
Again we've started with the Dim word. Then we've called the first variable FirstName. Finally,
we've ended the line by telling Visual Basic that we want to store text in the variable - As String.
we've set up the variables. But there is nothing stored in them yet. We store something in a
variable with the equals sign ( = ). Let's store a first name and a last name in them
FirstName = "Bill"
LastName = "Gates"
Here, we’ve instructed Visual Basic "Store the word 'Bill' into the variable FirstName and
store the word 'Gates' into the variable called LastName.
Note the quotation marks surrounding the two words. We didn't say Bill, we said "Bill".
VB needs the two double quotation marks to identify your text, your String.
To test all this out, add a new Button to your Form. Set the Text property of the Button to "String
Test". Your Form would then look like this:
1
Double click your new button, and add the following code:
FirstName = "Bill"
LastName = "Gates"
Textbox1.Text = FullName
Your code window should now look like this
2
Explanation:
we stored the string "Bill" and the string "Gates" into two variables. we're now joining those
two variables together. We do this with the ampersand symbol ( & ).
The ampersand is used to join strings together. It's called Concatenation.
Once Visual Basic has joined the two strings together (or concatenated them), we're saying
"store the result in the variable called FullName". After that, we tell VB to display the result
in our Textbox.
So, once you've typed the code, start your programme and test it out.
Once the programme is running, Click the Button and see what happens. You should have a
Form that looks something like this one:
The textbox displays the text stored in our variables, "Bill" and "Gates". We joined them
together with the ampersand ( & ).
Note that the two words are actually joined as one. We can add a bit of space between the
two words by using another ampersand.
Change this line FullName = FirstName & LastName to this:
This means join this lot together: the variable called FirstName and a single blank space and
the variable called LastName. When you've finished concatenating it all, store the result in
the variable FullName.
3
Notice that we don't surround FirstName and LastName with quotation marks. This is
because these two are already string variables; we stored "Bill" into FirstName and "Gates"
LastName. So VB already knows that they are text.
Exercise
i. Remove one of the ampersand symbols (&) from this line in your code:
ii. Move your cursor down a line or two. You should see that part of your code has a wiggly
blue line under it:
VB is telling you that it has problems with this line of code. If you hold your mouse over the
wiggly blue line, VB tries to provide an explanation:
The explanations VB provides are sometimes enigmatic(mysterious). But you will know that
there is a problem. If you run the code, you'll get this popping up at you:
Click the NO button. Put the ampersand back in, and all will be well.
Exercise
Amend your code so that the textbox reads Gates Bill when the Command button is clicked.
4
Exercise
Add another string variable to your code. The variable should hold a middle name. Display the
first name, the middle name and the last name in the textbox.
Points to remember:
Your variable names cannot include spaces. So the variable MiddleName would be all right, but
Middle Name will get you an error message
When you're putting text into your new variable, don't forget the two double quotes
Remember to put in enough ampersands in your FullName = line of code
5
Scroll down and locate the Text property. Delete the default text, and just leave it blank.
Click on your first textbox to select it. Change the Name property from Textbox1 to
txtFirstName.
What we've done is to give the two textboxes more descriptive names. This will help us to
remember what is meant to go in them.
Unfortunately, if you view your code (click the Form1.vb tab at the top, or press F7 on your
keyboard), you'll see that the blue wiggly lines have returned:
It's displaying this message because you changed the name of your Textbox1.
You now no longer have a textbox with this name.
In the code above, change Textbox1 into txtFirstName and the wiggly lines will go away.
(Change it in your Button1 code as well.) Your code should now read:
txtFirstName.Text = FullName
Run your programme again. If you see any error messages, stop the programme and look for the
wiggly lines in your code.
We'll now change our code slightly, and make use of the second textbox. You'll see how to get at
the text that a user enters.
FirstName = "Bill"
LastName = "Gates"
FirstName = txtFirstName.Text
LastName = txtLastName.Text
6
Remember: the equals ( = ) sign assigns things: Whatever is on the right of the equals sign gets
assigned to whatever is on the left. What we're doing now is assigning the text from the
textboxes directly into the two variables.
Amend your code slightly so that the Whole Name is now displayed in a message box. Your
code should now be this:
FirstName = txtFirstName.Text
LastName = txtLastName.Text
MsgBox(FullName)
Run your programme. Enter "Bill" in the first textbox, and "Gates" in the second textbox. Then
click your "String Test" button. You should get this:
Before we changed the code, we were putting a person's name straight in to the variable
FirstName
FirstName = "Bill"
But what we really want to do is get a person's name directly from the textbox. This will make
life a whole lot easier for us. After all, not everybody is called Bill Gates! In the line FirstName
7
= txtFirstName.Text that is what we're doing - getting the name directly from the textbox. What
we're saying to Visual Basic is this
And that's all there is to reading values from a textbox - just access its Text property, and then
pop it into a variable
Exercise
When you complete this exercise, your form should look like this one (the first button and its
code has been deleted, but you don't have to):