Say I have a Part, which I want to Implement IReadOnlyPart.
IReadOnlyPart will have
ReadOnly Property PartNumber() As String
And I *want* Part to have
Public Property PartNumber() As String Implements
IPart.PartNumber
Get
Return partNumberValue
End Get
Set(ByVal value As String)
partNumberValue = value
End Set
End Property
But that is a compile error "Error 72 'PartNumber' cannot implement
'PartNumber' because there is no matching property on interface
'IPart'."
Is there any way to get this to work or is this simply not How Things
Are Done.
Thanks,
James Thigpen
Unfortunately what you want to archieve is not supported by the VB compiler.
You may want to implement the 'Set' part as a function instead ('Function
SetPartNumber').
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
James
On Feb 27, 12:18 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> <James.R.Thig...@gmail.com> schrieb:
It's a CLR thing so it's not just VB.
Thanks,
Seth Rowe [MVP]
"rowe_newsgroups" <rowe_...@yahoo.com> schrieb:
>> > >I am trying to create a read-only interface for an object.
>>
>> > > Say I have a Part, which I want to Implement IReadOnlyPart.
>> > > IReadOnlyPart will have
>>
>> > > ReadOnly Property PartNumber() As String
>>
>> > > And I *want* Part to have
>>
>> > > Public Property PartNumber() As String Implements
>> > > IPart.PartNumber
>> > > Get
>> > > Return partNumberValue
>> > > End Get
>> > > Set(ByVal value As String)
>> > > partNumberValue = value
>> > > End Set
>> > > End Property
>>
>> > > But that is a compile error "Error 72 'PartNumber' cannot implement
>> > > 'PartNumber' because there is no matching property on interface
>> > > 'IPart'."
>>
>> > > Is there any way to get this to work or is this simply not How Things
>> > > Are Done.
>>
>> > Unfortunately what you want to archieve is not supported by the VB
>> > compiler.
>> > You may want to implement the 'Set' part as a function instead
>> > ('Function
>> > SetPartNumber').
>
> It's a CLR thing so it's not just VB.
No, it's actually a VB limitation. C# lets you do it.
Really? If so this error is really confusing:
"'ConsoleApplication1.bar.prop.set': cannot override because
'ConsoleApplication1.foo.prop' does not have an overridable set
accessor"
////////////////
class foo
{
public virtual string prop
{
get
{
return "hello, world";
}
}
}
class bar : foo
{
private string _prop = "no go?";
public override string prop
{
get
{
return _prop;
}
set
{
_prop = value;
}
}
}
////////////////
Thanks,
Seth Rowe [MVP]
Well, the OP is talking about a class implementing an interface which
contains a read-only property and your example shows a class attempting to
override a virtual read-only property.
I would do it like this. The Class1 property implementing IPart.PartNumber does
not have to be public, and does not have to be named the same:
Public Interface IReadOnlyPart
ReadOnly Property PartNumber() As String
End Interface
Public Class Class1
Implements IReadOnlyPart
Private PartNum As String
Private ReadOnly Property Dummy() As String _
Implements IReadOnlyPart.PartNumber
Get
Dummy = PartNum
End Get
End Property
Public Property PartNumber() As String
Get
PartNumber = PartNum ' or Dummy
End Get
Set(ByVal Value As String)
PartNum = Value
End Set
End Property
End Class
Sample test code:
Dim o As Class1 = New Class1
Dim p As IReadOnlyPart = o
o.PartNumber = "test 001"
Debug.WriteLine("Class1: " + o.PartNumber)
Debug.WriteLine("IReadOnlyPart: " + p.PartNumber)
That could be the reason :-)
I jumped in mid topic and thought we were talking about inheritance,
not interfaces.
Thanks,
Seth Rowe [MVP]
Wow. I didn't know the derived properties could be named something
different. That's kind of... perverse. But it gets the job done. Are
there any side-effects that I'm not considering to this approach that
I should be aware of?
Thanks,
-jt
If the method doing the implementing is Private, then there should be side
effect. The name of the method won't get used by anything, unless you call it
yourself within the class for some reason.
As Terry said, you are implementing a method, not overriding one. So the
Implements IPart.PartNumber is "hooking it up", and the method name is
irrelevant if it is private.