Discovering Visual Basic .NET: Working with Variables, Page 2
Storing text in strings
In addition to holding numbers, variables can hold strings. A string is a bunch of letters put together to form words or sentences. Here's an example:
Imports System Module HelloWorld Public Sub Main() Dim FirstName, LastName, WholeName As String FirstName = "Bill" LastName = "Gates" WholeName = FirstName & LastName Console.WriteLine(WholeName) End Sub End Module
Strings are always enclosed by quotation marks so you know exactly where they begin and end, even though the quotes aren't actually a part of the string. In this example, the variable FirstName is assigned the string "Bill", and LastName is assigned "Gates". WholeName is assigned to be equal to both FirstName and LastName. Notice the & separating the FirstName and LastName variables on the right side. In VB.NET, & sticks two strings together, or concatenates them.
There's no space between the first and last names. You have to add the space when you stick them together. Change the line that assigns a value to WholeName in the preceding page to look like this:
WholeName = FirstName & " " & LastName
This time, three strings are concatenated: the one in FirstName, a space, and then the one in LastName. You can see the result:
Creating variables with a value
Here's a shortcut for you. Instead of declaring all your variables and then assigning values to them as a separate step, you can do it all at once!
Imports System Module HelloWorld Public Sub Main() Dim FirstName As String = "Bill Dim LastName As String = "Gates" Dim WholeName As String = FirstName & " " & LastName Console.WriteLine(WholeName) End Sub End Module
This handy technique can save you a few keystrokes.
Making your code readable with comments
Comments, also called remarks, are the way you include notes to yourself or others inside your program without changing the way it works.
In Visual Basic, you can use the single-quote (or apostrophe) to indicate that the rest of the line is a comment.
Imports System Module HelloWorld Public Sub Main() ' This program calculates ' the total cost by adding ' the price and tax. Dim Cost, Tax, Total As Integer Cost = 40 Tax = 2 Total = Cost + Tax Console.WriteLine(Total) End Sub End Module
Note that you need a new apostrophe at the beginning of each new comment line.
You also can put a comment on the same line as code. Again, the comment starts with the apostrophe and ends at the end of the line:
Total = Cost + Tax ' This line does the sum
When you are testing your program, you can temporarily disable some lines you've written, but still easily get them back later if you need to. Rather than delete a line of code, simply put an apostrophe in front it. The apostrophe turns the line into a comment. The line of code no longer affects the way the page works, but it's there if you want to bring it back. To put that line of code back into service, simply remove the apostrophe. Adding an apostrophe is called commenting-out a line of code. Removing the apostrophe is referred to as un-commenting the code.
In this article, you discovered variables—how to create them, assign values to them, do math with them, and display them. You also found out how to add comments to include notes to yourself or to others who might work with your code in the future. In the next article, you'll discover functions and arguments.
About the Author
Bill Hatfield (MCSD, MCAD, MCP) is an internationally best-selling author of books on Internet, intranet and client/server technologies. Among his most recent books is ASP.NET For Dummies.
# # #
Page 2 of 2