Поделиться через


Using Message Queuing COM Components in Microsoft Visual Basic

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

The following programming tips are provided for those new to writing Message Queuing or Microsoft® Visual Basic® applications. They highlight several issues that may make writing your Message Queuing application easier.

Declaring Objects

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 Nothing.

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)  

Calling Functions and Subroutines Using the Call Keyword

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. In this case the return value of a function is discarded. 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.

y = Foo(x, z)              ' Result of Foo used to assign to y.  
Call Bar(Foo(1))           ' Result of Foo used as argument to Bar.  

Calling Functions and Subroutines Without Using the Call Keyword

When a function or subroutine is called without using the Call keyword, 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

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  

Variants Containing Arrays

A Variant containing an array can be used like an array. For example, ubound(msg.Id) or msg.CorrelationId(10).  

Use Early-Binding

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   

Using Microsoft® Visual Basic®

The Microsoft® Visual Basic® debugger can be used on Message Queuing application executable and DLL files generated by Visual Basic. You can set breakpoints as well as disassemble and see the generated VBA code and look at local variables.