- INDEX VB TO NET
- HOW TO MODIFY THE WAY THE CODE IS GENERATED
- DEFAULT PROPERTY RESOLUTION THROUGH HELPER
Default Property Resolution through Helper
Visual Basic 6 presents early, late and/or dynamic binding for objects and variables; these concepts refer to the time when that specific object or variable type is defined. As per Early Binding it can be said that an object is early bound when it is assigned to a variable declared to be of a specific object type.In contrast to this concept Late Binding is the linking of a routine or object at runtime based on the conditions at the moment. A common issue found by the VBUC is the inability to resolve default properties in late binding cases.
In a nutshell, VB6 is able to resolve during runtime the type of any variable, and if it is necessary, to expand the default property of that type in order to get the correct value to be used. In the following example you can see that the variable ‘o’ is an Object, which must have a default property; at runtime VB6 expands its corresponding default property and gets or sets the value.
The VBUC is able to solve the late binding conflicted default properties by means of a helper class called “DefaultPropHelper”
Original VB6 Code:
Private Sub Foo(ByVal o As Object) Dim v As Variant o = "Value for the Default Property" v = o End Sub
Resulting VB.NET Code (no DefaultPropHelper):
Public Sub Foo(ByVal o As Object) 'UPGRADE_WARNING: (1037) Couldn't resolve default property of object o. o = "Value for the Default Property" 'UPGRADE_WARNING: (1068) o of type Object is being forced to Scalar. Dim v As Object = o End Sub
Resulting VB.NET Code (using DefaultPropHelper):
Public Sub Foo(ByVal o As Object) DefaultPropHelper.SetDefaultProperty(o, "Value for the Default Property") Dim v As Object = DefaultPropHelper.GetDefaultProperty(o) End Sub
Resulting C#.NET Code (no DefaultPropHelper):
private void Foo( object o) { //UPGRADE_WARNING: (1037) Couldn't resolve default property of object o. o = " Value for the Default Property"; //UPGRADE_WARNING: (1068) o of type object is being forced to string. string v = o; }
Resulting .NET Code (using DefaultPropHelper):
private void Foo( object o) { DefaultPropHelper.SetDefaultProperty(ref o, "Value for the Default Property"); string v = DefaultPropHelper.GetDefaultProperty<string>(o); }