0

Looking to extend the BeginReceive method, and the event handler for MessageQueue.ReceiveCompleted to pass along by REFERENCE a TCPClient object. As is, I can pass it in as an object, but it will be by VALUE, and therefore will be a copy of the TCPClient object. No good!

So, I decided to try and write my own overloaded methods, etc. Debugging into the .NET 4.5.2 Framework code, I see that BeginReceive(TimeSpan timeout, object stateObject) returns:

ReceiveAsync(timeout, CursorHandle.NullHandle, NativeMethods.QUEUE_ACTION_RECEIVE, null,
             stateObject);

The problem is that CursorHandle and NativeMethods seem to be in System.Messaging.Interop namespace, but since all those classes are declared as "internal" I can't seemingly access them. (Yes - I downloaded the .NET Framework C# code). Is there any quick way to access this stuff?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
David P
  • 2,027
  • 3
  • 15
  • 27

1 Answers1

2

TCPClient is a class and therefore a reference type. Parameters and variables of this type contain a reference to an object. In this case ByVal means that this reference is passed by value, not the object.

The TCPClient object will not be copied!


Note that you can also pass a reference ByRef. In this case you are dealing with a reference to a reference to an object. What this means is that if you assign a new object to such a parameter in a method, this changes the variable used as method argument. If the parameter was ByVal it would not change this variable, as the method parameter would contain a copy of the reference (but not a copy of the object).

In both cases, if you change a property of the object in the method, it will change the original object, since it is the identical object.

Call Method(ByVal variable)

                              +---------+
                              |         |
        variable +----------> | Object  |
                              |         |
                              +---+-----+
                                  ^
        Sub Method (ByVal p)      |
            p  +------------------+        'If you change p here, it does NOT change variable.
        End Sub
Call Method(ByRef variable)

                              +---------+
                              |         |
+-----> variable +----------> | Object  |
|                             |         |
|                             +---------+
|
|       Sub Method (ByRef p)
+---------+ p                              'If you change p here, it DOES change variable.
        End Sub
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188