Declaration

1.16 Declaration

Class . . . End Class Statement

 


 
[accessmodifier] [Shadows] [inheritability] Class Name
  statements
End Class

 

accessmodifier optional; Keyword

The possible values of accessmodifier are Public, Private, and Friend

Shadows optional; Keyword

Indicates that the Name class shadows any element of this same name in a base class

inheritability optional; Keyword

One of the keywords, MustInherit or NotInheritable, must be used. MustInherit specifies that objects of this class cannot be created, but that objects of derived classes can be created. NotInheritable specifies that this class cannot be used as a base class

Name required; String literal

The name of the class

Description

Defines a class and delimits the statements that define that class' variables, properties, and methods.

If the Inherits or Implements statements appear in a class module, they must appear before any other statements in the module. Moreover, the Inherits keyword must appear before the Implements keyword.

Within a class code block, members are declared as Public, Private, Protected, Friend, or ProtectedFriend. The Dim keyword is equivalent to Private when used in class modules (but it is equivalent to Public in structures). Property declarations are automatically Public.

The Class...EndClass construct can include the following elements:

Private variable or procedure declarations

These items are accessible within the class but do not have scope outside of the class.

Public variable or procedure declarations

Public variables are public properties of the class; Public procedures are public methods of the class.

Property declarations

These are the public properties of the class. Default properties can be declared by using the Default keyword.

Const Statement

 


[accessmodifier] Const constantname [As type] = constantvalue

 

accessmodifier optional; Keyword

One of the keywords Public, Private, Protected, Friend, or ProtectedFriend.

constantname required; String Literal

The name of the constant.

type optional; Keyword

The data type; it can be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, Date, or String, as well as any of the data types defined in the Base Class Library.

constantvalue required; Numeric or String

A literal, constant, or any combination of literals and constants that includes arithmetic or logical operators, except Is.

Description

Associates a constant value with a name. This feature is provided to make code more readable. The name is referred to as a symbolic constant.

If OptionStrict is on, the data type of the constant must be defined by using the Astype clause.

Declare Statement

 


Syntax for subroutines:
[accessmodifier] Declare [Ansi|Unicode|Auto] Sub name_
  Lib "libname" [Alias "aliasname"] [([arglist])]
 
Syntax for functions:
[accessmodifier] Declare [Ansi|Unicode|Auto] Function name _
  Lib "libname" [Alias "aliasname"] [([arglist])] [As type]

 

accessmodifier optional; Keyword

accessmodifier can be any one of the following: Public, Private, Protected, Friend, or ProtectedFriend. The following table describes the effects of the various access modifiers. Note that direct access refers to accessing the member without any qualification, as in:

classvariable = 100

and class/object access refers to accessing the member through qualification, either with the class name or the name of an object of that class.

 

Direct access scope

Class/objectaccess scope

Private

Declaring class

Declaring class

Protected

All derived classes

Declaring class

Friend

Derived in-project classes

Declaring project

Protected Friend

All derived classes

Declaring project

Public

All derived classes

All projects

Ansi optional; Keyword

Converts all strings to ANSI values.

Unicode optional; Keyword

Converts all strings to Unicode values.

Auto optional; Keyword

Converts the strings according to .NET rules based on the name of the method (or the alias name, if specified). If no modifier is specified, Auto is the default.

name required; String literal

Any valid procedure name. Note that DLL entry points are case sensitive. If the aliasname argument is used, name represents the name by which the function or procedure is referenced in your code, while aliasname represents the name of the routine as found in the DLL.

Lib required; Keyword

Indicates that a DLL or code resource contains the procedure being declared.

libname required; String literal

Name of the DLL or code resource that contains the declared procedure.

Alias optional; Keyword

Indicates that the procedure being called has another name in the DLL. This is useful when the external procedure name is the same as a keyword. You can also use Alias when a DLL procedure has the same name as a public variable, constant, or any other procedure in the same scope. Alias is also useful if any characters in the DLL procedure name aren't allowed by VB.NET naming conventions.

aliasname optional; String literal

