Solved

VB.NET and Inheritance

Posted on 2009-04-13
7
355 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
  • 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now