|
Like many programming languages, Visual Basic .NET uses syntax and expressions that are similar to basic algebra. The following lists describes the general syntax of Visual Basic .NET:
Visual Basic .NET is not case sensitive. These statements are equivalent:
Imageslide.ImageURL = Path & Slidenames(CurrentSlide imageSlide.imageUrl = path & slideNames(currentSlide)
You can indent (and outdent) any way you like.
You can add comments to the code as notes to yourself. Comments are marked with an apostrophe:
The following adds one to the current page count.pageCount += 1
You can create statements of any length. If a statement gets too long to read in Web Matrix, you can split it across lines using a line-continuation character (_) preceded by a space:
labelNavbar.Text = "The caption for this label" & " can be very long and you might want to" & " split it to make it readable in the program."
You can split lines only between expressions.
Counting in Visual Basic .NET starts at 0. The first character in a string is character 0; the first item in a list is item 0:
captions(0) = "First caption captions(1) = "Second caption"
Visual Basic .NET is an object-oriented language. For our purposes, this concept means adhering to the following tenets:
All elements you work with are objects, such as buttons, text boxes, the page itself, data tables, colors, and so on.
An object is defined in a class, which is a program that describes what the object is and how it behaves. When you work with an object, you are working with an instance of a class. A Button control on a page is an instance of the Button class. ASP.NET and the .NET Framework are, at one level, simply a vast collection of classes that you can create instances of as needed.
Objects have properties that you can set to affect the behavior or look of an object. For example, a Label object has a Text property you can set to display text. Naturally, you can also read an object s properties.
Label1.Text = "Hello, World. slideCount = slidesArray.Length
Objects have methods (like verbs) that you can call to get the object to perform an action. Some methods allow you to pass values that the method should use:
FutureDate = Now.AddMonths(2)
Objects raise events (send messages) when something happens to them. When a button is clicked, it raises a Click event; when a page first runs, it raises a Load event; and so on. The majority of your programming consists of event handlers, which are chunks of code that run in response to the event:
Sub buttonPreview_Click(sender As Object, e As EventArgs Label1.Text = "You clicked the Preview button! End Sub
Objects can contain other objects. For example, a ListBox control (object) contains a collection of Item objects that constitute the elements in the list. You can refer to the contained object this way:
captionColorName = ListBox1.Items(1).Text
You specify literal text (strings) in quotation marks. You can enclose literal text in either single or double quotation marks, as long as the opening and closing quotation marks are of the same type.
pageTitle = "Welcome to my page!"
Information that the program needs to work with is stored in variables:
slideCount = 1 nextSlide = currentSlide + 1
Variable names can t have a space in them, must include at least one alphabetic character, and can t start with a number. You also can t use a Visual Basic .NET keyword as a variable name.
Before you use a variable, you have to declare it, which tells Visual Basic .NET the name of the variable and what data type it is. You use a Dim statement to declare a variable:
Dim slideCount As Intege Dim captionColorName As String
Common data types are numbers (Integer, Double, Long), text (String), and true/false (Boolean) values. The data types for variables can be any class. For example, you can create variables to hold a Button control or a color value:
Dim b As Butto Dim c As Color
To use a variable of one type where Visual Basic .NET expects a different one, you can cast (convert) the variable. Visual Basic .NET can automatically perform certain casts, such as converting integers to strings. But sometimes you have to explicitly cast from one type to another using the CType function:
Dim b As Butto b = CType(sender, Button)
At other times, you can call specific methods such as FromName and ToString to perform convesions:
Dim c As Colo Label1.ForeColor = c.FromName("Red" Label1.Text = "Current color is " & c.ToString()
An expression is any combination of variables, literal text, property values, and so on, combined with operators. You use standard math operators for arithmetic (* for multiplication and / for division). You can use parentheses to indicate precedence:
nextPage = (currentSlide + 1) * 5
You can combine text using the concatenation operator (&):
Label1.Text = "This is number " & currentSlide+1 & " of " _ & slideCount
Visual Basic .NET supports some special shorthand operators, such as +=,-=, *=, /=, and &=. You can use these operators to assign a new value to a variable based on its old value. Here are some examples:
Add 1 to the currentSlide variabl currentSlide += Subtract 1 from the currentSlide variabl currentSlide -= Concatenate text onto the current text in Label Label1.Text &= "Add another line to the label<BR>"
You can test the state of a value or an expression using an If-Then or If-Then-Else block. The block always ends with End If. The If-Then test evaluates whether an expression is true and proceeds accordingly:
If currentSlide > slideNames.Length - 1 The currentSlide = End I If IsPostBack = True The currentSlide = Viewstate("currentSlide" Els currentSlide = Viewstate("currentSlide") = currentSlid imageSlide.imageUrl = path & slideNames(currentSlide End If
You can test for a series of exclusive conditions using an If-ElseIf block:
If radioFontArial.Checked = True The captionFontName = "Arial, Helvetica Elseif radioFontCourier.Checked = True The captionFontName = "Courier New, Courier Elseif radioFontTimes.Checked = True The captionFontName = "Times New Roman, Times End If
When testing for a true condition, the following are equivalent:
If IsPostBack = True The If IsPostBack Then
You can use logical operators (And, Or, Not) as part of the If-Then test:
If Not (IsPostBack) The Logic her End I If (IsPostBack = True AND currentSlide = 0) The Logic her End If
To test whether an object exists, use the special test Is Nothing:
If Session("slidenumber") Is Nothing The Logic her End if
To repeat a sequence of statements a fixed number of times, you can use a For-Next block:
totalSlideCount = slideArray.Length For counter = 0 to totalSlideCoun nextSlide = slideArray(counter More logic her Next
To repeat statements until some condition is met, you can use a While block:
Dim quotesRemaining As Boolea quotesRemaining = Fals While quotesRemaining = Fals nextslideshow = quotesStreamReader.ReadLine( If nextslideshow Is Nothing The quotesRemaining = Tru Els Logic her End I End While
To repeat statements until you ve worked with every element in a collection, you can use the For Each-Next block
Dim lItem As ListIte For Each lItem in ListBox Label1.Text &= lItem.Text & "<BR> Next
A subroutine is a block of code that executes independently. Event handlers are subroutines. You can create your own subroutines to incorporate code that you want to call at different times in your programs:
Sub buttonPreview_Click(sender As Object, e As EventArgs Preview("Preview" End Su Sub Preview(displayText captionLabel.Text = displayTex End Sub
To create reusable blocks that calculate a value or otherwise manipulate data and then return a result, you can create a function:
squaredValue = CalculateSquare(userValue Function CalculateSquare(ByVal num As Integer) As Intege num *= nu Return nu End Function
|