Basics of Visual Basic .NET


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"
  • Object-Oriented Programming (OOP)

    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

    Literal 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!"

    Variable Declarations and Casting

    • 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()

    Expressions and Operators

    Conditions (If-Then)

    Loops

    Subroutines and Functions




    Microsoft ASP. NET Web Matrix Starter Kit
    Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
    ISBN: 0735618569
    EAN: 2147483647
    Year: 2003
    Pages: 169
    Authors: Mike Pope
    BUY ON AMAZON

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net