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