ReadOnly Property Inheritance

J

James.R.Thigpen

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
 
H

Herfried K. Wagner [MVP]

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').
 
R

rowe_newsgroups

Ok, Thanks. Does C# let you do this or is it a .net idiom?

James

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.

Thanks,

Seth Rowe [MVP]
 
R

rowe_newsgroups

Seth --








No, it's actually a VB limitation.  C# lets you do it.

--
 M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
 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]
 
H

Herfried K. Wagner [MVP]

rowe_newsgroups said:
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.
 
S

Steve Gerrard

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)
 
R

rowe_newsgroups

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.

--
 M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
 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]
 
J

James.R.Thigpen

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)

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
 
T

Terry

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

Steve Gerrard

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.
 

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