Solved

VB.NET and Inheritance

Posted on 2009-04-13
7
365 Views
Last Modified: 2013-11-26
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.
' 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

Open in new window

0
Comment
Question by:bmatumbura
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24135590
Making it readonly or not would be done in the base class, but how is the base class going to know the needs of the child class?  The only way is if the child class somehow indicated what its needs were.  But if the child class is empowered to make that decision then that defeats the purpose.

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.

0
 
LVL 11

Author Comment

by:bmatumbura
ID: 24136268
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
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 24136317
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
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

Open in new window

0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 11

Accepted Solution

by:
bmatumbura earned 0 total points
ID: 24136442
Hi ChloesDad:

I had already done that but using the MustInherit attribute. Though it works, but users of the class will not be able to tell if the property is readonly or not. More so, no errors will be raised when they assign values to such a property... which is what I would like to avoid.

Isn't there a way to make this work? I am now thinking of doing away with inheritance and create the properties and methods in each class.
0
 
LVL 11

Author Comment

by:bmatumbura
ID: 24137472
I have used this solution as a workaround:

http://msdn.microsoft.com/en-us/library/c4swkw24(printer).aspx
0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24142046
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.
0
 
LVL 11

Author Comment

by:bmatumbura
ID: 24145699
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
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question