Microsoft KB Archive/246460: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
Line 46: Line 46:
The '''IsLastInTransaction''' property of a Microsoft Message Queuing, also known as MSMQ, MSMQMessage object returns an Integer result rather than a Boolean. '''IsLastInTransaction''' returns a 1 if the message is the last in the transaction and a 0 if it is not. This can cause logic errors in a Visual Basic (VB) program if the property is used in the expression portion of an '''If''', '''While''', or similar conditional statement.<br />
The '''IsLastInTransaction''' property of a Microsoft Message Queuing, also known as MSMQ, MSMQMessage object returns an Integer result rather than a Boolean. '''IsLastInTransaction''' returns a 1 if the message is the last in the transaction and a 0 if it is not. This can cause logic errors in a Visual Basic (VB) program if the property is used in the expression portion of an '''If''', '''While''', or similar conditional statement.<br />
<br />
<br />
When a numeric value is used in a condition statement in VB, 0 is evaluated as false and all non-zero values are evaluated as true. The '''Not''' operator in VB inverts the bits of a value when used on a numeric value. The expression &quot;Not 1&quot; produces the result &quot;-2&quot; which is considered a &quot;true&quot; value in a VB condition statement.<br />
When a numeric value is used in a condition statement in VB, 0 is evaluated as false and all non-zero values are evaluated as true. The '''Not''' operator in VB inverts the bits of a value when used on a numeric value. The expression "Not 1" produces the result "-2" which is considered a "true" value in a VB condition statement.<br />
<br />
<br />
Since VB evaluates both &quot;1&quot; and &quot;Not 1&quot; as true statements, if a message is the last in a transaction both &quot;message.IsLastInTransaction&quot; and &quot;Not message.IsLastInTransaction&quot; evaluate true. In the following example code, if the message is the last in a transaction both message boxes appear:
Since VB evaluates both "1" and "Not 1" as true statements, if a message is the last in a transaction both "message.IsLastInTransaction" and "Not message.IsLastInTransaction" evaluate true. In the following example code, if the message is the last in a transaction both message boxes appear:
<pre class="codesample">If message.IsLastInTransaction Then
<pre class="codesample">If message.IsLastInTransaction Then
   MsgBox &quot;The message was the last message sent in the transaction.&quot;
   MsgBox "The message was the last message sent in the transaction."
End If
End If


If Not message.IsLastInTransaction Then
If Not message.IsLastInTransaction Then
   MsgBox &quot;The message was not the last message sent in the transaction.&quot;
   MsgBox "The message was not the last message sent in the transaction."
End If
End If
                 </pre>
                 </pre>
Line 68: Line 68:
</p>
</p>
<pre class="codesample">If MSMQMessageObject.IsLastInTransaction = 1 Then
<pre class="codesample">If MSMQMessageObject.IsLastInTransaction = 1 Then
   MsgBox &quot;The message was the last message sent in the transaction.&quot;
   MsgBox "The message was the last message sent in the transaction."
End If
End If


If MSMQMessageObject.IsLastInTransaction = 0 Then
If MSMQMessageObject.IsLastInTransaction = 0 Then
   MsgBox &quot;The message was not the last message sent in the transaction.&quot;
   MsgBox "The message was not the last message sent in the transaction."
End If
End If
                         </pre></li>
                         </pre></li>
<li>Avoid &quot;negative&quot; tests that use the '''Not''' operator with the '''IsLastInTransaction''' property.</li></ol>
<li>Avoid "negative" tests that use the '''Not''' operator with the '''IsLastInTransaction''' property.</li></ol>





Latest revision as of 13:50, 21 July 2020

Knowledge Base


Article ID: 246460

Article Last Modified on 2/23/2007



APPLIES TO

  • Microsoft Message Queuing 2.0



This article was previously published under Q246460

SYMPTOMS

The IsLastInTransaction property of a Microsoft Message Queuing, also known as MSMQ, MSMQMessage object returns an Integer result rather than a Boolean. IsLastInTransaction returns a 1 if the message is the last in the transaction and a 0 if it is not. This can cause logic errors in a Visual Basic (VB) program if the property is used in the expression portion of an If, While, or similar conditional statement.

When a numeric value is used in a condition statement in VB, 0 is evaluated as false and all non-zero values are evaluated as true. The Not operator in VB inverts the bits of a value when used on a numeric value. The expression "Not 1" produces the result "-2" which is considered a "true" value in a VB condition statement.

Since VB evaluates both "1" and "Not 1" as true statements, if a message is the last in a transaction both "message.IsLastInTransaction" and "Not message.IsLastInTransaction" evaluate true. In the following example code, if the message is the last in a transaction both message boxes appear:

If message.IsLastInTransaction Then
   MsgBox "The message was the last message sent in the transaction."
End If

If Not message.IsLastInTransaction Then
   MsgBox "The message was not the last message sent in the transaction."
End If
                

RESOLUTION

There are two possible workarounds for the problem:

  1. Explicitly test the value of the IsLastInTransaction property against 0 and 1. A value of 1 indicates that the message is the last in the transaction, a value of 0 indicates that the message is not the last in the transaction. An example of this test is shown in the following code:

    If MSMQMessageObject.IsLastInTransaction = 1 Then
       MsgBox "The message was the last message sent in the transaction."
    End If
    
    If MSMQMessageObject.IsLastInTransaction = 0 Then
       MsgBox "The message was not the last message sent in the transaction."
    End If
                            
  2. Avoid "negative" tests that use the Not operator with the IsLastInTransaction property.


STATUS

It is safe to test explicitly for the 1 and 0 values. These values will not be changed in any future version of Message Queuing and will remain consistent. Future versions of the Message Queuing ActiveX components may add a property that may be evaluated as a true Boolean type for testing this characteristic.

MORE INFORMATION

The MSDN Online help describes the return values for the IsLastInTransaction property as follows:

MSMQMessage.IsLastInTransaction

Returned Value
   1 
      The message was the last message sent in the transaction. 
   0 
      The message was not the last message sent in the transaction. 
                

REFERENCES

MSDN Online Help for the MSMQMessage Object
Visual Basic Programmer's Guide

246218 MSMQMessage Object IsAuthenticated Property Returns an Integer



246222 MSMQQueueInfo Object IsTransactional Property Returns An Integer



246225 MSMQQueueInfo Object IsWorldReadable Property Returns and Integer


245753 MSMQQueue Object IsOpen Property Returns an Integer


246458 MSMQMessage Object IsFirstInTransaction Property Returns An Integer



Additional query words: MSMQ MSMQMessage IsLastInTransaction

Keywords: kbprb KB246460