Wednesday, May 15, 2013

String Tutorial in Visual Basic.NET

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?
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:
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 (some of the first line has been cropped in the image below):

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.

No comments:

Post a Comment