Sideway from Sideway
Draft for Information Only


VB.NET Assignment  Statements
  AddHandler Statement
  See also
  Declare Statement
  Data Type Rules
  See also
  Enum Statement
  Data Type
  Initializing Members
  Assigning Multiple Values
  See also
  Erase Statement
  See also
  RemoveHandler Statement
  See also
  Set Statement
  See also

VB.NET Assignment  Statements

The supporting VB.NET Assignment Statements are AddHandler, Declare, Enum, Erase,  RemoveHandler, Set,      =

AddHandler Statement

Associates an event with an event handler at run time.


AddHandler event, AddressOf eventhandler  


event: The name of the event to handle.

eventhandler: The name of a procedure that handles the event.


The AddHandler and RemoveHandler statements allow you to start and stop event handling at any time during program execution.

The signature of the eventhandler procedure must match the signature of the event event.

The Handles keyword and the AddHandler statement both allow you to specify that particular procedures handle particular events, but there are differences. The AddHandler statement connects procedures to events at run time. Use the Handles keyword when defining a procedure to specify that it handles a particular event. For more information, see Handles.


For custom events, the AddHandler statement invokes the event's AddHandler accessor. For more information on custom events, see Event Statement.

See also

Declare Statement

Declares a reference to a procedure implemented in an external file.


[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ Overloads ] _
Declare [ charsetmodifier ] [ Sub ] name Lib "libname" _
[ Alias "aliasname" ] [ ([ parameterlist ]) ]
' -or-
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ Overloads ] _
Declare [ charsetmodifier ] [ Function ] name Lib "libname" _
[ Alias "aliasname" ] [ ([ parameterlist ]) ] [ As returntype ]


Term Definition
attributelist Optional. See Attribute List.
accessmodifier Optional. Can be one of the following:

- Public
- Protected
- Friend
- Private
- Protected Friend
- Private Protected

See Access levels in Visual Basic.
Shadows Optional. See Shadows.
charsetmodifier Optional. Specifies character set and file search information. Can be one of the following:

- Ansi (default)
- Unicode
- Auto
Sub Optional, but either Sub or Function must appear. Indicates that the external procedure does not return a value.
Function Optional, but either Sub or Function must appear. Indicates that the external procedure returns a value.
name Required. Name of this external reference. For more information, see Declared Element Names.
Lib Required. Introduces a Lib clause, which identifies the external file (DLL or code resource) that contains an external procedure.
libname Required. Name of the file that contains the declared procedure.
Alias Optional. Indicates that the procedure being declared cannot be identified within its file by the name specified in name. You specify its identification in aliasname.
aliasname Required if you use the Alias keyword. String that identifies the procedure in one of two ways:

The entry point name of the procedure within its file, within quotes ("")


