Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The VeryTime,Stay informed and read the latest news today from The VeryTime, the definitive source.

.NET - COM Interoperability in Visual Basic

34
< Continued from page 1

To illustrate the use of Linq, I decided to use the same VB.NET code that was developed for the Visual Basic .NET 2008 Express tutorial. This example uses 'Linq to XML' to read an XML 'Signature Block' file containing information like name, email address, and so forth. Since the goal is entirely different, however, only the code that directly uses Linq was used in the example here.

To begin the process, start a new Class Library project in VB.NET.


(Again, I used the totally free VB.NET 2008 Express.)

The first thing you need in your new class is an Interface. This defines the members that our class will make available to our VB6 program. (By tradition, interface names always begin with "i".)

Public Interface iAVBNetLinq
   ReadOnly Property FullName() As String
   Function GetFullName() As String
End Interface


The class that will be written next will have to implement both a FullName property and a GetFullName() method. (Design note: In a real application, it's likely that only one of these would be implemented because they both do the same thing. But this is an example.)

We start the class code by declaring that it will implement the interface:

Public Class AVBNETLinq
   Implements iAVBNetLinq


The property will need a member variable:

Private _FullName As String

And because the property is ReadOnly, a 'getter' is all we need in the class code that implements the FullName property in the interface.

Private _FullNameInit As Boolean = False
Public ReadOnly Property FullName() As String _
   Implements iAVBNetLinq.FullName
   Get
      If _FullNameInit = False Then _
         GetFullName()
      FullName = _FullName
   End Get
End Property


(Design Note: Because the FullName property and GetFullName method both do about the same thing, I added an initialization flag to avoid unnecessary execution of the code to get the name information.)

The code to actually use Linq to access the XML data file is in the method GetFullName(). Consult the Visual Basic .NET 2008 Express tutorial for more on how Linq works.

Private Const SigDocument As String = "SigBlockData.xml"
Private mySigBlockStruct As New SigBlockStruct
Public Function GetFullName() As String _
   Implements iAVBNetLinq.GetFullName
   MsgBox("GetFullName - _FullNameInit is " & _FullNameInit.ToString)
   If _FullNameInit = False Then
      Dim mySigBlockXML As XElement = XElement.Load(SigDocument)
      Dim myFName = From FName In mySigBlockXML...<SigBlock> _
         Select FName.<FName>.Value
      Dim myLName = From LName In mySigBlockXML...<SigBlock> _
         Select LName.<LName>.Value
      _FullName = myFName.First & " " & myLName.First
      GetFullName = _FullName
      _FullNameInit = True
   Else
      GetFullName = _FullName
   End If
End Function


And this is the entire class! One more thing needs to be done. Open the project properties and in the Application -> Assembly Information ... window, click the Make Assembly COM Visible checkbox on. (In the first version of VB.NET, this was identified as the Register For COM Interop checkbox in Configuration Properties. You might see that in older documentation.)

Now Build the project. This will create the DLL we need and we're ready for the next step. We start into that on the next page.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.