spacer    
Blog Search
Others' posts of interest
Recent del.icio.us tags
Consumer evaluation of mental health and substance abuse providers - sharing experiences on the web
Apr 10

Written by: Matthew Hile
4/10/2007 4:09 AM

I am a lazy programmer - If I can figure out a way for the computer to do something for me I am all over it. Recently I was working on a DNN module and noticed that my class definitions of various objects were remarkably similar. I cut and pasted from one into another. For example these two info classes contain the same PortalID and ModuleID properties.

Public Class EventInfo
        Private _PortalId As Integer
        Private _ModuleId As Integer
        Private _EventId As Integer

        Public Property PortalId() As Integer
            ...
        End Property

        Public Property ModuleId() As Integer
            ...
        End Property

        Public Property EventId () As Integer
            ...
        End Property
   End Class
Public Class UserReminderInfo
        Private _PortalId As Integer
        Private _ModuleId As Integer
        Private _UserReminderId As Integer

        Public Property PortalId() As Integer
            ...
        End Property

        Public Property ModuleId() As Integer
            ...
        End Property

        Public Property UserReminderId () As Integer
            ...
        End Property
   End Class

It irritated me that I needed to copy and past the duplicate code. I made errors not copying enough of the code. It also worried me that if I changed it in one place I would need to change it in others.

So enter Object Oriented Programming (OOP) and inheritance in the form of .NET Abstract classes. To set this up I created a new base class that would be shared with these (and other classes). Using the MustInherit key word and declaring the common properties.

    Public MustInherit Class InfoBase
        Private _PortalId As Integer
        Private _ModuleId As Integer

       Public Property PortalId() As Integer
            ...
        End Property

        Public Property ModuleId() As Integer
            ...
        End Property
    End Class

Now I can simplify my other two classes by having them Inherit the InfoBase information as show below.

Public Class EventInfo
        Inherits InfoBase
        Private _EventId As Integer

        Public Property EventId() As Integer
            ...
        End Property
   End Class
Public Class UserReminderInfo
        Inherits InfoBase
        Private _UserReminderId As Integer

        Public Property UserReminderId() As Integer
            ...
        End Property
   End Class

I save time by not having to retype or cut-and-paste and reduce errors that may creep in if I need to go back and redefine any of the shared properties. A win-win. For more information on abstract classes check out http://www.devx.com/dotnet/Article/28086/1954 or http://www.startvbdotnet.com/oop/abstract.aspx.

Tags:

Your name:
Title:
Comment:
Add Comment    Cancel  
Disclaimer
NOTE: The ideas, opinions, and viewpoints expressed in these entries are solely those of the author.
There are no categories in this blog.
 
Missouri Institute of Mental Health    Terms Of Use    Privacy Statement