Microsoft KB Archive/814603

= BUG: Late-bound calls to overloaded methods of a structure instance do not work as expected in Visual Basic .NET =

Article ID: 814603

Article Last Modified on 2/3/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
In Microsoft Visual Basic .NET, late-bound calls to overloaded methods of a structure instance do not work as expected. For example, if you call an overloaded method in a structure or in a class, the method that is called depends on the arguments that are passed as parameters to the method. In a late-bound call, the argument is an object. Based on the boxed value of the argument, the appropriate method is called at run time. However, you do not receive the same output as you receive for early bound arguments. The output you receive depends on the default values that the members of the structure have.



CAUSE
This problem occurs because of late-bound resolution. When a late-bound call is made to a method in the structure, the structure is boxed before passing to the late-bound helpers. In this process, a temporary copy of the structure is created. The instructions for the method are carried out in the temporary boxed structure instead of in the actual structure instance. The temporary structure goes out of scope when the method execution is completed. Executing the method does not affect the actual structure instance.



WORKAROUND
To work around this problem, use one of the following methods:
 * Type-cast the data to the required data type while you pass the data as a parameter to the method.

-or-


 * Use early binding. To do this, declare the parameters with the actual datatypes instead of with late binding (declare the parameters as objects).



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Open Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects. Under Templates, click Console Application. Name the project TestLateBound, and then click OK.

By default, Module1.vb is created.  Replace the existing code in Module1.vb with the following code: Module Module1 Structure MyTestStruct ' Value to store Public myVal As Integer

' Overload the function: If the function receives an Integer ' increment the value by 10. If the argument is Double then ' increment the value by 50.

Sub FuncOverload(ByVal arg As Integer) myVal = arg + 10 End Sub

Sub FuncOverload(ByVal arg As Double) myVal = arg + 50 End Sub End Structure

Sub Main

' Main Program Dim myStruct As MyTestStruct

' Declare an object Dim myObj As Object myObj = 20 ' Boxing of an integer

' Late-Bound Call to the method of the structure myStruct.FuncOverload(myObj)

Console.WriteLine(&quot;Expected Value is 30, Actual Value is {0}&quot;, myStruct.myVal)

' Work around, To make it type cast myStruct.FuncOverload(CType(myObj, Integer))

Console.WriteLine(&quot;Expected Value is 30, Actual Value is {0}&quot;, myStruct.myVal) Console.Read End Sub End Module </li> On the Debug menu, click Start to run the application.</li> Click any key to close the application.</li></ol>

<div class="references_section">