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.Dim LastName 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. LastName = "Gates"
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:
Dim FirstName As String
Dim LastName As String
Dim FullName As String
Dim LastName As String
Dim FullName As String
FirstName = "Bill"
LastName = "Gates"
LastName = "Gates"
FullName = FirstName & LastName
Textbox1.Text = FullName
Your code window should now look like this (some of the first line has been cropped in the image below):
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:
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.
No comments:
Post a Comment