CreateInstance and late binding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rippo

    #1

    CreateInstance and late binding

    Hi
    I have the following console application and am attempting to late bind
    a class with option strict on! However of course I cant and I get the
    following error "Option Strict On disallows late binding" at the line
    BL.a() in method Main

    How do I overcome this?

    Public Class test1

    Public Sub a()
    Console.WriteLi ne("TEST1.A Called")
    End Sub

    End Class

    Public Class test2

    Public Sub a()
    Console.WriteLi ne("TEST2.A Called")
    End Sub

    End Class

    Module Module1

    Dim BL As Object

    Sub New()
    Dim o As Object
    If 1 = 1 Then
    o = Activator.Creat eInstance(GetTy pe(test1))
    Dim BL As test1 = CType(o, test1)
    Else
    o = Activator.Creat eInstance(GetTy pe(test2))
    Dim BL As test2 = CType(o, test2)
    End If
    End Sub

    Sub Main()
    BL.a()
    Console.ReadLin e()
    End Sub

    End Module

  • Cor Ligthert [MVP]

    #2
    Re: CreateInstance and late binding

    Rippo,

    If you want to use late binding with option strict on, you have to do
    yourself what VB does without that on.

    The class for that is Reflection. There is enough written on MSDN in that
    class and on the Interenet to see what it does. It is very popular to use.
    I don't understand why because it has a huge bad impact on the performance
    of your program.

    http://msdn2.microsoft.com/en-us/lib...eflection.aspx

    I hope this helps,

    Cor

    "Rippo" <info@rippo.co. ukschreef in bericht
    news:1152112082 .882266.223500@ l70g2000cwa.goo glegroups.com.. .
    Hi
    I have the following console application and am attempting to late bind
    a class with option strict on! However of course I cant and I get the
    following error "Option Strict On disallows late binding" at the line
    BL.a() in method Main
    >
    How do I overcome this?
    >
    Public Class test1
    >
    Public Sub a()
    Console.WriteLi ne("TEST1.A Called")
    End Sub
    >
    End Class
    >
    Public Class test2
    >
    Public Sub a()
    Console.WriteLi ne("TEST2.A Called")
    End Sub
    >
    End Class
    >
    Module Module1
    >
    Dim BL As Object
    >
    Sub New()
    Dim o As Object
    If 1 = 1 Then
    o = Activator.Creat eInstance(GetTy pe(test1))
    Dim BL As test1 = CType(o, test1)
    Else
    o = Activator.Creat eInstance(GetTy pe(test2))
    Dim BL As test2 = CType(o, test2)
    End If
    End Sub
    >
    Sub Main()
    BL.a()
    Console.ReadLin e()
    End Sub
    >
    End Module
    >

    Comment

    • Jim Wooley

      #3
      Re: CreateInstance and late binding

      An alternative to what Cor said, if your classes implement the same interface,
      you can code to the interface rather than the instance. In that case, your
      code becomes:

      public interface IDoSomething
      sub a()
      end interface

      public Class test1
      implements IDoSomething
      Public Sub a() implements IDoSomething.a
      '...
      end sub
      end Class

      Private BL as IDoSomething
      Sub New()
      Dim o As IDoSomething
      If 1 = 1 Then
      o = Activator.Creat eInstance(GetTy pe(test1))
      Dim BL As test1 = CType(o, test1)
      Else
      o = Activator.Creat eInstance(GetTy pe(test2))
      Dim BL As test2 = CType(o, test2)
      End If
      End Sub

      Sub Main()
      BL.a()
      Console.ReadLin e()
      End Sub

      Here, you have late binding with option strict.

      Jim Wooley

      Hi
      I have the following console application and am attempting to late
      bind
      a class with option strict on! However of course I cant and I get the
      following error "Option Strict On disallows late binding" at the line
      BL.a() in method Main
      How do I overcome this?
      >
      Public Class test1
      >
      Public Sub a()
      Console.WriteLi ne("TEST1.A Called")
      End Sub
      End Class
      >
      Public Class test2
      >
      Public Sub a()
      Console.WriteLi ne("TEST2.A Called")
      End Sub
      End Class
      >
      Module Module1
      >
      Dim BL As Object
      >
      Sub New()
      Dim o As Object
      If 1 = 1 Then
      o = Activator.Creat eInstance(GetTy pe(test1))
      Dim BL As test1 = CType(o, test1)
      Else
      o = Activator.Creat eInstance(GetTy pe(test2))
      Dim BL As test2 = CType(o, test2)
      End If
      End Sub
      Sub Main()
      BL.a()
      Console.ReadLin e()
      End Sub
      End Module
      >

      Comment

      • Rippo

        #4
        Re: CreateInstance and late binding

        Nice one Jim

        I have slightly modified your code!

        Public Interface iSome
        Sub a()
        End Interface


        Public Class test1 : Implements iSome

        Public Sub a() Implements iSome.a
        Console.WriteLi ne("TEST1.A Called")
        End Sub

        End Class

        Public Class test2 : Implements iSome

        Public Sub a() Implements iSome.a
        Console.WriteLi ne("TEST2.A Called")
        End Sub

        End Class

        Module Module1

        Private BL As iSome

        Sub New()
        Dim o As Object
        If 1 = 1 Then
        o = Activator.Creat eInstance(GetTy pe(test1))
        BL = CType(o, test1)
        Else
        o = Activator.Creat eInstance(GetTy pe(test2))
        BL = CType(o, test2)
        End If
        End Sub

        Sub Main()
        BL.a()
        Console.ReadLin e()
        End Sub

        End Module

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: CreateInstance and late binding

          Jim,
          if your classes implement the same interface, you can code to the
          interface rather than the instance. In that case, your code becomes:
          >
          But than it is not late binding anymore it is accessing them using the
          interface.

          :-)

          Cor

          public interface IDoSomething
          sub a()
          end interface
          >
          public Class test1
          implements IDoSomething
          Public Sub a() implements IDoSomething.a
          '...
          end sub
          end Class
          >
          Private BL as IDoSomething
          Sub New()
          Dim o As IDoSomething
          If 1 = 1 Then
          o = Activator.Creat eInstance(GetTy pe(test1))
          Dim BL As test1 = CType(o, test1)
          Else
          o = Activator.Creat eInstance(GetTy pe(test2))
          Dim BL As test2 = CType(o, test2)
          End If
          End Sub
          >
          Sub Main()
          BL.a()
          Console.ReadLin e()
          End Sub
          >
          Here, you have late binding with option strict.
          Jim Wooley

          >
          >Hi
          >I have the following console application and am attempting to late
          >bind
          >a class with option strict on! However of course I cant and I get the
          >following error "Option Strict On disallows late binding" at the line
          >BL.a() in method Main
          >How do I overcome this?
          >>
          >Public Class test1
          >>
          >Public Sub a()
          >Console.WriteL ine("TEST1.A Called")
          >End Sub
          >End Class
          >>
          >Public Class test2
          >>
          >Public Sub a()
          >Console.WriteL ine("TEST2.A Called")
          >End Sub
          >End Class
          >>
          >Module Module1
          >>
          >Dim BL As Object
          >>
          >Sub New()
          >Dim o As Object
          >If 1 = 1 Then
          >o = Activator.Creat eInstance(GetTy pe(test1))
          >Dim BL As test1 = CType(o, test1)
          >Else
          >o = Activator.Creat eInstance(GetTy pe(test2))
          >Dim BL As test2 = CType(o, test2)
          >End If
          >End Sub
          >Sub Main()
          >BL.a()
          >Console.ReadLi ne()
          >End Sub
          >End Module
          >>
          >
          >

          Comment

          Working...