Home All Groups Group Topic Archive Search About

CreateInstance and late binding

Author
5 Jul 2006 3:08 PM
Rippo
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.WriteLine("TEST1.A Called")
    End Sub

End Class

Public Class test2

    Public Sub a()
        Console.WriteLine("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.CreateInstance(GetType(test1))
            Dim BL As test1 = CType(o, test1)
        Else
            o = Activator.CreateInstance(GetType(test2))
            Dim BL As test2 = CType(o, test2)
        End If
    End Sub

    Sub Main()
        BL.a()
        Console.ReadLine()
    End Sub

End Module

Author
5 Jul 2006 3:28 PM
Cor Ligthert [MVP]
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/library/system.reflection.aspx

I hope this helps,

Cor

Show quoteHide quote
"Rippo" <i***@rippo.co.uk> schreef in bericht
news:1152112082.882266.223500@l70g2000cwa.googlegroups.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.WriteLine("TEST1.A Called")
>    End Sub
>
> End Class
>
> Public Class test2
>
>    Public Sub a()
>        Console.WriteLine("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.CreateInstance(GetType(test1))
>            Dim BL As test1 = CType(o, test1)
>        Else
>            o = Activator.CreateInstance(GetType(test2))
>            Dim BL As test2 = CType(o, test2)
>        End If
>    End Sub
>
>    Sub Main()
>        BL.a()
>        Console.ReadLine()
>    End Sub
>
> End Module
>
Author
5 Jul 2006 3:40 PM
Jim Wooley
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.CreateInstance(GetType(test1))
  Dim BL As test1 = CType(o, test1)
Else
  o = Activator.CreateInstance(GetType(test2))
  Dim BL As test2 = CType(o, test2)
End If
End Sub

Sub Main()
BL.a()
Console.ReadLine()
End Sub

Here, you have late binding with option strict.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

Show quoteHide quote
> 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.WriteLine("TEST1.A Called")
> End Sub
> End Class
>
> Public Class test2
>
> Public Sub a()
> Console.WriteLine("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.CreateInstance(GetType(test1))
> Dim BL As test1 = CType(o, test1)
> Else
> o = Activator.CreateInstance(GetType(test2))
> Dim BL As test2 = CType(o, test2)
> End If
> End Sub
> Sub Main()
> BL.a()
> Console.ReadLine()
> End Sub
> End Module
>
Author
5 Jul 2006 4:03 PM
Rippo
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.WriteLine("TEST1.A Called")
    End Sub

End Class

Public Class test2 : Implements iSome

    Public Sub a() Implements iSome.a
        Console.WriteLine("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.CreateInstance(GetType(test1))
            BL = CType(o, test1)
        Else
            o = Activator.CreateInstance(GetType(test2))
            BL = CType(o, test2)
        End If
    End Sub

    Sub Main()
        BL.a()
        Console.ReadLine()
    End Sub

End Module
Author
5 Jul 2006 4:15 PM
Cor Ligthert [MVP]
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


Show quoteHide quote
> 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.CreateInstance(GetType(test1))
>  Dim BL As test1 = CType(o, test1)
> Else
>  o = Activator.CreateInstance(GetType(test2))
>  Dim BL As test2 = CType(o, test2)
> End If
> End Sub
>
> Sub Main()
> BL.a()
> Console.ReadLine()
> End Sub
>
> Here, you have late binding with option strict.
> Jim Wooley
> http://devauthority.com/blogs/jwooley/default.aspx
>
>> 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.WriteLine("TEST1.A Called")
>> End Sub
>> End Class
>>
>> Public Class test2
>>
>> Public Sub a()
>> Console.WriteLine("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.CreateInstance(GetType(test1))
>> Dim BL As test1 = CType(o, test1)
>> Else
>> o = Activator.CreateInstance(GetType(test2))
>> Dim BL As test2 = CType(o, test2)
>> End If
>> End Sub
>> Sub Main()
>> BL.a()
>> Console.ReadLine()
>> End Sub
>> End Module
>>
>
>