Name of the procedure in the DLL or code resource. If the first character is not a number sign (#), aliasname is the name of the procedure's entry point in the DLL. If # is the first character, all characters that follow must indicate the ordinal number of the procedure's entry point.

arglist optional

List of variables representing arguments that are passed to the procedure when it is called.

type optional; Keyword

Data type of the value returned by a Function procedure; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, Date, String, Object, or any user-defined type. Arrays of any type cannot be returned, but an Object containing an array can.

Description

Used at module level to declare references to external procedures in a dynamic-link library (DLL).

The data type you use in the As clause following arglist must match that returned by the function.

Dim Statement

 


 
[Shared] [Shadows] [readonly] Dim_
[WithEvents] varname[([subscripts])]_
[As [New] type] [= initexpr]

 

Shared optional; Keyword

Indicates the variable is not associated with any particular class instance but is accessible directly using the class name and is therefore "shared" by all class instances.

Shadows optional; Keyword

Indicates that the variable shadows any programming elements (variables, procedures, enums, constants, etc.) of the same name in a base class.

WithEvents optional; Keyword

In an object variable definition, indicates that the object will receive event notification

varname required

The name of the variable

subscripts optional

Dimensions of an array variable

New optional; Keyword

Keyword that creates an instance of an object

type optional unless OptionStrict is On

The data type of varname

initexpr optional

Any expression that provides the initial value to assign to the variable; cannot be used if an As New clause is used

Description

Declares and allocates storage space in memory for variables. The Dim statement is used either at the start of a procedure or the start of a module to declare a variable of a particular data type.

Object is the default data type created when no data type is explicitly declared.

The declaration of a nonobject variable actually creates the variable. For an object variable, the variable is not created unless the optional New statement is used. If not, then the object variable is set to Nothing and must be assigned a reference to an existing object at some later point in the code.

Variables that are not explicitly initialized by the Dim statement have the following default values:

Data type

Initial value

All numeric types

0

Boolean

False

Date

01/01/0001 12:00:00 AM

Decimal

0

Object

Nothing

String

Nothing

 

Enum Statement

 


 
accessmodifier Enum name [As type]
  membername [= constantexpression]
  membername [= constantexpression]
  ...
End Enum

 

accessmodifier optional; Keyword

Possible values are Public, Private, Friend, Protected, or Protected Friend

name required; String literal

The name of the enumerated data type

membername required; String literal

The name of a member of the enumerated data type

constantexpression optional; Long

The value to be assigned to membername

type optional; Keyword

The data type of the enumeration. All enumerated members must be integers; possible values are Byte, Short, Integer, and Long

Description

Defines an enumerated data type. All of the values of the data type are defined by the instances of membername.

Friend Keyword

 


 

 

Description

The Friend keyword is used to declare classes, module-level variables (but not local variables), constants, enumerations, properties, methods, functions, and subroutines.

When the Friend keyword is used, the item being declared has direct access scope inside of the class module in which the item is declared, as well as in all derived classes in the same project. However, if the item is declared using Protected Friend, then the scope is all derived classes, including those that are in other projects.

Function Statement

 


 
[ClassBehavior][AccessModifier] Function name _
  [(arglist)] [As type][(  )]
  [statements]
  [name = expression]
  [statements]
End Function

 

ClassBehavior optional; Keyword

One of the following keywords:

Overloads

Indicates that more than one declaration of this function (with different argument signatures) exists.

Overrides

For derived classes, indicates that the function overrides the function by the same name (and argument signature) in the base class.

Overridable

Indicates that the function can be overridden in a derived class.

NotOverridable

Indicates that the function cannot be overridden in a derived class.

MustOverride

Indicates that the function must be overridden in a derived class.

Shadows

In a derived class definition, indicates that this element shadows any elements of the same name in the base class.

Shared

Makes the function callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static functions.

AccessModifier optional; Keyword

One of the following keywords: Public, Private, Protected, Friend, Protected Friend. The following table describes the effects of the various access modifiers. Note that direct access refers to accessing the member without any qualification, as in:

classvariable = 100

and class/object access refers to accessing the member through qualification, either with the class name or the name of an object of that class:

 

Direct access scope

Class/objectaccess scope

Private

Declaring class

Declaring class

Protected

All derived classes

Declaring class

Friend

Derived in-project classes

Declaring project

Protected Friend

All derived classes

Declaring project

Public

All derived classes

All projects

name required; String literal

The name of the function.

arglist optional

A comma-delimited list of variables to be passed to the function as arguments from the calling procedure.

arglist uses the following syntax and parts:

[Optional] [ByVal | ByRef] [ParamArray] varname[( )]_ 
[As type] [= defaultvalue]

Optional optional; Keyword

An optional argument is one that need not be supplied when calling the function. However, all arguments following an optional one must also be optional. A ParamArray argument cannot be optional.

ByVal optional; Keyword

The argument is passed by value; that is, the local copy of the variable is assigned the value of the argument. ByVal is the default method of passing variables.

ByRef optional; Keyword

The argument is passed by reference; that is, the local variable is simply a reference to the argument being passed. All changes made to the local variable will be also reflected in the calling argument.

ParamArray optional; Keyword

Indicates that the argument is an optional array of Objects (or a strongly typed array, if Option Strict is on) containing an arbitrary number of elements. It can only be used as the last element of the argument list and cannot be used with the ByRef or Optional keywords.

varname required; String literal

The name of the local variable containing either the reference or value of the argument.

type optional; Keyword

The data type of the argument.

defaultvalue optional; String literal

For optional arguments, you must specify a default value.

type optional; Keyword

The return data type of the function.

statements optional

Program code to be executed within the function.

expression optional

The value to return from the function to the calling procedure.

Description

Defines a function procedure.

If you do not include one of the access keywords, a function will be Public by default.

Option Explicit Statement

 


 
 
Option Explicit [On | Off]

 

Description

Use Option Explicit to generate a compile-time error whenever a variable that has not been declared is encountered.

In modules where the Option Explicit statement is not used, any undeclared variables are automatically cast as Objects.

The default is Option Explicit On. In other words, the statement:

Option Explicit

is equivalent to:

Option Explicit On

Private Statement

 


 
 
Private [WithEvents] varname[([subscripts])] [As [New] type] _
[, [WithEvents] varname[([subscripts])] [As [New] type]] ...

 

WithEvents optional; Keyword

A keyword that denotes the object variable, varname, can respond to events triggered from within the object to which it refers

varname required; any

The name of the variable, following Visual Basic naming conventions

subscripts optional; Integer or Long

Denotes varname as an array and specifies the number and extent of array dimensions

New optional; Keyword

Used to automatically create an instance of the object referred to by the varname object variable

type optional; Keyword

Data type of the variable varname

Description

Used at module level to declare a private variable and allocate the relevant storage space in memory. Private can also be used with procedures and class modules.

Property Statement

 


[Default] [accessmodifier] [ReadOnly| WriteOnly] _
  [ClassBehavior] Property name _
  [(arglist)] [As type] [Implements interfacemember]
  Get
  [statements]
  End Get
  Set
  [statements]
  End Set
End Property

 

Default optional; Keyword

Specifies that the property is the default property. Must have both a Get and a Set block.

accessmodifier optional; Keyword

One of the keywords Public, Private, Protected, Friend, or Protected Friend.

ReadOnly optional; Keyword

Indicates that the property is read-only. Must have only a Get block. (If you try to write a Set block, VB will generate a compiler error.)

WriteOnly optional; Keyword

Indicates that the property is write-only. Must have only a Set block. (If you try to write a Get block, VB will generate a compiler error.)

ClassBehavior optional; Keyword

One of the following keywords:

Overloads

Indicates that more than one declaration of this function (with different argument signatures) exists.

Overrides

For derived classes, indicates that the function overrides the function by the same name (and argument signature) in the base class.

Overridable

Indicates that the function can be overridden in a derived class.

NotOverridable

Indicates that the function cannot be overridden in a derived class.

MustOverride

Indicates that the function must be overridden in a derived class.

Shadows optional; Keyword

Indicates that the property shadows any element of this same name in a base class.

Shared

Makes the function callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static functions.

name required; String literal

The name of the property.

arglist optional; any

A comma-delimited list of variables to be passed to the property as arguments from the calling procedure.

type optional

The return data type of the property. The default is Object.

Implementsinterfacename optional

Indicates that the property implements a property by the same name in the interface named interfacename.

Description

Declares a class property.

Overloads and Shadows cannot be used in the same property declaration.

Property procedures are Public by default.

The Friend keyword is only valid within class modules. Friend properties are accessible to all procedures in all modules and classes within a project, but are not listed in the class library for that project. Therefore, they cannot be accessed from projects or applications outside the defining application.

Properties and procedures defined using the Friend keyword cannot be late bound.

The Default keyword can be used only in the case of parameterized properties. Typically, these are properties that either return collection objects or are implemented as property arrays.

Protected Keyword

 


 

 

Description

Used to declare classes and their members.

When the Protected keyword is used to modify a member declaration, the member being declared has direct access scope to the class module in which the member is declared, as well as to all derived classes in all projects. However, as far as object access is concerned, the member is considered Private; that is, it can only be accessed within the declaring class.

Declaring a class module as Protected limits all of the class' members to Protected access (or stronger if the member has further specific access restrictions).

Public Statement

 


 
[Overrides] [Shadows] Public [WithEvents] _ 
 varname[([subscripts])] [As [New] type] [, [WithEvents] _
  varname[([subscripts])] [As [New] type]] ...

 

Overrides optional; Keyword

In a derived class definition, indicates that a variable overrides a similar variable in a base class

Shadows optional; Keyword

In a derived class definition, indicates that calls to derived class members that are made through a base class ignore the shadowed implementation

WithEvents optional; Keyword

A keyword that denotes the object variable, varname, can respond to events triggered from within the object to which it refers

varname required; String literal

The name of the variable, which must follow Visual Basic naming conventions

subscripts optional; Numeric constant or literal

Denotes varname as an array and specifies the dimensions and number of elements of the array

New optional; Keyword

Used to automatically create an instance of the object referred to by the varname object variable

type optional

Data type of the variable varname

Description

Used at module level to declare a public variable and allocate the relevant storage space in memory.

A Public variable has both project-level scope that is, it can be used by all procedures in all modules in the project and, when used in a Class module, it can have scope outside the project.

The Public keyword also applies to procedures and class modules.

The behavior of a Public variable depends on where it is declared, as the following table shows:

Variable declared in . . .

Scope

A procedure

Illegal this generates a compile-time error.

Code module declarations section

Variable is available to all modules within the project.

Class module declarations section

Variable is available as a property of the class to all modules within the project and to all other projects referencing the class.

Form module declarations section

Variable is available as a property of the form to all modules within the project.

 

Static Statement

 


 
 
 
Static varname[([subscripts])] [As [New] type] _
  [,varname[([subscripts])] [As [New] type]] ...

 

varname required; any

The name of the variable, following Visual Basic naming conventions

subscripts optional; Integer

Denotes varname as an array and specifies the dimension and upper bounds of the array

New optional; Keyword

Used to automatically create an instance of the object referred to by the object variable varname

type optional; Keyword

Data type of the variable varname

Description

Used at procedure level to declare a Static variable and to allocate the relevant storage space in memory. Static variables retain their value between calls to the procedure in which they are declared.

Structure...End Structure Statement

 


 
accessmodifier Structure StructureName
  [Implements interfacenames]
  variable declarations
  procedure declarations
End Structure

 

accessmodifier optional; Keyword

The possible values of accessmodifier are Public, Private, Friend, Protected, Protected Friend.

Implementsinterfacenames optional

Indicates that the structure implements the members of one or more interfaces

Description

Used to declare user-defined types. Structures are similar to classes, but they are value types rather than reference types.

Sub Statement

 


[ClassBehavior] [AccessModifier] Sub name [(arglist)]
  [statements]
  [Exit Sub]
  [statements]
End Sub

 

ClassBehavior optional; Keyword

One of the keywords shown in the following table:

Keyword

Description

Overloads

Indicates that more than one declaration of this subroutine exists (with different argument signatures).

Overrides

For derived classes, indicates that the subroutine overrides the subroutine by the same name (and argument signature) in the base class.

Overridable

Indicates that the subroutine can be overridden in a derived class.

NotOverridable

Indicates that the subroutine cannot be overridden in a derived class.

MustOverride

Indicates that the subroutine must be overridden in a derived class.

Shadows

In a derived class definition, indicates that calls to derived class members that are made through a base class ignore the shadowed implementation.

Shared

Callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static subroutines.

AccessModifier optional

Possible values are Public, Private, Friend, Protected, or Protected Friend. The following table describes the effects of the various access modifiers. Note that "direct access" refers to accessing the member without any qualification, as in:

classvariable = 100

and "class/object access" refers to accessing the member through qualification, either with the class name or the name of an object of that class.

 

Direct access scope

Class/objectaccess scope

Private

Declaring class

Declaring class

Protected

All derived classes

Declaring class

Friend

Derived in-project classes

Declaring project

Protected Friend

All derived classes

Declaring project

Public

All derived classes

All projects

name required; String literal

The name of the Sub procedure.

arglist optional; any

A comma-delimited list of variables to be passed to the sub procedure as arguments from the calling procedure.

arglist uses the following syntax and parts:

[Optional] [ByVal | ByRef] [ParamArray] varname[( )] _
  [As type] [= defaultvalue]

Optional optional; Keyword

An optional argument is one that need not be supplied when calling the function. However, all arguments following an optional one must also be optional. A ParamArray argument cannot be optional.

ByVal optional; Keyword

The argument is passed by value; that is, the local copy of the variable is assigned the value of the argument. ByVal is the default method of passing variables.

ByRef optional; Keyword

The argument is passed by reference; that is, the local variable is simply a reference to the argument being passed. All changes made to the local variable will be reflected in the calling argument.

ParamArray optional; Keyword

Indicates that the argument is an optional array containing an arbitrary number of elements. It can only be used as the last element of the argument list, and cannot be modified by either the ByRef or Optional keywords. If Option Strict is on, the array type must also be specified.

varname required; String literal

The name of the local variable containing either the reference or value of the argument.

type optional; Keyword

The data type of the argument. It can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, String, a user- defined type, or an object type.

defaultvalue optional; any

For optional arguments, you must specify a default value.

statements optional

Program code to be executed within the procedure.

Description

Defines a subroutine.

If you do not include one of the accessmodifier keywords, a subroutine will be Public by default.

 



VB. NET Language Pocket Reference
VB.NET Language Pocket Reference
ISBN: 0596004281
EAN: 2147483647
Year: 2002
Pages: 31

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