PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET CreateInstance and late binding

Reply

CreateInstance and late binding

 
Thread Tools Rate Thread
Old 05-07-2006, 04:08 PM   #1
Rippo
Guest
 
Posts: n/a
Default 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.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

  Reply With Quote
Old 05-07-2006, 04:28 PM   #2
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default 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/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
>



  Reply With Quote
Old 05-07-2006, 04:40 PM   #3
Jim Wooley
Guest
 
Posts: n/a
Default 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.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
>



  Reply With Quote
Old 05-07-2006, 05:03 PM   #4
Rippo
Guest
 
Posts: n/a
Default 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.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

  Reply With Quote
Old 05-07-2006, 05:15 PM   #5
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default 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.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
>>

>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off