Results 1 to 7 of 7

Thread: [RESOLVED] Same interface, base and derived class.

  1. #1

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Resolved [RESOLVED] Same interface, base and derived class.

    I'm struggling here.. I know I can disable the warning manually. However I'm not sure if this would be the right way to go about this.

    Here's my interface..
    vbnet Code:
    1. Public Interface IPopulatingClassBase
    2.     Property Identity() As Int64
    3.     ReadOnly Property Dirty() As Boolean
    4.     Sub Populate_Class(ByVal RecordID As Int64)
    5.     Sub Save_Class()
    6.     Sub Reset_Class()
    7. End Interface


    Here's my parent class...
    vbnet Code:
    1. Public Class Parent
    2. Implements IPopulatingClassBase
    3.  
    4.     Public Property Identity() As Int64 _
    5.     Implements IPopulatingClassBase.Identity
    6.         '...
    7.     End Property
    8.  
    9.     Private Dirty() As Boolean = {False, False}
    10.     Public ReadOnly Property Dirty() As Boolean _
    11.     Implements IPopulatingClassBase.Dirty
    12.         '...
    13.     End Property
    14.  
    15.  
    16.     Friend Sub Populate_Class(ByVal ParentID As Int64) _
    17.     Implements IPopulatingClassBase.Populate_Class
    18.         '...
    19.     End Sub
    20.  
    21.     Friend Sub Save_Class() _
    22.     Implements IPopulatingClassBase.Save_Class
    23.         '...
    24.     End Sub
    25.  
    26.     Public Sub Reset_Class() _
    27.     Implements IPopulatingClassBase.Reset_Class
    28.         '...
    29.     End Sub
    30.  
    31. End Class


    Here's my derived class..
    vbnet Code:
    1. Public Class Derived
    2. Inherits Parent
    3. Implements IPopulatingClassBase
    4.  
    5.     Public Property Identity() As Int64 _
    6.     Implements IPopulatingClassBase.Identity
    7.         '...
    8.     End Property
    9.  
    10.     Private Dirty() As Boolean = {False, False}
    11.     Public ReadOnly Property Dirty() As Boolean _
    12.     Implements IPopulatingClassBase.Dirty
    13.         '...
    14.     End Property
    15.  
    16.  
    17.     Friend Shadows Sub Populate_Class(ByVal DerivedID As Int64) _
    18.     Implements IPopulatingClassBase.Populate_Class
    19.         '...
    20.     End Sub
    21.  
    22.     Friend Shadows Sub Save_Class() _
    23.     Implements IPopulatingClassBase.Save_Class
    24.         '...
    25.     End Sub
    26.  
    27.     Public Shadows Sub Reset_Class() _
    28.     Implements IPopulatingClassBase.Reset_Class
    29.         '...
    30.     End Sub
    31.  
    32. End Class

    I obviously get the warnings in the derived class, but I feel this should be allowed. I've read vb.net doesn't whereas c# does. Is there a way around this or should I just create separate interfaces? (Which I feel defeats the entire purpose of an interface.)

    PS In my example think of it as classes that populate with data from a database. The parent class would be an organization, and the derived class a location. Both populate and have similar functionality. The only difference is locations are an entity of an organization(the data record for a location constraints it to an organization) upon loading location data I also populate the organization class. This is a real example of what my project does.. so having said I just want to know how I can get around this warning, and if not would it have any consequences if I left it as-is.


    Thanks in advance
    David
    Last edited by DavesChillaxin; May 11th, 2012 at 11:03 AM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Same interface, base and derived class.

    you shouldn't be inheriting a class that also implement the interface you're trying to implement.... one or the other....

    your base parent class as property called Identity... because it implemented it from the interface.
    Now your class ALSO implements it... AND inherits from a class that already implemented Identity... in essence, you have TWO properties Identity... that just doesn't make sense.

    you can implement an interface AND inherit from a class at the same time. That's not the problem. The problem is you inherited from a class that implemented the same interface you're trying to implement again.

    If I were to call DerivedClass.Populate_Class.... which one should it call? the inherited one? or the implemented one?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Same interface, base and derived class.

    I agree with techgnome here. If you're wanting to be able to redefine the base class function then declare it as Overridable.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Same interface, base and derived class.

    I would suggest considering making the base class a pure virtual that doesn't implement the interface and can't be instantiated. My reasoning is that you implemented the interface in the base class because you felt that an instance of the base class would be needed at times. However, you have run into a problem with the base and the derived both implementing the interface. Therefore, you can most readily avoid having the base class need to implement the interface by removing the possibility of the base class ever being instantiated...conceptually (you can always prevent it physically, you just want to remove the perception of the instance ever being needed). Any situation where you would currently use an instance of the base class can be alterered to a situation where you use an instance of a derived class if you make a derived class that implements the interface but otherwise provides only the base class functionality. You would then be able to remove the interface from the base class and mark it Must Override.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Same interface, base and derived class.

    Quote Originally Posted by techgnome View Post
    you shouldn't be inheriting a class that also implement the interface you're trying to implement.... one or the other....

    your base parent class as property called Identity... because it implemented it from the interface.
    Now your class ALSO implements it... AND inherits from a class that already implemented Identity... in essence, you have TWO properties Identity... that just doesn't make sense.

    you can implement an interface AND inherit from a class at the same time. That's not the problem. The problem is you inherited from a class that implemented the same interface you're trying to implement again.

    If I were to call DerivedClass.Populate_Class.... which one should it call? the inherited one? or the implemented one?

    -tg
    Well it does make sense, an organization record has an Identity as does a location. Both have identities. They're not necessarily both called Identity, they're actually OrganizationID and LocationID. I should have demonstrated this in my example, but both are considered to be an identity of that class.

    The problem is an organization and location class act as the same, they populate with data. But a location is constricted to an organization therefore inherits the organization class. So when I call to populate the location class, that method in turn calls it's base populating method.


    @MattP These methods do shadow when they inherit the parent class. I did try the "correct" method of overriding but I was getting some odd behavior, pretty much the opposite of what I wanted so I stuck with shadows for now until I can figure it out.


    PS I'll demonstrate the shadows in the original post, forgot I left that out.
    Last edited by DavesChillaxin; May 11th, 2012 at 12:39 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Same interface, base and derived class.

    Just because two objects have similar properties (and thus can implement the same interfaces) doesn't mean they should.

    Indeed, your example of 'Organization' and 'location' do not imply they have a parent child relationship, and so one shouldn't inherit from one or the other.

    It sounds like that you should have an 'Entity' class, which implements your interface, and the Organization and Location objects inherit from the Entity object.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  7. #7

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Same interface, base and derived class.

    Actually you guys were all correct.

    It took me some time to get a grip on, but before I was trying to fix what wasn't mine, so with how it was structured made it difficult to suddenly implement interfaces. Thankfully, I began making a project in my spare time, a movie database library with GUI. So by starting at the beginning and structuring the different classes in a more appropriate way. I was able to use interfaces as I had originally hoped, and in the ways instructed here. A little more simple though than the project I'm working on at work but none-the-less, everything looks and works beautifully.

    Also sorry it took me so long to work, busy at work. Thanks again guys!
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width