bmatumbura
asked on
VB.NET and Inheritance
I have a Base Class and a Child Class declared as shown in the attached code.
A number of other Child Classes will be inheriting from the Base Class. My problem is how do I selectively make some of the inherited properties readonly in a Child class?
For example:
Child ClassA may need to have the UserDataHeaderIndicator property readonly whereas another child ClassB may need to have the same property read/write.
A number of other Child Classes will be inheriting from the Base Class. My problem is how do I selectively make some of the inherited properties readonly in a Child class?
For example:
Child ClassA may need to have the UserDataHeaderIndicator property readonly whereas another child ClassB may need to have the same property read/write.
' Base Class '
Public MustInherit Class TransportProtocolDataUnit
Protected mvarMessageTypeIndicator As Integer
Protected mvarUserDataHeaderIndicator As Boolean
Protected mvarProtocolIdentifier As New ProtocolIdentifier
Public Property MessageTypeIndicator() As Integer
Get
Return mvarMessageTypeIndicator
End Get
Protected Friend Set(ByVal value As Integer)
mvarMessageTypeIndicator = value
End Set
End Property
Public MustOverride Property UserDataHeaderIndicator() As Boolean
Public MustOverride Property ProtocolIdentifier() As ProtocolIdentifier
End Class
' Child Class '
Public Class SMSDeliverPDU : Inherits TransportProtocolDataUnit
Public Overrides Property ProtocolIdentifier() As ProtocolIdentifier
Get
End Get
Set(ByVal value As ProtocolIdentifier)
End Set
End Property
Public Overrides Property UserDataHeaderIndicator() As Boolean
Get
End Get
Set(ByVal value As Boolean)
End Set
End Property
End Class
ASKER
Thanks brandonvmoore:
I am writing all the code. I would like to make the properties read only for certain classes and read/write for other classes that derive from the Base Class. The reason why I am doing this is:
I have to develop 6 Child classes that derive from this Base Class. The Base class has a lot of fields that it shares with most of these child classes... but some of the properties must be exposed as readonly on some classes, and read/write on others. What would be the best way to achieve this functionality?
I see you mentioned using interfaces... how can I go about implementing this using the interfaces approach?
Thanks
I am writing all the code. I would like to make the properties read only for certain classes and read/write for other classes that derive from the Base Class. The reason why I am doing this is:
I have to develop 6 Child classes that derive from this Base Class. The Base class has a lot of fields that it shares with most of these child classes... but some of the properties must be exposed as readonly on some classes, and read/write on others. What would be the best way to achieve this functionality?
I see you mentioned using interfaces... how can I go about implementing this using the interfaces approach?
Thanks
Hi you can't AFAIR make the property readonly selectively in child classes, however you can have the same effect using the overrides method and the mybase class the code would be as shown.
The base class has the code required for reading and writing the property. The read/write needs no code as this is already in the base class. The read only calls just the read to make it behave like a read only property. You can still call the write part, but it will have no effect as there is no code in the set half of the property
Hope this helps
The base class has the code required for reading and writing the property. The read/write needs no code as this is already in the base class. The read only calls just the read to make it behave like a read only property. You can still call the write part, but it will have no effect as there is no code in the set half of the property
Hope this helps
Base Class
Public overridable Property UserDataHeaderIndicator() As Boolean
Get
' code for get goes here
End Get
Set(ByVal value As Boolean)
code for set goes here
End Set
End Property
In the child classes for read/write you dont need to do anything as it will be handled by the base class
In the child classes for read only you need to override the base class so that the write part does nothing
Public Overrides Property UserDataHeaderIndicator() As Boolean
Get
return mybase.UserDataHeaderIndicator
End Get
Set(ByVal value As Boolean)
' no code here
End Set
End Property
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have used this solution as a workaround:
http://msdn.microsoft.com/en-us/library/c4swkw24(printer).aspx
http://msdn.microsoft.com/en-us/library/c4swkw24(printer).aspx
You said no "errors will be raised when they assign values to such a property" referring to an implementation you have tried. You are aware that you can raise an error, right? Define your own error or use one that already exists, and raise it if the write property gets called. Of course, that would be in effect at run time and would not be caught at compile time, but I just wanted to make sure you knew that you could do that.
ASKER
Thanks brandonvmoore:
Yeah, I could have used
Throw New Exception("...")
to raise an error at runtime if someone attempted to assign a value to such a property but that wasn't exactly what I would have wanted. I managed to get it working though by using the Shwadows attribute as documented here: http://msdn.microsoft.com/en-us/library/c4swkw24(printer).aspx
Yeah, I could have used
Throw New Exception("...")
to raise an error at runtime if someone attempted to assign a value to such a property but that wasn't exactly what I would have wanted. I managed to get it working though by using the Shwadows attribute as documented here: http://msdn.microsoft.com/en-us/library/c4swkw24(printer).aspx
You could make it a property instead of a member variable so that you could implement logic inside the property to reject or accept the call to change the property based on who is making the call.
Are you just wanting to make it read only for certain classes because those classes don't happen to use it? Are you writing all the code or are there other people that will be implementing these classes? I don't really understand why you would 'need' to do this, but if you could explain maybe I could help you find a better approach.
Here's one idea for a better approach:
Typically, you might make something read only if the base class manages it and while you want child classes to be able to see it, there would be problems if they were able to update it directly. But let's say that one of your child classes needs to manage this value for itself for whatever reason. Instead of letting it have direct access to the property, give it the option to implement an interface that would do the managing instead of the base classes implementation of the interface.