JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Objects

You can create objects of a class using the Dim statement; this statement is used at module, class, structure, procedure, or block level:

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

Here are the parts of this statement:

  • 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 can only 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 Shadows, you can omit the ReadOnly 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 which 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.

Each attribute in the attrlist list must use this syntax:

<attrname [({ attrargs | attrinit })]>

Here are the parts of the attrlist list:

  • attrname—Name of the attribute.

  • attrargs—List of arguments for this attribute. Separate multiple arguments with commas.

  • attrinit—List of field or property initializers for this attribute. Separate multiple arguments with commas.

When you create a new object from a class, you use the New keyword. You can do that in either of these ways:

Dim employee As New EmployeeClass()
Dim employee As EmployeeClass = New EmployeeClass()

If you omit the New keyword, you're just declaring a new object, and it's not yet created:

Dim employee As EmployeeClass

Before using the object, you must explicitly create it with New:

employee = New EmployeeClass()

As discussed in the In Depth section of this chapter, classes often use constructors that let you configure objects with data. You write constructors by giving a method the name New, as in the DataClass class from the In Depth section of this chapter:

Public Class DataClass
    Private value As Integer

    Public Sub New(ByVal newValue As Integer)
        value = newValue
    End Sub

    Public Function GetData() As Integer
        Return value
    End Function
End Class

Now I can store the value 5 inside an object of the DataClass class simply by passing 5 to the constructor, and I can retrieve that value with the GetData method:

Dim data As New DataClass(5)
MsgBox(data.GetData())

Related solution:

Found on page:

Storing Objects in a List Box or Combo Box

322

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor