Online Book Reader

Home Category

Access Cookbook - Ken Getz [319]

By Root 2092 0
event handler within a Visual Basic 6 class. .NET, however, allows you to create constructors that can accept parameters, so-called parameterized constructors . COM, however, has no way to call a class containing a parameterized constructor. If you attempt to create an object from a .NET class that contains a parameterized constructor, you will get a runtime error. A workaround for this issue is presented in topic 17.2.

Another limitation of calling .NET components from Access is that you won't be able to access any properties, methods, or events marked as static (also know as shared). A static member of a .NET class is a member that applies across all instances of a class. Static members cannot be called from Access or other COM programs.

See Also


Microsoft Office and .NET Interoperability (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office11012001.asp).

17.2. Call a .NET Component Containing a Parameterized Constructor


Problem


Attempting to call a .NET class containing a parameterized constructor generates the compile error "Invalid use of New keyword". Is there some sort of workaround so that I can call a .NET class containing a parameterized constructor?

Solution


To see the problem, you will need to follow these steps:

Start a Visual Studio .NET command prompt and run the RegAsm utility program on the Geometry.dll file found in the Geometry\bin folder of this chapter's sample files using the following syntax (see topic 17.1 for more details on the Visual Studio .NET command prompt and running the RegAsm utility):

regasm Geometry.dll /tlb: Geometry.tlb /codebase

RegAsm will display a warning about this being an unsigned assembly but you can safely ignore the warning.

Load the 17-02.MDB database and open the frmCircleDirect form in Design view.

From the VBA IDE, select Tools → References. At the References dialog, select the Geometry component.

Close and save the form.

Open frmCircleDirect in form view. Enter a numeric value into the Radius textbox and click the Calculate button. Access should respond with the error shown in Figure 17-3.

Figure 17-3. This compile error is triggered when you attempt to instantiate a .NET class containing a parameterized constructor

Click OK. Select Run → Reset and close the form.

The Circle class is shown here:

Public Class Circle

' NOTE: This class contains a

' parameterized constructor which

' prevents it from being called

' by a COM program.

Private RadiusVal As Double

Public Sub New(ByVal Radius As Double)

' This constructor takes a parameter

RadiusVal = Radius

End Sub

Public Property Radius( ) As Double

Get

Return RadiusVal

End Get

Set(ByVal Value As Double)

RadiusVal = Value

End Set

End Property

Public Function Area( ) As Double

Return Radius ^ 2 * System.Math.PI

End Function

Public Function Circumference( ) As Double

Return 2 * Radius * System.Math.PI

End Function

End Class

This class is inaccessible from Access because its constructor (the New subroutine) contains a parameter. The trick to being able to call the inaccessible class from Access is to create a helper class that you can use to call the unavailable class. To create a helper class that you can use to call the Circle class, follow these steps:

Exit Access completely. This is necessary because otherwise Access may place a lock on the existing Geometry.tlb type library.

Start Visual Studio .NET and load the Geometry project.

Open Geometry.vb and add the following class after the Circle class's End Class statement:

Public Class CircleCOM

Inherits Circle

Sub New( )

' Call base class' constructor

' with dummy radius value.

MyBase.New(1)

End Sub

End Class

Notice that the new class, CircleCOM, inherits from the original inaccessible Circle class.

Compile the project by selecting Build → Build Solution.

Start a Visual Studio .NET command prompt and run the RegAsm utility program on the updated Geometry.dll file found in the Visual Studio Projects\Geometry\bin folder

Return Main Page Previous Page Next Page

®Online Book Reader