String Variables

An Introduction to Programming Strings


So we've learnt something about variables, what they are and how to set one up. We learnt about the word "integer", and that integer variables held numbers. But what if we don't want numbers? After all, our first Form asked users to type in their First Name and Last Name. Names are not numbers, so what do we do then? Well that's where Strings come in.

What is a String? Actually a string is nothing more than text. And if we want Visual Basic to store text we need to use the word "String". To set up a variable to hold text we need to use As String and not As Integer. If the information we want to store in our variables is a First Name and a Last Name, we can set up two variables like this.

Dim FirstName As String
Dim LastName As String

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.

So 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 said to Visual Basic "Store the word 'Bill' into the variable FirstName and store the word 'Gates' into the variable called LastName. But pay attention to the quotation marks surrounding the two words. We didn't say Bill, we said "Bill". Visual Basic needs the two double quotation marks before it can identify your text, your String.

So remember: if you're storing text in a variable, don't forget the quotation marks!

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 (it will look rather grey and dull in newer version of Visual Studio Express):

VB Net Form to test string variables

Double click your new button, and add the following code:

Dim FirstName As String
Dim LastName As String
Dim FullName As String

FirstName = "Bill"
LastName = "Gates"

FullName = FirstName & LastName

Textbox1.Text = FullName

Your code window should now look like this: (You can put in an underscore character to break up long lines in your code.)

VB .NET code window

There's a line there that needs explaining

FullName = FirstName & LastName

In the two lines of code above that one, we stored the string "Bill" and the string "Gates" into two variables. What we're doing now is 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 name appears in the text box

The textbox displays the text stored in our variables, "Bill" and "Gates". We joined them together with the ampersand ( & ). But as you can see, 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:

FullName = FirstName & " " & LastName

What we're saying here is 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.

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.




Remove one of the ampersand symbols (&) from this line in your code:

FullName = FirstName & " " & LastName

Move your cursor down a line or two. You should see that part of your code has a wiggly blue or red line under it:

Synatx error in VB NET

VB is telling you that it has problems with this line of code. If you hold your mouse over the wiggly blueor red line, VB tries to provide an explanation:

Error message

The explanations VB provides are sometimes enigmatic. But you will know that there is a problem. If you run the code, you'll get this popping up at you:

Build Erros in VB NET

Click the NO button. Put the ampersand back in, and all will be well.

Exercise B

Amend your code so that the textbox reads Gates Bill when the Command button is clicked.

Answer to Exercise B

Exercise C

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.

Answer to Exercise C

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

In the next part, we'll take a look at how to asign text from a textbox into our string variables.