Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.NET and Inheritance

Posted on 2009-04-13
7
Medium Priority
?
371 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

596 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