How to implements the interface System.ICloneable in Visual Basic 2008?

A

Armin Zingler

Dr. Zharkov said:
Hello. In project Visual Studio 2008, Visual C#, Windows Forms
Application, I have written down such code on C# without errors:

public class DeviceSettings : ICloneable

{

/// <summary>Clone this object</summary>

public DeviceSettings Clone()

{

DeviceSettings clonedObject = new DeviceSettings();

//...

return clonedObject;

}

/// <summary>Clone this object</summary>

object ICloneable.Clone()

{ throw new NotSupportedException("Use ...");

}

}



The same code, I have copied in the project Visual Studio 2008,
Visual Basic, Windows Forms Application in such kind on Visual
Basic:

Public Class DeviceSettings

Implements ICloneable

''' <summary>Clone this object</summary>

Public Function Clone() As DeviceSettings

Dim clonedObject As DeviceSettings = New DeviceSettings()

'...

Return clonedObject

End Function

''' <summary>Clone this object</summary>

Public Function Clone() As Object Implements ICloneable.Clone

Throw New NotSupportedException("Use...")

End Function

End Class

The compiler gives out the following error:

Public Function Clone() As DeviceSettings' and 'Public Function
Clone() As Object' cannot overload each other because they differ
only by return types.



Inform, please, how correctly to write down this code on Visual
Basic?


In VB.Net, the contract is fulfilled by using the "Implements" keyword
in the function decleration, not by the function name. This means, you
can use any function name you want with the function implementing the
method of the interface. For example:

Public Class DeviceSettings

Implements ICloneable

''' <summary>Clone this object</summary>

Public Function Clone() As DeviceSettings

Dim clonedObject As DeviceSettings = New DeviceSettings()

'...

Return clonedObject

End Function

''' <summary>Clone this object</summary>

Private Function ICloneable_Clone() As Object _
Implements ICloneable.Clone

Throw New NotSupportedException("Use...")

End Function

End Class


Note that the 2nd function is private now because of the function name.
(Accessing the method via the Interface is still done with the name
'Clone')


BTW, it's funny to implement an interface in order to throw a
NotSupportedException. :) (I guess it's not the real-world code).


Armin
 
A

Armin Zingler

Dr. Zharkov said:
Mr. Armin Zingler.

Many thanks for your answer. It is possible to ask to inform your
email, to ask you to look a code in Visual Basic 2008, which because
of the big length cannot be presented at this forum. Thanks.
Zharkov.

In this group you will reach more people that can help you. Before, try
narrowing the problem down to few lines of code.

To send a mail, choose reply to the sender of this pots and insert a "_"
between "no" and "spam" in my address.


Armin
 
D

Dr. Zharkov

Hello. In project Visual Studio 2008, Visual C#, Windows Forms Application,
I have written down such code on C# without errors:

public class DeviceSettings : ICloneable

{

/// <summary>Clone this object</summary>

public DeviceSettings Clone()

{

DeviceSettings clonedObject = new DeviceSettings();

//...

return clonedObject;

}

/// <summary>Clone this object</summary>

object ICloneable.Clone()

{ throw new NotSupportedException("Use ...");

}

}



The same code, I have copied in the project Visual Studio 2008, Visual
Basic, Windows Forms Application in such kind on Visual Basic:

Public Class DeviceSettings

Implements ICloneable

''' <summary>Clone this object</summary>

Public Function Clone() As DeviceSettings

Dim clonedObject As DeviceSettings = New DeviceSettings()

'...

Return clonedObject

End Function

''' <summary>Clone this object</summary>

Public Function Clone() As Object Implements ICloneable.Clone

Throw New NotSupportedException("Use...")

End Function

End Class

The compiler gives out the following error:

Public Function Clone() As DeviceSettings' and 'Public Function Clone() As
Object' cannot overload each other because they differ only by return types.



Inform, please, how correctly to write down this code on Visual Basic?

Many thanks, Zharkov, Moskva, Rossiya.
 
D

Dr. Zharkov

Mr. Armin Zingler.

Many thanks for your answer. It is possible to ask to inform your email, to
ask you to look a code in Visual Basic 2008, which because of the big length
cannot be presented at this forum. Thanks. Zharkov.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top