SAS Object Manager Code Samples

Using CreateObjectByServer to Make a Connection

Overview of Using CreateObjectByServer to Make a Connection

Samples 1 through 5 show how to create a connection by specifying server parameters (machine, protocol, port) directly in the source code.
Samples 1, 2, and 3 show synchronous connections. The synchronous connection is the most frequently used and is the simplest connection to create.
Samples 4 and 5 use asynchronous connections. The advantage of making an asynchronous connection is that the client application can continue to execute code while the connection is being established. An application that interacts with multiple SAS servers might create asynchronous connections to all of the servers at the same time.
When making asynchronous connections, the ObjectKeeper maintains a reference to the created object until you call ObjectKeeper.RemoveObject. This call is not shown in the code for samples 4 and 5.
The object factory adds the newly created object to the ObjectKeeper as soon as the connection is established. You can then use the ObjectKeeper to get the connection.

Sample 1: Make a Connection to a Local Workspace Using COM

Dim obObjectFactory As New SASObjectManager.ObjectFactoryMulti2
Dim obSAS As SAS.Workspace
Set obSAS = obObjectFactory.CreateObjectByServer(
"myName", True, Nothing, "", "")

Sample 2: Make a Connection to a Remote Workspace Using DCOM

Dim obObjectFactory As New SASObjectManager.ObjectFactoryMulti2
Dim obSAS As SAS.Workspace
Dim obServer As New SASObjectManager.ServerDef
obServer.MachineDNSName = "RemoteMachine.company.com"
Set obSAS = obObjectFactory.CreateObjectByServer(
"myName", True, obServer, "", "")

Sample 3: Make a Connection to a Remote Workspace Using IOM Bridge

Dim obObjectFactory As New SASObjectManager.ObjectFactoryMulti2
Dim obSAS As SAS.Workspace
Dim obServer As New SASObjectManager.ServerDef
obServer.MachineDNSName = "RemoteMachine.company.com"
obServer.Protocol = ProtocolBridge
obServer.Port = 6903
Set obSAS = obObjectFactory.CreateObjectByServer("myName", True, obServer,
"myUserName", "myPassword")

Sample 4: Start Multiple Connections Asynchronously

Dim obObjectFactory As New SASObjectManager.ObjectFactoryMulti2
Dim obObjectKeeper As New SASObjectManager.ObjectKeeper
Dim obSAS As SAS.Workspace
Dim obSAS2 As SAS.Workspace
Dim obServer As New SASObjectManager.ServerDef
Dim obServer2 As New SASObjectManager.ServerDef
obServer.MachineDNSName = "MachineA.company.com"
obServer.Protocol = ProtocolBridge
obServer.Port = 6903
obObjectFactory.CreateObjectByServer "myName", False, obServer,
"myUsername", "myPassword"
obServer2.MachineDNSName = "MachineB.company.com"
obServer2.Protocol = ProtocolBridge
obServer2.Port = 6903
obObjectFactory.CreateObjectByServer "myName2", False, obServer2,
"myUsername", "myPassword"
' Note that the first parameter here matches the first parameter in the
' call to CreateObjectByServer
Set obSAS = obObjectKeeper.WaitForObject("myName", 10000)
Set obSAS2 = obObjectKeeper.WaitForObject("myName2", 10000)

Sample 5: Listen for Events Using Asynchronous Connections

Public WithEvents obObjectKeeperEvents As SASObjectManager.ObjectKeeper
Private Sub Form_Load()
Dim obObjectFactory As New SASObjectManager.ObjectFactoryMulti2
Dim obObjectKeeper As New SASObjectManager.ObjectKeeper
Dim obSAS As SAS.Workspace
Dim obSAS2 As SAS.Workspace
Dim obServer As New SASObjectManager.ServerDef
Dim obServer2 As New SASObjectManager.ServerDef
Set obObjectKeeperEvents = obObjectKeeper
obServer.MachineDNSName = "MachineA.company.com"
obServer.Protocol = ProtocolBridge
obServer.Port = 6903
obObjectFactory.CreateObjectByServer "myName", False, obServer,
"myUsername", "myPassword"
obServer2.MachineDNSName = "Machineb.company.com"
obServer2.Protocol = ProtocolBridge
obServer2.Port = 6903
obObjectFactory.CreateObjectByServer "myName2", False, obServer2,
"myUsername", "myPassword"
End Sub
Private Sub obObjectKeeperEvents_ErrorAdded(ByVal objectName As String,
ByVal errInfo As String)
Debug.Print "Error creating " & objectName & ": " & errInfo
End Sub
Private Sub obObjectKeeperEvents_ObjectAdded(ByVal objectName As String,
ByVal objectUUID As String)
Debug.Print "Added object " & objectName & ": " & objectUUID
Dim obSAS As SAS.Workspace
Set obSAS = obObjectKeeperEvents.GetObjectByUUID(objectUUID)
Debug.Print obSAS.UniqueIdentifier
End Sub

Using CreateObjectByLogicalName to Make a Connection

The CreateObjectByLogicalName method creates a SAS object from a logical name definition. When you use CreateObjectByLogicalName, you must define server metadata for your connections and associate a logical name with the connection. This technique enables you to administratively change the machines where SAS is running without modifying your source code.
The following sample shows how to make a connection to a logical server.
Dim obObjectFactory As New SASObjectManager.ObjectFactoryMulti2
Dim obSAS As SAS.Workspace
' This assumes that your metadata configuration files are in the default
' location, or that the location of your configuration files is stored
' in the registry. If this is not the case, or you want each application
' on a given machine to use its own metadata repository, then you should
' call
' obObjectFactory.SetMetadataFile systemFile, userFile, false
Set obSAS = obObjectFactory.CreateObjectByLogicalName("myName", True,
"LogicalName", "LoginReference")