A number sign (#) followed by an integer specifying the ordinal number of the procedure's entry point within its file
parameterlist Required if the procedure takes parameters. See Parameter List.
returntype Required if Function is specified and Option Strict is On. Data type of the value returned by the procedure.


Sometimes you need to call a procedure defined in a file (such as a DLL or code resource) outside your project. When you do this, the Visual Basic compiler does not have access to the information it needs to call the procedure correctly, such as where the procedure is located, how it is identified, its calling sequence and return type, and the string character set it uses. The Declare statement creates a reference to an external procedure and supplies this necessary information.

You can use Declare only at module level. This means the declaration context for an external reference must be a class, structure, or module, and cannot be a source file, namespace, interface, procedure, or block. For more information, see Declaration Contexts and Default Access Levels.

External references default to Public access. You can adjust their access levels with the access modifiers.


  • Attributes. You can apply attributes to an external reference. Any attribute you apply has effect only in your project, not in the external file.

  • Modifiers. External procedures are implicitly Shared. You cannot use the Shared keyword when declaring an external reference, and you cannot alter its shared status.

    An external procedure cannot participate in overriding, implement interface members, or handle events. Accordingly, you cannot use the Overrides, Overridable, NotOverridable, MustOverride, Implements, or Handles keyword in a Declare statement.

  • External Procedure Name. You do not have to give this external reference the same name (in name) as the procedure's entry-point name within its external file (aliasname). You can use an Alias clause to specify the entry-point name. This can be useful if the external procedure has the same name as a Visual Basic reserved modifier or a variable, procedure, or any other programming element in the same scope.


    Entry-point names in most DLLs are case-sensitive.

  • External Procedure Number. Alternatively, you can use an Alias clause to specify the ordinal number of the entry point within the export table of the external file. To do this, you begin aliasname with a number sign (#). This can be useful if any character in the external procedure name is not allowed in Visual Basic, or if the external file exports the procedure without a name.

Data Type Rules

  • Parameter Data Types. If Option Strict is On, you must specify the data type of each parameter in parameterlist. This can be any data type or the name of an enumeration, structure, class, or interface. Within parameterlist, you use an As clause to specify the data type of the argument to be passed to each parameter.


    If the external procedure was not written for the .NET Framework, you must take care that the data types correspond. For example, if you declare an external reference to a Visual Basic 6.0 procedure with an Integer parameter (16 bits in Visual Basic 6.0), you must identify the corresponding argument as Short in the Declare statement, because that is the 16-bit integer type in Visual Basic. Similarly, Long has a different data width in Visual Basic 6.0, and Date is implemented differently.

  • Return Data Type. If the external procedure is a Function and Option Strict is On, you must specify the data type of the value returned to the calling code. This can be any data type or the name of an enumeration, structure, class, or interface.


    The Visual Basic compiler does not verify that your data types are compatible with those of the external procedure. If there is a mismatch, the common language runtime generates a MarshalDirectiveException exception at run time.

  • Default Data Types. If Option Strict is Off and you do not specify the data type of a parameter in parameterlist, the Visual Basic compiler converts the corresponding argument to the Object Data Type. Similarly, if you do not specify returntype, the compiler takes the return data type to be Object.


    Because you are dealing with an external procedure that might have been written on a different platform, it is dangerous to make any assumptions about data types or to allow them to default. It is much safer to specify the data type of every parameter and of the return value, if any. This also improves the readability of your code.


  • Scope. An external reference is in scope throughout its class, structure, or module.

  • Lifetime. An external reference has the same lifetime as the class, structure, or module in which it is declared.

  • Calling an External Procedure. You call an external procedure the same way you call a Function or Sub procedure—by using it in an expression if it returns a value, or by specifying it in a Call Statement if it does not return a value.

    You pass arguments to the external procedure exactly as specified by parameterlist in the Declare statement. Do not take into account how the parameters were originally declared in the external file. Similarly, if there is a return value, use it exactly as specified by returntype in the Declare statement.

  • Character Sets. You can specify in charsetmodifier how Visual Basic should marshal strings when it calls the external procedure. The Ansi modifier directs Visual Basic to marshal all strings to ANSI values, and the Unicode modifier directs it to marshal all strings to Unicode values. The Auto modifier directs Visual Basic to marshal strings according to .NET Framework rules based on the external reference name, or aliasname if specified. The default value is Ansi.

    charsetmodifier also specifies how Visual Basic should look up the external procedure within its external file. Ansi and Unicode both direct Visual Basic to look it up without modifying its name during the search. Auto directs Visual Basic to determine the base character set of the run-time platform and possibly modify the external procedure name, as follows:

    • On an ANSI platform, such as Windows 95, Windows 98, or Windows Millennium Edition, first look up the external procedure with no name modification. If that fails, append "A" to the end of the external procedure name and look it up again.

    • On a Unicode platform, such as Windows NT, Windows 2000, or Windows XP, first look up the external procedure with no name modification. If that fails, append "W" to the end of the external procedure name and look it up again.

  • Mechanism. Visual Basic uses the .NET Framework platform invoke (PInvoke) mechanism to resolve and access external procedures. The Declare statement and the DllImportAttribute class both use this mechanism automatically, and you do not need any knowledge of PInvoke. For more information, see Walkthrough: Calling Windows APIs.


If the external procedure runs outside the common language runtime (CLR), it is unmanaged code. When you call such a procedure, for example a Windows API function or a COM method, you might expose your application to security risks. For more information, see Secure Coding Guidelines for Unmanaged Code.

See also

Enum Statement

Declares an enumeration and defines the values of its members.


[ <attributelist> ] [ accessmodifier ]  [ Shadows ]
Enum enumerationname [ As datatype ]
End Enum


  • attributelist

    Optional. List of attributes that apply to this enumeration. You must enclose the attribute list in angle brackets ("<" and ">").

    The FlagsAttribute attribute indicates that the value of an instance of the enumeration can include multiple enumeration members, and that each member represents a bit field in the enumeration value.

  • accessmodifier

    Optional. Specifies what code can access this enumeration. Can be one of the following:

  • Shadows

    Optional. Specifies that this enumeration redeclares and hides an identically named programming element, or set of overloaded elements, in a base class. You can specify Shadows only on the enumeration itself, not on any of its members.

  • enumerationname

    Required. Name of the enumeration. For information on valid names, see Declared Element Names.

  • datatype

    Optional. Data type of the enumeration and all its members.

  • memberlist

    Required. List of member constants being declared in this statement. Multiple members appear on individual source code lines.

    Each member has the following syntax and parts: [<attribute list>] member name [ = initializer ]

    Part Description
    membername Required. Name of this member.
    initializer Optional. Expression that is evaluated at compile time and assigned to this member.
  • End Enum

    Terminates the Enum block.


If you have a set of unchanging values that are logically related to each other, you can define them together in an enumeration. This provides meaningful names for the enumeration and its members, which are easier to remember than their values. You can then use the enumeration members in many places in your code.

The benefits of using enumerations include the following:

  • Reduces errors caused by transposing or mistyping numbers.

  • Makes it easy to change values in the future.

  • Makes code easier to read, which means it is less likely that errors will be introduced.

  • Ensures forward compatibility. If you use enumerations, your code is less likely to fail if in the future someone changes the values corresponding to the member names.

An enumeration has a name, an underlying data type, and a set of members. Each member represents a constant.

An enumeration declared at class, structure, module, or interface level, outside any procedure, is a member enumeration. It is a member of the class, structure, module, or interface that declares it.

Member enumerations can be accessed from anywhere within their class, structure, module, or interface. Code outside a class, structure, or module must qualify a member enumeration's name with the name of that class, structure, or module. You can avoid the need to use fully qualified names by adding an Imports statement to the source file.

An enumeration declared at namespace level, outside any class, structure, module, or interface, is a member of the namespace in which it appears.

The declaration context for an enumeration must be a source file, namespace, class, structure, module, or interface, and cannot be a procedure. For more information, see Declaration Contexts and Default Access Levels.

You can apply attributes to an enumeration as a whole, but not to its members individually. An attribute contributes information to the assembly's metadata.

Data Type

The Enum statement can declare the data type of an enumeration. Each member takes the enumeration's data type. You can specify Byte, Integer, Long, SByte, Short, UInteger, ULong, or UShort.

If you do not specify datatype for the enumeration, each member takes the data type of its initializer. If you specify both datatype and initializer, the data type of initializer must be convertible to datatype. If neither datatype nor initializer is present, the data type defaults to Integer.

Initializing Members

The Enum statement can initialize the contents of selected members in memberlist. You use initializer to supply an expression to be assigned to the member.

If you do not specify initializer for a member, Visual Basic initializes it either to zero (if it is the first member in memberlist), or to a value greater by one than that of the immediately preceding member.

The expression supplied in each initializer can be any combination of literals, other constants that are already defined, and enumeration members that are already defined, including a previous member of this enumeration. You can use arithmetic and logical operators to combine such elements.

You cannot use variables or functions in initializer. However, you can use conversion keywords such as CByte and CShort. You can also use AscW if you call it with a constant String or Char argument, since that can be evaluated at compile time.

Enumerations cannot have floating-point values. If a member is assigned a floating-point value and Option Strict is set to on, a compiler error occurs. If Option Strict is off, the value is automatically converted to the Enum type.

If the value of a member exceeds the allowable range for the underlying data type, or if you initialize any member to the maximum value allowed by the underlying data type, the compiler reports an error.


Class, structure, module, and interface member enumerations default to public access. You can adjust their access levels with the access modifiers. Namespace member enumerations default to friend access. You can adjust their access levels to public, but not to private or protected. For more information, see Access levels in Visual Basic.

All enumeration members have public access, and you cannot use any access modifiers on them. However, if the enumeration itself has a more restricted access level, the specified enumeration access level takes precedence.

By default, all enumerations are types and their fields are constants. Therefore the Shared, Static, and ReadOnly keywords cannot be used when declaring an enumeration or its members.

Assigning Multiple Values

Enumerations typically represent mutually exclusive values. By including the FlagsAttribute attribute in the Enum declaration, you can instead assign multiple values to an instance of the enumeration. The FlagsAttribute attribute specifies that the enumeration be treated as a bit field, that is, a set of flags. These are called bitwise enumerations.

When you declare an enumeration by using the FlagsAttribute attribute, we recommend that you use powers of 2, that is, 1, 2, 4, 8, 16, and so on, for the values. We also recommend that "None" be the name of a member whose value is 0. For additional guidelines, see FlagsAttribute and Enum.

See also

Erase Statement

Used to release array variables and deallocate the memory used for their elements.


Erase arraylist  


Required. List of array variables to be erased. Multiple variables are separated by commas.


The Erase statement can appear only at procedure level. This means you can release arrays inside a procedure but not at class or module level.

The Erase statement is equivalent to assigning Nothing to each array variable.

See also

RemoveHandler Statement

Removes the association between an event and an event handler.


RemoveHandler event, AddressOf eventhandler  


Term Definition
event The name of the event being handled.
eventhandler The name of the procedure currently handling the event.


The AddHandler and RemoveHandler statements allow you to start and stop event handling for a specific event at any time during program execution.


For custom events, the RemoveHandler statement invokes the event's RemoveHandler accessor. For more information on custom events, see Event Statement.

See also

Set Statement

Declares a Set property procedure used to assign a value to a property.


[ <attributelist> ] [ accessmodifier ] Set (ByVal value [ As datatype ])  
    [ statements ]  
End Set  


Optional. See Attribute List.

Optional on at most one of the Get and Set statements in this property. Can be one of the following:

See Access levels in Visual Basic.

Required. Parameter containing the new value for the property.

Required if Option Strict is On. Data type of the value parameter. The data type specified must be the same as the data type of the property where this Set statement is declared.

Optional. One or more statements that run when the Set property procedure is called.

End Set
Required. Terminates the definition of the Set property procedure.


Every property must have a Set property procedure unless the property is marked ReadOnly. The Set procedure is used to set the value of the property.

Visual Basic automatically calls a property's Set procedure when an assignment statement provides a value to be stored in the property.

Visual Basic passes a parameter to the Set procedure during property assignments. If you do not supply a parameter for Set, the integrated development environment (IDE) uses an implicit parameter named value. The parameter holds the value to be assigned to the property. You typically store this value in a private local variable and return it whenever the Get procedure is called.

The body of the property declaration can contain only the property's Get and Set procedures between the Property Statement and the End Property statement. It cannot store anything other than those procedures. In particular, it cannot store the property's current value. You must store this value outside the property, because if you store it inside either of the property procedures, the other property procedure cannot access it. The usual approach is to store the value in a Private variable declared at the same level as the property. You must define a Set procedure inside the property to which it applies.

The Set procedure defaults to the access level of its containing property unless you use accessmodifier in the Set statement.


  • Mixed Access Levels. If you are defining a read-write property, you can optionally specify a different access level for either the Get or the Set procedure, but not both. If you do this, the procedure access level must be more restrictive than the property's access level. For example, if the property is declared Friend, you can declare the Set procedure Private, but not Public.

    If you are defining a WriteOnly property, the Set procedure represents the entire property. You cannot declare a different access level for Set, because that would set two access levels for the property.


  • Returning from a Property Procedure. When the Set procedure returns to the calling code, execution continues following the statement that provided the value to be stored.

    Set property procedures can return using either the Return Statement or the Exit Statement.

    The Exit Property and Return statements cause an immediate exit from a property procedure. Any number of Exit Property and Return statements can appear anywhere in the procedure, and you can mix Exit Property and Return statements.

See also




ID: 200800009 Last Updated: 9/8/2020 Revision: 0 Ref:



  1. Active Server Pages,  ,
  2. ASP Overview,  ,
  3. ASP Best Practices,  ,
  4. ASP Built-in Objects,  ,
  5. Response Object,  ,
  6. Request Object,  ,
  7. Server Object (IIS),  ,
  8. Application Object (IIS),  ,
  9. Session Object (IIS),  ,
  10. ASPError Object,  ,
  11. ObjectContext Object (IIS),  ,
  12. Debugging Global.asa Files,  ,
  13. How to: Debug Global.asa files,  ,
  14. Calling COM Components from ASP Pages,  ,
  15. IIS ASP Scripting Reference,  ,
  16. ASP Keywords,  ,
  17. Creating Simple ASP Pages,  ,
  18. Including Files in ASP Applications,  ,
  19. ASP Overview,  ,
  20. FileSystemObject Object,  ,
  21.,  , ADO Object Model
  22. ADO Fundamentals,  ,

Home 5






Hobbies 8


Chinese 1097

English 337

Reference 67


Hardware 149


Application 198

Digitization 25

Latex 7

Manim 115

Numeric 19


Web 283

Unicode 494


CSS 58


OS 389

DeskTop 7

Python 19



Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2


Electric 27

Biology 1

Geography 1

Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019