Private Statement

   
Private Statement

Syntax

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

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.

Rules at a Glance

  • A Private variable's scope is limited to the module in which it is created.

  • WithEvents is only valid when used to declare an object variable. The WithEvents keyword informs VB that the object being referenced exposes events. When you declare an object variable using WithEvents , an entry for the object variable is placed in the code window's Object List, and a list of the events available to the object variable is placed in its Procedures List. You can then write code in the object variable's event handlers in the same way you write other more common event handlers.

  • There is no limit to the number of object 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 . This is because WithEvents is designed to trap event notifications that would ordinarily be inaccessible to a Visual Basic program. Consequently, WithEvents can only be used when defining an instance of an existing object.

  • The subscripts argument has the following syntax:

       upperbound   [,   upperbound   ] 

    For example:

     Private strNames(10, 15) 

    defines a two-dimensional array with 11 elements in the first coordinate and 16 elements in the second coordinate. Thus, the first element is strNames(0,0) , and the last element is strNames(10,15) .

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

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

     Private sNames(  ) Private sThings(,) 

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

  • The New keyword is used only when declaring an object variable. For example:

     Private oEmployee As Employee oEmployee = New Employee 

    or:

     Private oEmployee As New Employee 
  • The New keyword can only be used with early-bound objects.

  • datatype may be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, String, a user -defined type, or an object type.

Programming Tips and Gotchas

  • All variables created at procedure level are Private by default. That is, they do not have scope outside of the procedure in which they are created.

  • A new type of scope was introduced in Visual Basic 5.0. The Friend scope is halfway between Public and Private . It is useful in situations where Private is too restricting and Public is too open . For more information, refer to the Friend Keyword entry.

  • You should note that when you use the New keyword to declare an object variable, its class constructor is fired when the object variable is declared.

  • The WithEvents keyword cannot be used with local variables whose scope is limited to a function or procedure.

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 Private 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:

     Private 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