473,785 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Option Strict and late binding problem

I think I've done my homework and checked around on Google Groups but this
seems to be a situation not yet covered.

Here is the scenario...

There are two classes, Foo and Bar (actually there are more than two
classes involved but two will suffice to explain the problem), and each
class has a Copy() method as follows:

Public Function Copy() As Object Implements ICloneable.Clon e
' returns a copy of a Foo object
End Function

Public Function Copy() As Object Implements ICloneable.Clon e
' returns a copy of a Bar object
End Function

I have another class that subclasses ArrayList. This class allows the list
to contain Foo and Bar objects.

With me so far? Here comes the kicker...

I then iterate over the ArrayList as follows making a copy of the array
list:

Dim objekt, objcopy As Object
For Each objekt In myArrayListInst ance
objcopy = objekt.Copy() ' Each objekt is either a Foo object or a Bar
object
' build another ArrayList with copies of all objects from the original
ArrayList
Next

With 'Option Strict On' this produces a compiler error.

How can I achieve this kind of polymorphism with 'Option Strict On' ?

Thanks for your consideration,

Daniel Klein
Cuyahoga Falls, OH
Nov 21 '05 #1
12 1556
>How can I achieve this kind of polymorphism with 'Option Strict On' ?

For Each objekt As ICloneable In myArrayListInst ance
objcopy = objekt.Clone()

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
"Daniel Klein" <da***********@ hotmail.com> wrote in message
news:k5******** *************** *********@4ax.c om...
With 'Option Strict On' this produces a compiler error.
How can I achieve this kind of polymorphism ... ?


"Object" isn't half as useful as you might think - always try to get at
a proper type when manipulating objects and, since both (all?) of
your classes implement IClonable, that's as good a type as any when
you need to use them, as in :

For Each objekt As IClonable In myArrayListInst ance
objcopy = objekt.Clone() ' returns either Foo or Bar
' build another ArrayList with copies
NewArrayList.Ad d( objekt )
Next

HTH,
Phill W.
Nov 21 '05 #3
Mttias,

Thank you for the cortesy of your reply.

Unfortunately, this won't work. Each of the Copy() methods is doing a bit
more than what Object.Clone() is doing, which is the reason for creating my
own implementation. And each of the Copy() methods cannot be refactored to
a common implementation.

Besides, this problem address a more generic one of how to achieve true
polymorphism without late binding.

Any other ideas are welcome.

Daniel Klein
Cuyahoga Falls, OH
On Tue, 01 Feb 2005 01:46:03 +0100, Mattias Sjögren
<ma************ ********@mvps.o rg> wrote:
How can I achieve this kind of polymorphism with 'Option Strict On' ?


For Each objekt As ICloneable In myArrayListInst ance
objcopy = objekt.Clone()

Mattias


Nov 21 '05 #4
On Tue, 1 Feb 2005 12:28:36 -0000, "Phill. W"
<P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote:
"Daniel Klein" <da***********@ hotmail.com> wrote in message
news:k5******* *************** **********@4ax. com...
With 'Option Strict On' this produces a compiler error.
How can I achieve this kind of polymorphism ... ?


"Object" isn't half as useful as you might think - always try to get at
a proper type when manipulating objects and, since both (all?) of
your classes implement IClonable, that's as good a type as any when
you need to use them, as in :

For Each objekt As IClonable In myArrayListInst ance
objcopy = objekt.Clone() ' returns either Foo or Bar
' build another ArrayList with copies
NewArrayList.Ad d( objekt )
Next

HTH,
Phill W.


Good answer Phil, and it would work if all I was using was the Clone()
method, but the Copy() method in each class is doing more than just
'cloning'.

It looks like this is one place where 'late binding' will have to do.

Dan
Nov 21 '05 #5
Can you use an abstact base class and then derive your other classes
from it to get polymorphism?

Public MustInherit Class FooBarBase
Public MustOverride Function Clone() As FooBarBase
End Class

Public Class Foo
Inherits FooBarBase

Public MyString As String

Public Overrides Function Clone() As FooBarBase
'Code to create a copy of Foo and return it
End Function
End Class

Public Class Bar
Inherits FooBarBase

Public MyString As String

Public Overrides Function Clone() As FooBarBase
'Code to create a copy of Bar and return it
End Function
End Class

Private Sub Button1_Click(. ..) Handles Button1.Click
Dim al As New ArrayList

Dim f1 As New Foo
f1.MyString = "F1"
Dim b1 As New Bar
b1.MyString = "B1"
Dim f2 As New Foo
f2.MyString = "F2"
Dim b2 As New Bar
b2.MyString = "B2"

al.Add(f1)
al.Add(b1)
al.Add(f2)
al.Add(b2)

Dim al2 As New ArrayList

For Each fb As FooBarBase In al
al2.Add(fb.Clon e())
Next

For Each fb As FooBarBase In al2
If fb.GetType Is GetType(Foo) Then
MsgBox("fb is a Foo: " & DirectCast(fb, Foo).MyString)
Else
MsgBox("fb is a Bar: " & DirectCast(fb, Bar).MyString)
End If
Next

End Sub

Then your arraylist class could be strongly typed to hold foobarbase
objects.

Will this work for you?

Chris

Nov 21 '05 #6
Daniel,
Unfortunatel y, this won't work. Each of the Copy() methods is doing a bit
more than what Object.Clone() is doing, which is the reason for creating my
own implementation.
As long as the Copy method Implements ICloneable.Clon e, the same
method is called. So the same work is done.

Besides, this problem address a more generic one of how to achieve true
polymorphism without late binding.


