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



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Open Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects.
  4. Under Templates, click Console Application.
  5. Name the project TestLateBound, and then click OK.

    By default, Module1.vb is created.
  6. 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("Expected Value is 30, Actual Value is {0}", myStruct.myVal)
    
          ' Work around,  To make it type cast 
          myStruct.FuncOverload(CType(myObj, Integer))
    
          Console.WriteLine("Expected Value is 30, Actual Value is {0}", myStruct.myVal)
          Console.Read()
       End Sub
    End Module
  7. On the Debug menu, click Start to run the application.
  8. Click any key to close the application.

REFERENCES

For more information about overloading methods, click the following article number to view the article in the Microsoft Knowledge Base:

311330 INFO: Overloading methods in Visual Basic .NET


Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbProgramming kbCompiler kbbug KB814603 kbAudDeveloper