Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ReadOnly Property Inheritance

0 views
Skip to first unread message

James.R...@gmail.com

unread,
Feb 27, 2008, 3:14:56 PM2/27/08
to
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.

Thanks,

James Thigpen

Herfried K. Wagner [MVP]

unread,
Feb 27, 2008, 3:18:04 PM2/27/08
to
<James.R...@gmail.com> schrieb:

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.R...@gmail.com

unread,
Feb 27, 2008, 4:06:53 PM2/27/08
to
Ok, Thanks. Does C# let you do this or is it a .net idiom?

James

On Feb 27, 12:18 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> <James.R.Thig...@gmail.com> schrieb:

rowe_newsgroups

unread,
Feb 27, 2008, 4:16:02 PM2/27/08
to

It's a CLR thing so it's not just VB.

Thanks,

Seth Rowe [MVP]

Herfried K. Wagner [MVP]

unread,
Feb 27, 2008, 5:17:06 PM2/27/08
to
Seth --

"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.

rowe_newsgroups

unread,
Feb 27, 2008, 9:05:35 PM2/27/08
to
On Feb 27, 5:17 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> Seth --
>
> "rowe_newsgroups" <rowe_em...@yahoo.com> schrieb:
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>- Hide quoted text -
>
> - Show quoted text -

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]

Herfried K. Wagner [MVP]

unread,
Feb 27, 2008, 9:50:16 PM2/27/08
to
"rowe_newsgroups" <rowe_...@yahoo.com> schrieb:
>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"

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.

Steve Gerrard

unread,
Feb 27, 2008, 10:43:29 PM2/27/08
to
James.R...@gmail.com wrote:
> 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'."
>

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)

rowe_newsgroups

unread,
Feb 28, 2008, 8:18:00 AM2/28/08
to
On Feb 27, 9:50 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> "rowe_newsgroups" <rowe_em...@yahoo.com> schrieb:
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>- Hide quoted text -
>
> - Show quoted text -

That could be the reason :-)

I jumped in mid topic and thought we were talking about inheritance,
not interfaces.

Thanks,

Seth Rowe [MVP]

James.R...@gmail.com

unread,
Feb 28, 2008, 2:20:43 PM2/28/08
to
On Feb 27, 7:43 pm, "Steve Gerrard" <mynameh...@comcast.net> wrote:

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

Terry

unread,
Feb 28, 2008, 6:52:06 PM2/28/08
to
Well, if they could not be named differntly, then what would the purpose of
the "Implements" clause be? ;~)
Just to be consise, it is not a 'derived' property, you are implementing an
interface. And I know of no downfalls, at least I have not come accross any
yet. I use this technique all the time to provide a different 'look' to
clients of the class.
Also, you will notice that if you add the Implements ISomeInterface to the
class after you have set up your properties/functions etc. the IDE will add
new properties/functions with different names. I just wish that it would make
them Private by default instead of public.
--
Terry

Steve Gerrard

unread,
Feb 28, 2008, 10:56:14 PM2/28/08
to
James.R...@gmail.com wrote:
>
> 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?
>

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.


0 new messages