JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

All About Statement Syntax

Each statement has its own syntax, and there are a few conventions and terms you should be aware of before getting started: In the formal definition of each statement, you use brackets, [ and ], for optional items, and curly braces, { and }, to indicate that you select one of the enclosed items, like this for the Dim statement:

[ <attrlist> ] [{ Public | Protected | Friend | Protected Friend |
Private | Static }] [ Shared ] [ Shadows ] [ ReadOnly ] Dim [ WithEvents ]
name[ (boundlist) ] [ As [ New ] type ] [ = initexpr ]

Here, all the items in square brackets are optional, and you only choose one of the items in curly braces if you want to use the keyword Public, Protected, Friend, Protected Friend, Private, or Static. I'll explain the keywords used in each statement; note that keywords like Public, Protected, Friend, Protected Friend, and Private will only really make sense when you've mastered object-oriented programming, which we'll discuss later in the book:

  • attrlist-A list of attributes that apply to the variables you're declaring in this statement. You separate multiple attributes with commas.

  • Public-Gives variables public access, which means there are no restrictions on their accessibility. You can use Public only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Public, you can omit the Dim keyword if you want to.

  • Protected-Gives variables protected access, which means they are accessible only from within their own class or from a class derived from that class. You can use Protected only at class level (which means you can't use it inside a procedure), because you use it to declare members of a class. Note that if you specify Protected, you can omit the Dim keyword if you want to.

  • Friend-Gives variables friend access, which means they are accessible from within the program that contains their declaration, as well as anywhere else in the same assembly. You can use Friend only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Friend, you can omit the Dim keyword if you want to.

  • Protected Friend-Gives variables both protected and friend access, which means they can be used by code in the same assembly, as well as by code in derived classes.

  • Private-Gives variables private access, which means they are accessible only from within their declaration context (usually a class), including any nested procedures. You can use Private only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Private, you can omit the Dim keyword if you want to.

  • Static-Makes variables static, which means they'll retain their values, even after the procedure in which they're declared ends. You can declare static variables inside a procedure or a block within a procedure, but not at class or module level. Note that if you specify Static, you can omit the Dim keyword if you want to, but you cannot use either Shadows or Shared.

  • Shared-Declares a shared variable, which means it is not associated with a specific instance of a class or structure, but can be shared across many instances. You access a shared variable by referring to it either with its class or structure name, or with the variable name of an instance of the class or structure. You can use Shared only at module, namespace, or file level (but not at the procedure level). Note that if you specify Shared, you can omit the Dim keyword if you want to.

  • Shadows-Makes this variable a shadow of an identically named programming element in a base class. A shadowed element is unavailable in the derived class that shadows it. You can use Shadows only at module, namespace, or file level (but not inside a procedure). This means you can declare shadowing variables in a source file or inside a module, class, or structure, but not inside a procedure. Note that if you specify Shadows, you can omit the Dim keyword if you want to.

  • ReadOnly-Means this variable only can be read and not written. This can be useful for creating constant members of reference types, such as an object variable with preset data members. You can use ReadOnly only at module, namespace, or file level (but not inside procedures). Note that if you specify ReadOnly, you can omit the Dim keyword if you want to.

  • WithEvents-Specifies that this variable is used to respond to events caused by the instance that was assigned to the variable. Note that you cannot specify both WithEvents and New in the same variable declaration.

  • name-The name of the variable. You separate multiple variables by commas. If you specify multiple variables, each variable is declared of the data type given in the first As clause encountered after its name part.

  • boundlist-Used to declare arrays; gives the upper bounds of the dimensions of an array variable. Multiple upper bounds are separated by commas. An array can have up to 60 dimensions.

  • New-Means you want to create a new object immediately. If you use New when declaring an object variable, a new instance of the object is created. Note that you cannot use both WithEvents and New in the same declaration.

  • type-The data type of the variable. Can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, or String; or the name of an enumeration, structure, class, or interface. To specify the type, you use a separate As clause for each variable, or you can declare a number of variables of the same type by using common As clauses. If you do not specify type, the variable takes the data type of initexpr. Note that if you don't specify either type or initexpr, the data type is set to Object.

  • initexpr-An initialization expression that is evaluated and the result is assigned to the variable when it is created. Note that if you declare more than one variable with the same As clause, you cannot supply initexpr for those variables.

Main Page

Overview: Procedures, Classes, Modules, Methods, and More

Note the terms like module and class above; they are important in the definition of the Dim statement. If you've programmed in Visual Basic before, you're familiar with terms like these. However, if these terms are unfamiliar to you, they can present a barrier in your study of Visual Basic, because-as with so many aspects of Visual Basic .NET-you need some preliminary understanding of terms like these to get anywhere at all. Here's an overview of like terms that will come up a great deal; if you're already familiar with Visual Basic programming, feel free to skip this list, and if some of these are unfamiliar to you, don't worry, we'll see them all in detail in this book:

  • block-As we've seen, a block of statements is made up of a number of statements, enclosed inside another statement designed for that purpose.

  • file-Refers to code in the same file.

  • variable-A named memory location of a specific data type (such as Integer) that stores data. You can assign data to a variable with the assignment operator; for example, if you have a variable named temperature, you can assign it a value of 72 like this: temperature = 72.

  • procedure-A callable series of statements that may or may not return a value. You may pass data to the procedure in the form of parameters in a procedure call like this addem(2, 3), where 2 and 3 are parameters I'm passing to a procedure named addem. When the series of statements terminates, control returns to the statement that called the procedure.

  • sub procedure-A procedure that does not return a value.

  • function-A procedure that returns a value.

  • method-A procedure that is built into a class.

  • constructor-A special method that is automatically called when you create an object from a class. Used to initialize and customize the object. You can pass data to constructor methods just like other methods.

  • module-Visual Basic modules are designed to hold code-that is, to separate the code in them from other, possibly conflicting, code. Their main purpose is to make your code more modular, and if your program is a long one, you should consider breaking it up into modules.

  • class-This is an OOP class, which can contain both code and data; you use classes to create objects. A class can have members, which are elements that can be accessible from outside the class if you so specify. Data members are called fields, procedure members are called methods.

  • object-This is an instance of a class, much like a variable is an instance of a data type.

  • shared or static members and instance members-Fields and methods that apply to a class and are invoked with the class name are called shared or static fields and methods; the fields and methods that apply to objects created from the class are called instance fields and methods.

  • structure-Also an OOP element, just like a class but with some additional restrictions. In VB6 and earlier, you used structures to create user-defined types; now they are another form of OOP classes.

Note also the term attrlist in the description of the parts of the Dim statement above. This term corresponds to a list of attributes; attributes are new to VB .NET, and I'll take a look at them here.

Main Page

Understanding Attributes

Attributes are items that let you specify information about the items you're using in VB .NET. You enclose attributes in angle brackets, < and >, and you use them when VB .NET needs to know more than standard syntax can specify. For example, if you want to call one of the functions that make up the Windows Application Programming Interface (API), you have to specify the Windows Dynamic Link Libraries (DLLs) that the function you're calling resides in, which you can do with the DllImport attribute, like this:

Public Shared Function <DllImport("user32.dll")> MessageBox(ByVal Hwnd

As we need to use various attributes, I'll discuss how they work. You can use an attribute like this: <theAttribute>, or pass its values as parameters as you do to a procedure, like this: <theAttribute("Watch out!")>. Some attributes require the use of named parameters, which you must list specifically when you assign a value to them with the := operator like this: <theAttribute(Warning := "Watch out!")>. We won't be using attributes in these early chapters.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor