Question Inheritance in Classes and Extending Subs

AvidGamer

New member
Joined
Feb 26, 2011
Messages
3
Programming Experience
1-3
Hi, I've been wondering if there is anyway to extend a sub of a base class in a derived class or subclass. To date, the only thing I've been able to find is overrides and overloads functions, but to my understanding these functions just ignore your base class's sub and just allows you to run the new one with the same name. What I want to be able to do is:

VB.NET:
      If intGamecounter Mod 30 = 0 Then
            ReDim Preserve clMushroom(clMushroom.Count)
            clMushroom(clMushroom.Count - 1) = New mushroom
            clMushroom(clMushroom.Count - 1).createenemy()
        End If

        If intGamecounter Mod 40 = 0 Then
            ReDim Preserve clTurtle(clTurtle.Count)
            clTurtle(clTurtle.Count - 1) = New turtle
            clTurtle(clTurtle.Count - 1).createenemy()
        End If

Class mushroom
    Dim intRand As New Random
    Public pbMushroom As New PictureBox
    Sub createenemy()
        With pbMushroom
            .Height = 80
            .Width = 80
            .Left = -100
            .Top = intRand.Next(0, 201) + 20
            .Image = Image.FromFile(My.Application.Info.DirectoryPath & "\mushroom.jpg")
            .SizeMode = PictureBoxSizeMode.StretchImage
        End With
        frmGame.Controls.Add(pbMushroom)
    End Sub
End Class

Class turtle
 inherits mushroom
     Sub createenemy()
 ''Somehow Add additional Code onto this sub, so that when runs, it runs the previous
''code in the sub createenemy() of the base class, and then performs the code in this
''sub. is this possible?
     End Sub
 End Class
I'm not sure whether this is possible, or if I'd simply either make a new sub in my derived class that first ran the base's class sub or whether I'd override the base class and then copy and paste the old code. really I'm looking for the best way to accomplish this whatever it may be. Thanks in advance! ;)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
When you override a method a call to the base method is automatically added. The base method must be Overridable of course. You can add code before or after the base call, or choose to not call the base if you prefer.
 

ignyte87

Active member
Joined
Feb 20, 2011
Messages
39
Programming Experience
1-3
What you are describing is the VB.NET Shadows keyword. Shadows does what you describe:
just ignore your base class's sub and just allows you to run the new one with the same name
Your methods do not need to be Overridable (or even exist) to use the Shadows keyword. But since Shadows isn't compiler-checked (and it's bad OO practice anyways), overriding is a better approach.
 

AvidGamer

New member
Joined
Feb 26, 2011
Messages
3
Programming Experience
1-3
Still Trouble

Thanks for the help both of you... but I can't get what you're saying to work. I've looked up a ton of tutorials on the override function, but still don't understand it. so to my understanding what should happen in the code I have now is upon running my object.createenemy sub. the sub from the base class should run, show the message box, and then the base class should run showing the inputbox. but right now it seems like I can't refer to any of the variables from the base class. shadows keyword worked... but then it wouldn't let me add anything to that sub, so didn't work. thanks again for all your help.

VB.NET:
Class mushroom
    Public intRand As New Random
    Public pbMushroom As New PictureBox
    Public strA As String = "a"
    Public Overridable Sub createenemy()
        MsgBox(strA)
        With pbMushroom
            .Height = 80
            .Width = 80
            .Left = 100
            .Top = intRand.Next(0, 201) + 20
            .Image = Image.FromFile(My.Application.Info.DirectoryPath & "\mushroom.jpg")
            .SizeMode = PictureBoxSizeMode.StretchImage
        End With
        frmGame.Controls.Add(pbMushroom)
    End Sub

End Class

Class turtle
    Inherits mushroom

    Public Overrides Sub createenemy()
        InputBox("a")

    End Sub
 

ignyte87

Active member
Joined
Feb 20, 2011
Messages
39
Programming Experience
1-3
You need to call the base class method explicitly.
VB.NET:
Class turtle
    Inherits mushroom

    Public Overrides Sub createenemy()
        MyBase.createenemy()
        InputBox("a")

    End Sub
End Class
 

AvidGamer

New member
Joined
Feb 26, 2011
Messages
3
Programming Experience
1-3
Thanks!

Haha took me a bit to figure out that MyBase was an actual keyword and not you just referring to the base class. that was probably my problem with the tutorials as well. Thank you for all the help!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
Haha took me a bit to figure out that MyBase was an actual keyword and not you just referring to the base class. that was probably my problem with the tutorials as well. Thank you for all the help!
This happened because you added the Overrides keyword after you had defined the method. If you in a class write 'overrides ' you can in the Intellisense drop-down select the member you want to override and it generates the stub including the base call.

Knowing the keywords is useful even still: Me, My, MyBase, and MyClass in Visual Basic and Keywords (Visual Basic)
 
Top Bottom