0% found this document useful (0 votes)
3 views

3. String Variables

The document provides an introduction to string variables in Visual Basic, explaining how to declare and store text in variables using the 'As String' syntax. It demonstrates concatenation of strings using the ampersand (&) and shows how to retrieve text from textboxes to dynamically assign values to variables. Exercises are included to reinforce understanding of string manipulation and variable assignment.

Uploaded by

safarijimmy25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

3. String Variables

The document provides an introduction to string variables in Visual Basic, explaining how to declare and store text in variables using the 'As String' syntax. It demonstrates concatenation of strings using the ampersand (&) and shows how to retrieve text from textboxes to dynamically assign values to variables. Exercises are included to reinforce understanding of string manipulation and variable assignment.

Uploaded by

safarijimmy25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

String Variables

An Introduction to Programming Strings


 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.

 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.

NB: if you're storing text in a variable, include 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:

1
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

2
Explanation:

FullName = FirstName & LastName

 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:

FullName = FirstName & " " & LastName

 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:

FullName = FirstName & " " & LastName

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

Assigning Textbox text to your Variables


Working with Textboxes
Instead of putting direct text into your variables, such as "Bill" or "Gates", you can get text from
a textbox and put that straight into your variables. We'll see how that's done now. First, do this:

 Add a new textbox to your form


 With the textbox selected, locate the Name property in the Properties area:

 Change the Name property Textbox2 and enter txtLastName.

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:

If you hold your cursor of the Textbox1, you'll see this:

 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.

Locate these two lines of code

FirstName = "Bill"
LastName = "Gates"

Change them to this

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:

Dim FirstName As String


Dim LastName As String
Dim FullName As String

FirstName = txtFirstName.Text
LastName = txtLastName.Text

FullName = FirstName & " " & LastName

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

 Look for a Textbox that has the Name txtFirstName


 Locate the Text property of the Textbox that has the Name txtFirstName
 Read whatever this Text property is
 Put this Text property into the variable FirstName

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

 Add a third textbox to your form


 Change its Name property to txtWholeName
 Add labels to your form identifying each textbox (A quick way to add more labels is to use the
toolbox to add one label. Then right click on that label. Choose Copy from the menu. Right click
on the form, and select Paste.)
 Write code so that when the "String Test" button is clicked, the whole of the persons name is
displayed in your new textbox

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):

You might also like