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
12 1557
IClonaeable.Clo ne() 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****@newsgro up.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.Clon e, 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
The thing is, ICloneable.Clon e() and Copy() are the exact same method.
When you declare
Function Copy() As Object Implements ICloneable.Clon e
Then you can execute this method via
obj.Copy()
or
DirectCast(obj, ICloneable).Clo ne()
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.Cl one() will never work in this instance due the the 'extra work' the Copy() methods have to do. However the ICopiable solution has possibilitie s, I'll let you know. Thanks.
What I was 'knocking' was the if/then logic, not .NET. Apology accepted ;-)
Dan
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****@newsgro up.nospam> wrote: The thing is, ICloneable.Clon e() and Copy() are the exact same method. When you declare
Function Copy() As Object Implements ICloneable.Clon e
Then you can execute this method via
obj.Copy()
or
DirectCast(obj , ICloneable).Clo ne()
And it calls the exact same method. This is called explicit interface implementati on 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.C lone() 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kenneth |
last post by:
Should I have it ON or OFF
//Kenneth
|
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
|
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)
|
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...
|
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...
| |
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
|
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
|
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...
|
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:
|
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 ?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |