473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.Clone
' returns a copy of a Foo object
End Function

Public Function Copy() As Object Implements ICloneable.Clone
' 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 myArrayListInstance
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 1523
>How can I achieve this kind of polymorphism with 'Option Strict On' ?

For Each objekt As ICloneable In myArrayListInstance
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.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 myArrayListInstance
objcopy = objekt.Clone() ' returns either Foo or Bar
' build another ArrayList with copies
NewArrayList.Add( 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.org> wrote:
How can I achieve this kind of polymorphism with 'Option Strict On' ?


For Each objekt As ICloneable In myArrayListInstance
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 myArrayListInstance
objcopy = objekt.Clone() ' returns either Foo or Bar
' build another ArrayList with copies
NewArrayList.Add( 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.Clone())
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,
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.
As long as the Copy method Implements ICloneable.Clone, 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.Clone())
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
IClonaeable.Clone() will never work in this instance due the the 'extra
work' the Copy() methods have to do. However the ICopiable solution has
possibilities, I'll let you know. Thanks.

What I was 'knocking' was the if/then logic, not .NET. Apology accepted ;-)

Dan
On Tue, 01 Feb 2005 12:22:16 -0500, Samuel R. Neff
<bl****@newsgroup.nospam> wrote:

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 #11

The thing is, ICloneable.Clone() and Copy() are the exact same method.
When you declare

Function Copy() As Object Implements ICloneable.Clone

Then you can execute this method via

obj.Copy()

or

DirectCast(obj, ICloneable).Clone()

And it calls the exact same method. This is called explicit interface
implementation where you're implementing an interface method but
internally using a different method name. This way when the object is
declared as the type that defined it, "Clone" is not accessible, but
"Copy" is. But you can cast it to ICloneable and then "Clone" is
accessible. But no matter what you do, it calls the exact same code,
the contents of the function.

Sam
On Wed, 02 Feb 2005 15:13:18 GMT, Daniel Klein
<da***********@hotmail.com> wrote:
IClonaeable.Clone() will never work in this instance due the the 'extra
work' the Copy() methods have to do. However the ICopiable solution has
possibilities, I'll let you know. Thanks.

What I was 'knocking' was the if/then logic, not .NET. Apology accepted ;-)

Dan

Nov 21 '05 #12
Just to put some closure on this, the piece I was missing was to do:

For Each objekt As ICloneable In myArrayList

It was the 'As ICloneable' that was missing.

Thanks Samuel, I has been educational to say the least.

Daniel Klein
On Wed, 02 Feb 2005 10:21:51 -0500, Samuel R. Neff
<bl****@newsgroup.nospam> wrote:

The thing is, ICloneable.Clone() and Copy() are the exact same method.
When you declare

Function Copy() As Object Implements ICloneable.Clone

Then you can execute this method via

obj.Copy()

or

DirectCast(obj, ICloneable).Clone()

And it calls the exact same method. This is called explicit interface
implementation where you're implementing an interface method but
internally using a different method name. This way when the object is
declared as the type that defined it, "Clone" is not accessible, but
"Copy" is. But you can cast it to ICloneable and then "Clone" is
accessible. But no matter what you do, it calls the exact same code,
the contents of the function.

Sam
On Wed, 02 Feb 2005 15:13:18 GMT, Daniel Klein
<da***********@hotmail.com> wrote:
IClonaeable.Clone() will never work in this instance due the the 'extra
work' the Copy() methods have to do. However the ICopiable solution has
possibilities, I'll let you know. Thanks.

What I was 'knocking' was the if/then logic, not .NET. Apology accepted ;-)

Dan


Nov 21 '05 #13

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

Similar topics

7
by: Kenneth | last post by:
Should I have it ON or OFF //Kenneth
12
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...
8
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...
13
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...
11
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...
3
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...
6
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...
4
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....
1
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...
6
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.