PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
CreateInstance and late binding
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
CreateInstance and late binding
![]() |
CreateInstance and late binding |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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/li...reflection.aspx I hope this helps, Cor "Rippo" <info@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 > |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 > 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 > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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.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 >> > > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

