Public Statement

   
Public Statement

Syntax

 [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 object variable, varname

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.

Rules at a Glance

  • 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.

  • WithEvents is only valid when used to declare an object variable.

  • There is no limit to the number of variables that can refer to the same object using the WithEvents keyword; they will all respond to that object's events.

  • You cannot create an array variable that uses the WithEvents keyword.

  • The New keyword cannot be used in the same object-variable declaration as WithEvents .

  • The subscripts argument has the following syntax:

       upperbound   [,   upperbound   ] 
  • Using the subscripts argument, you can declare up to 60 dimensions for the array.

  • To declare an array with no specified size , use commas with no integers between them, as in:

     Public sNames(  ) Public sThings(,) 

    You can set or change a number of elements of an array using the ReDim statement.

  • The New keyword denotes that a new instance of the object will be created when the first reference to the object is made. Use of the New keyword therefore negates the need to use the Set statement.

  • You cannot use the New keyword to declare any of the following: variables of any intrinsic data type (the New keyword is for use with object variables only); instances of dependent objects (a dependant object is one that can only be created from a method or property in another object; a dependent object is not publicly createable ); or a variable that uses the WithEvents argument.

Programming Tips and Gotchas

  • Instead of declaring a variable as Public within either a form or class module, proper object-oriented programming techniques dictate that you should create a Property procedure that assigns and retrieves the value of a Private variable.

  • Always use Option Explicit at the beginning of a module to prevent misnamed variables from causing hard to find errors.

VB.NET/VB 6 Differences

  • In VB 6, the subscripts argument takes the form:

     [   lowerbound   To]   upperbound   [, [   lowerbound   To]   upperbound   ] 

    VB.NET, however, does not allow you to set the lower bound of an array.

  • In VB 6, an array whose number of elements are declared in advance is a fixed array; it cannot be redimensioned. In VB.NET, all arrays are dynamic and can be redimensioned.

  • In VB.NET, variables declared with the New keyword on the same line as the Public statement are no longer created when their first reference is encountered . Hence, whereas in VB 6, declaring an object variable using a statement such as:

     Public oObj As New MyApp.SomeObject 

    could interfere with object destruction, in VB.NET this is not the case.

  • In VB 6, the type argument can be Currency . The Currency data type, however, is not supported by VB.NET.

See Also

Friend Keyword, Protected Keyword, Public Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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