Click or drag to resize

Understanding the programming model

The programming model of the .NET SDK is very similar to the Meridian Enterprise COM API. In the most cases, to migrate your Visual Basic 6 code, replace the “AM” prefix of the class name with “BC” (for example, replace AMDocument with BCDocument). The following example lists the same code in Visual Basic 6 and Visual Basic .NET.

Original Visual Basic 6 code
Dim vaultLink As New AmVaultLinkDim dr As AMDocumentRepository
Dim fso As IAMFSObject
Dim doc As IAMDocument
Dim ver As IAMVersionable
Dim tvc As IAMTableViewCollection

Set dr = vaultLink.CreateVault
' Enumerate all subfolders and documents in the root folder.
Set tvc = dr.Root.Elements(EFF_ALL)
For Each fso In tvc
  ' If the object is a folder then display it's name.
  If fso.IsFolder Then
    MsgBox("Folder: " & fso.DisplayName)
  ' If the object is a document then ...
  Else
    Set doc = fso
    Set ver = fso
    ' Display it's name, version number, and document type name.
    MsgBox("Document: " & doc.DisplayName & " v." & _
    ver.VersionNr & " // " _
    & doc.DocumentTypeDisplayName)
    ' Change name of the document.
    Let doc.DisplayName = doc.DisplayName & " ~ "
  End If
Next fso
' Release COM objects.
Set fso = Nothing
Set tvc = Nothing
Set doc = Nothing
Set ver = Nothing
' Save changes.
dr.CloseRepository(True)
' Release the repository COM object.
Set dr = Nothing
Equivalent Visual Basic .NET code
Dim vaultLink As New BCVaultLink
Dim dr As BCRepository
Dim fso As BCFSObject
Dim doc As BCDocument
Dim tvc As BCCollection(Of BCFSObject)
dr = vaultLink.CreateVault
' Enumerate all subfolders and documents in the root folder.
tvc = dr.Root.Elements(FSObjectKind.EFF_ALL)
For Each fso In tvc
  ' If the object is a folder then display it's name.
  If TypeOf fso Is BCFolder Then
    MessageBox.Show("Folder: " + fso.DisplayName)
  ' If the object is a document then ...
  Else
    doc = fso
    ' Display it's name, version number, and document type name.
    MessageBox.Show("Document: " + doc.DisplayName + " v." + _
    doc.Versionable.VersionNr + " // " + _
    doc.DocumentTypeDisplayName)
    ' Change name of the document.
    doc.DisplayName = doc.DisplayName + " ~ "
  End If
Next fso
' Save changes.
dr.Commit()
' Release the repository COM object.
dr.Dispose()

The main difference between these examples is that when you use the SDK, you don’t need to release every COM object manually as you do in Visual Basic 6 (assign Nothing to a variable). The SDK deals with garbage collection by managing the references to COM objects. All you need to do is to call the Commit method of the BCRepository object to save any changes or the Rollback method to revoke changes.  Furthermore, every class of the SDK library supports a special .NET interface IDisposable that defines a Dispose method to explicitly release memory resources allocated by the object.

For classes that support the IDisposable interface, Visual Basic .NET (or C#) introduces a special statement Using that defines a scope at the end of which an object will be disposed. The Using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a Try block and then calling Dispose in a final block; in fact, this is how the Using statement is translated by the compiler. So, we highly recommend you use the Using statement in your code.

The following example shows how to use the statement in Visual Basic .NET code.

Using dr As BCRepository = vaultLink.CreateVault
  Using doc As BCDocument = dr.GetFSObject( _
  dr.PathToID("\MyFolder\MyDocument.doc"))
  doc.Workflow.ChangeState( _
    DocumentWorkflowFlags.DWFS_RELEASED, "Automatically released", "")
    ' Your code goes here...
  End Using
  dr.Commit()
End Using

The other topics in this chapter contain practical instructions for common scenarios, which make them good places to start learning about the .NET SDK and will help you effectively migrate from Visual Basic 6.

See Also