ISSUE #2072

    1% 2% could not be resolved because it was within the generic namespace 3%.

    Description

    This occurs when a generic class (like Form class) is used and then a method specific of some classes that inherits from that class, are called, those methods are late binded so, VB6 could resolved (if it exists on the object), but in .Net this is not possible.

    Recommendations

    Define an Interface and make all the forms that are ussed in those clases implement it. Then use the interface instead of using the generic class (in the example Form Class).

    Sample VB6

    Form1 Code

    PublicSub ObjectMethod()

    MsgBox("Form1")

    EndSub

     

    Form2 Code

    PublicSub ObjectMethod()

    MsgBox("Form2")

    EndSub

     

    Module Example

    PublicSub CallMethodInForm(ByVal F As Form)

    F.ObjectMethod()

    EndSub

     

    PublicSub Test()

    Dim F1 AsNew Form1

    Dim F2 AsNew Form2

    CallMethodInForm(F1)

    CallMethodInForm(F2)

    EndSub

    Target VB.NET

    Form1 Code

    Inherits System.Windows.Forms.Form

    PublicSub ObjectMethod()

    MessageBox.Show("Form1", Application.ProductName)

    EndSub

     

    Form2 Code

    Inherits System.Windows.Forms.Form

    PublicSub ObjectMethod()

    MessageBox.Show("Form2", Application.ProductName)

    EndSub

     

    Module Example

    PublicSub CallMethodInForm(ByVal F As Form)

    'UPGRADE_ISSUE: (2072) Control ObjectMethod could not be resolved because it was within the generic namespace Form.

    F.ObjectMethod()

    EndSub

     

    PublicSub Test()

    Dim F1 AsNew Form1

    'UPGRADE_ISSUE: (2068) Form2 object was not upgraded.

    Dim F2 AsNew Form2

    CallMethodInForm(F1)

    'UPGRADE_WARNING: (1068) F2 of type Form2 is being forced to VB.Form.

    CallMethodInForm(F2)

    EndSub

    Expected VB.NET

    Add an Interface

    PublicInterface FormInterface

    Sub ObjectMethod()

    EndInterface

     

    Form1 Code

    Inherits System.Windows.Forms.Form

    Implements FormInterface

    PublicSub ObjectMethod() Implements FormInterface.ObjectMethod

    MessageBox.Show("Form1", Application.ProductName)

    EndSub

     

    Form2 Code

    Inherits System.Windows.Forms.Form

    Implements FormInterface

    PublicSub ObjectMethod() Implements FormInterface.ObjectMethod

    MessageBox.Show("Form2", Application.ProductName)

    EndSub

     

    Module Example

    PublicSub CallMethodInForm(ByVal F As FormInterface)

    F.ObjectMethod()

    EndSub

     

    PublicSub Test()

    Dim F1 AsNew Form1

    Dim F2 AsNew Form2

    CallMethodInForm(F1)

    CallMethodInForm(F2)

    EndSub

    Target C#

    Form1 Code

    internalpartialclassForm1

    : System.Windows.Forms.Form

    {

     

    publicvoid ObjectMethod()

    {

    MessageBox.Show("Form1", Application.ProductName);

    }

    }

    Form2 Code

    internalpartialclassForm2

    : System.Windows.Forms.Form

    {

     

    publicvoid ObjectMethod()

    {

    MessageBox.Show("Form2", Application.ProductName);

    }

    }

     

    Module Example

    staticpublicvoid CallMethodInForm( Form F)

    {

    //UPGRADE_ISSUE: (2072) Control ObjectMethod could not be resolved because it was within the generic namespace Form.

    F.ObjectMethod();

    }

     

    staticpublicvoid Test()

    {

    Form1 F1 = newForm1();

    Form2 F2 = newForm2();

    CallMethodInForm(F1);

    CallMethodInForm(F2);

    }

    Expected C#

    Add an Interface

    interfaceFormInterface

    {

    void ObjectMethod();

    }

     

    Form1 Code

    internalpartialclassForm1

    : System.Windows.Forms.Form, FormInterface

    {

    publicvoid  ObjectMethod()

    {

    MessageBox.Show("Form1", Application.ProductName);

    }

    }

     

    Form2 Code

    internalpartialclassForm2

    : System.Windows.Forms.Form, FormInterface

    {

    publicvoid  ObjectMethod()

    {

    MessageBox.Show("Form2", Application.ProductName);

    }

    }

     

    Module Example

    staticpublicvoid CallMethodInForm(FormInterface F)

    {

    F.ObjectMethod();

    }

    staticpublicvoid Test()

    {

    Form1 F1 = newForm1();

    Form2 F2 = newForm2();

    CallMethodInForm(F1);

    CallMethodInForm(F2);

    }


    Download VBUC Free Trial
    Download VBUC Now

    It's time to eradicate VB6
    ROI of eradicating VB6

    8 Proven Tips for
    Planning a Successful Migration

    8 Tips for migration