The following programming tips are provided for those new to writing MSMQ or Microsoft® Visual Basic® applications. They highlight several issues that may make writing your MSMQ application easier.
-
When declaring object variables, the New keyword can be added to the Dim statement to enable implicit creation of the object. This means that each time the variable is referenced, a new instance of the object is implicitly created if the current value of the variable is NULL.
-
In contrast, when the New keyword is not used, an instance of the object is only created when the Set command is called or some other mechanism (such as a call to a method that returns an object reference) is used to obtain an object instance. The Set command can be used as well for variables that were declared with the New keyword.
-
The example below shows when the New keyword should be used and when it should not be.
Dim qDest As MSMQQueue 'Set command needed.
Dim msgSent As New MSMQMessage
Dim msgDest As MSMQMessage 'Peek method returns
'MSMQMessage instance.
Set qDest = qinfoDest.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
msgSent.Send qDest
Set qDest = qinfoDest.Open(MQ_PEEK_ACCESS, MQ_DENY_NONE)
Set msgDest = qDest.Peek(ReceiveTimeout:=100)
-
When functions and subroutines are called, parentheses are not always required.
-
When functions or subroutines are called explicitly using the Call keyword, parentheses are required whenever there is one or more arguments. (The return value of a function can be ignored.)
For example:
Call Foo(x)
Call Foo(1, 2)
-
When a function is called and the return value is used, parentheses are always required whenever there is one or more arguments.
For example:
y = Foo(x, z) ' Result of Foo used to assign to y.
Call Bar(Foo(1)) ' Result of Foo used as argument to Bar.
-
However, when a function or subroutine is called without using the Call keyword and its return value is ignored:
-
Parentheses cannot be used for functions or subroutines that take more than one argument.
-
If parentheses are used for functions or subroutines that take a single argument, that argument is effectively passed 'by value' since the argument is in effect an expression whose result is returned in a temporary variable.
For example:
Foo x 'Parentheses cannot be used: x is passed by reference.
Foo (x) 'x is effectively passed by value.
-
Use named arguments to make your code easier to read. Using non-named arguments forces the reader to remember the argument's name and the order of the arguments. For example, the following two lines of code are functionally identical, yet the first is much easier to understand:
Create IsWorldReadable:=True, IsTransactional:=False
Create False, True
-
A Variant containing an array can be used like an array. For example, ubound(msg.Id) or msg.CorrelationId(10).
-
When declaring object's specify the object class in the Dim statement (early-binding). Using early-binding whenever possible will make your application run faster. For example, the following examples are both functionally equal, yet the first example executes faster due to early-binding of the object.
dim qinfo as MSMQQueueInfo
set qinfo = New MSMQQueueInfo
qinfo.PathName = ".\PRIVATE$\CreateTest"
qinfo.Create
dim qinfo as Object
set qinfo = New MSMQQueueInfo
qinfo.PathName = ".\PRIVATE$\CreateTest"
qinfo.Create
-
The Microsoft® Visual Basic® 5.0 debugger can be used on MSMQ application executable and DLL files generated by Visual Basic 5.0. You can set breakpoints as well as disassemble and see the generated VBA code and look at local variables.
-
The MSMQApplication object does not have to be referenced. For example, the following three calls to MachineIdOfMachineName all return the same computer identifier.
Dim strId As String
Dim myapp As New MSMQApplication
strId = MachineIdOfMachineName("machinename")
Debug.Print strId
strId = MSMQApplication.MachineIdOfMachineName("machinename")
Debug.Print strId
strId = myapp.MachineIdOfMachineName("machinename")
Debug.Print strId