Interface implementation or deriving from a common base class with
virtual methods are great ways of accomplishing that.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #7
On 1 Feb 2005 06:37:38 -0800, "Chris Dunaway" <du******@gmail .com> wrote:
Can you use an abstact base class and then derive your other classes
from it to get polymorphism?
[snip]
For Each fb As FooBarBase In al2
If fb.GetType Is GetType(Foo) Then
MsgBox("fb is a Foo: " & DirectCast(fb, Foo).MyString)
Else
MsgBox("fb is a Bar: " & DirectCast(fb, Bar).MyString)
End If
Next

End Sub

Then your arraylist class could be strongly typed to hold foobarbase
objects.

Will this work for you?


Sorry, but no...at least not in this manner :-(

I thought the purpose of polymorphism was to eliminate cumbersome if/then
logic. Besides, there are more classes that implement the Copy() method,
which means the if/then logic would have to be modifed every time a new
class was created that implements the Copy() method. This goes against the
grain of OOD techniques.

I still maintain that late binding is the only solution to 'pure'
polymorphic behavior such as this.

Daniel Klein
Cuyahoga Falls, OH
Nov 21 '05 #8

The IClonable.Clone () example shown earlier is exactly what you need,
but if you can't see how calling Clone() actually calls Copy() through
explicit interface implementation then you can create your own
interface and use it as follows

Interface ICopiable
Function Copy() As Object
End Interface

Class Foo
Implements ICopiable

Function Copy() Implements ICopiable.Copy
...
End Function
End Class

Class Bar
Implements ICopiable

Function Copy() Implements ICopiable.Copy
...
End Function
End Class

Sub Test()
Dim al As New ArrayList
al.Add(new Foo)
al.Add(new Bar)

For Each obj as ICopiable in al
Dim newObj = obj.Copy()
Next
End Sub

Of course since all the Copy methods you mentioned already implement
IClonable.Clone , then calling Clone is the exact same thing, but if
using a custom ICopiable interface is somehow cleaner or more
understandable, then use that. But don't knock .NET's OOD techniques
when you don't understand how to use them.

Best regards,

Sam


Sorry, but no...at least not in this manner :-(

I thought the purpose of polymorphism was to eliminate cumbersome if/then
logic. Besides, there are more classes that implement the Copy() method,
which means the if/then logic would have to be modifed every time a new
class was created that implements the Copy() method. This goes against the
grain of OOD techniques.

I still maintain that late binding is the only solution to 'pure'
polymorphic behavior such as this.

Daniel Klein
Cuyahoga Falls, OH


Nov 21 '05 #9
I just put the if statement in to show that the correct Clone method
was being called.

Look at the loop that clones the object:

For Each fb As FooBarBase In al
al2.Add(fb.Clon e())
Next

Notice that there is no if then clause here and the correct .clone
method depending on the object is called and the correct copy returned.

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1444
by: Kenneth | last post by:
Should I have it ON or OFF //Kenneth
12
2084
by: Doug Hill | last post by:
Please, Microsoft, update Option Explicit Option Strict barks at late binding. We love late binding. So Option Strict flags too many things. Option Explicit is misleading. It allows Dim LastName rather than forcing
8
1832
by: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a record in a sql Server view) .... Dim entry As Domino.NotesViewEntry Dim obj As Object str1 = entry.ColumnValues(0)
13
2516
by: Shannon Richards | last post by:
Hello: I have a problem using ByRef arguments with Option Strict ON. I have built a generic sub procedure "ChangeValue()" to change the value of an argument if the new value is not the same as the original value...To accommodate all variable types I made the arguments in ChangeValue() of type object...I then check the typecode and do the correct comparison etc... With Option Strict ON I have to cast the arguments to the generic object...
11
1587
by: Dieter Schwerdtfeger via DotNetMonster.com | last post by:
I have this function where i search through my database for items that were made on a specific date. The line "CType(dvClubs.Item(teller).Item(veld).ToShortDateString" gives me the error : option strict on disallows late binding. I have read some posts here about late binding but I just can't figure it out :s This code worked with option strict off, but i just heard that we had to write our program with option strict on :/ I hope you guys can...
3
2006
by: Starbuck | last post by:
Hi The following generates an error when Option Strict is On Can anytell tell me how to get round this please. Private Sub optWithTone_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optWithTone.CheckedChanged If eventSender.Checked Then pAlarmOption = NokiaCLCalendar.CalendarAlarmType.CALENDAR_ALARM_WITH_TONE
6
1574
by: Brett | last post by:
I find there is more casting required in C# than VB.NET. If Option Strict/Explicit is turned on, will this basically create the same environment as C# - uppercase, more casting required, must build event handlers? Thanks, Brett
4
8161
by: Heinz | last post by:
Hi all, I use VB.net 2003 and want to export data to Excel. Target PCs still have Office 2000 so I could not use Microsofts PIAs. Instead I use the included Excel 10 COM DLL from Microsoft. Everything works fine. Now I want to sign my application with a strong name. Therefore I also need to sign all DLLs. So I searched through the web and found information that I need to use tlbimp, and the source file is 'xl5en32.olb'. Now this also...
1
2368
by: Adotek | last post by:
Hi All, I've just converted a solution from .Net v1.1 to v2.0, by allowing Visual Studio 2005 to do the conversion. Since doing so, I am getting a compilation error as follows: "Option Strict On disallows late binding." This references line 1, which is my page directive:
6
423
by: Rob | last post by:
I have employed a "Singleton mode" of programming for this project. I have a class that exposes some properties of the class "Sample" to other forms.... If I set Option Strict On, I get many "Option Strict On disallows late binding" errors (see below) How might I fix this ?
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.