|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
CreateInstance and late bindingI 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 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 > 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 > 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 Jim,
> if your classes implement the same interface, you can code to the But than it is not late binding anymore it is accessing them using the > interface rather than the instance. In that case, your code becomes: > interface. :-) CorShow 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 >> > >
What exactly does it mean when they say
CopyFile() and Progress Bar? Get object from name how to parse an Enum Structure in vb.net No of files in folder VB 2005 Newbie - Binding? activate a click onto a picture button Getting form to run its load event without showing form Who is using a given port? Weird problem |
|||||||||||||||||||||||