Overrides and Overridable...

Loffen

Well-known member
Joined
Mar 21, 2004
Messages
51
Location
Akershus, Norway
I have two classes, like these:

Code:
Class MainClass
    Inherits SubClass

    Private NewClass as SubClass

    Public Function Test()
        NewClass = new SubClass

        Messagebox.show("TestBox :p")
        NewClass.DoSomething()
    End Function

    Public Overrides SubFunction()
        Messagebox.show("Yup it works!!")
    End Function
End Class

Class SubClass
    Public Function DoSomething()
        Do something
        SubFunction
        Do something
    End Function

    Public Overridable Function SubFunction()
        Should be overwritten
    End Function
End Class

But only the first messagebox appears... What am i doing wrong? I get no errors or anything..
 
Last edited by a moderator:
Just looking at your code - what did you expect to happen? Havent got VS installed here so I cant step through it and it looks a bit convaluted to follow through by hand.
It should display a message box saying "TestBox :p" - is that happening?
In the line
Code:
SubClass.DoWhatever()
did you mean
Code:
newClass.DoWhatever()
 
did you mean
Code:
newClass.DoWhatever()
Err... Yeah i did mean that... *Fixed*

Actually this is a part of my game made with DirectX, Where SubFunction is the Render() function... Posting the class again whith changes... Hope it get clearer then...

Code:
[COLOR=Blue]Class[/COLOR] GameEngine
    [COLOR=Blue]Inherits[/COLOR] GraphicsEngine

    [COLOR=Blue]Private[/COLOR] Graphics [COLOR=Blue]as[/COLOR] GraphicsEngine

    [COLOR=Green]This is called from form1..[/COLOR]
    [COLOR=Blue]Public Function[/COLOR] Init()
        Graphics = [COLOR=Blue]new[/COLOR] GraphicsEngine

        Messagebox.show("Init Success")
        Graphics.RenderLoop()
    [COLOR=Blue]End Function[/COLOR]

    [COLOR=Blue]Public Overrides Function[/COLOR] Render()
        Messagebox.show("In overrides Render()")
    [COLOR=Blue]End Function
End Class[/COLOR]

[COLOR=Blue]Class[/COLOR] GraphicsEngine
    [COLOR=Blue]Public Function[/COLOR] RenderLoop()
        Device.Clear()
        DeviceBeginScene()

        Render()

        Device.EndScene()
        Device.Present()
    [COLOR=Blue]End Function[/COLOR]

    [COLOR=Blue]Public Overridable Function[/COLOR] Render()
        [COLOR=Green]Should be overwritten[/COLOR]
    [COLOR=Blue]End Function
End Class[/COLOR]
 
Last edited by a moderator:
Thats by design. If you want to run the base version you need to call it from the overridden version.
Code:
    Public Overrides Function Render()
        MyBase.Render()
        MessageBox.Show("In overrides Render()")
    End Function
 
Neee.... not 100% sure what you mean?? I want to run the code inside: Public Overrides Function Render(). The overwritten function is only there so my Graphics Class knows what function to call uring rendering. And as the object rendered are different from game to game. Later i probably will put the whole Graphics class into a dll, thets why i need the function so the caller can render whatever the caller wants

Public Overridable Function Render()
Should be overwritten. Nothing inside here sould be executed, only inside the other Render()
End Function

Public Overrides Function Render()
Messagebox.show("In overrides Render()")
Here the code sould be executed...
End Function

!! It wont let me place MyBase.Render() inside Public Overridable Function Render()
 
Sorry - mis-understood your problem.

In your init method you are declaring a variable of type GraphicsEngine - so when you call render you will get the GraphicsEngine version of the function.

What you need to do is declare a variable of type GameEngine:
Code:
    Public Function Init()
        Graphics = New GameEngine

        Messagebox.show("Init Success")
        Graphics.RenderLoop()
    End Function
 
Didnt understand that 100% ether? Ill post my GameEngine Class... The code i am talking about is almost at the bottom..

Code:
[COLOR=Blue]Imports[/COLOR] Microsoft.DirectX
[COLOR=Blue]Imports[/COLOR] Microsoft.DirectX.Direct3D
[COLOR=Blue]Imports[/COLOR] Microsoft.DirectX.Direct3D.D3DX
[COLOR=Blue]Imports[/COLOR] System.Math

[COLOR=Blue]Public Class[/COLOR] GameEngine
    [COLOR=Blue]Inherits[/COLOR] GraphicsEngine

    [COLOR=Blue]Public[/COLOR] Graphics [COLOR=Blue]As[/COLOR] GraphicsEngine

    [COLOR=Blue]Public Function[/COLOR] Init([COLOR=Blue]ByVal[/COLOR] Target [COLOR=Blue]As[/COLOR] System.Windows.Forms.Form)
        Graphics = [COLOR=Blue]New[/COLOR] GraphicsEngine

        Graphics.BackBufferWidth = 800
        Graphics.BackBufferHeight = 600
        Graphics.BackBufferFormat = Format.A8R8G8B8
        Graphics.BackBufferCount = 1

        Graphics.SwapEffect = SwapEffect.Copy
        Graphics.PresentInterval = PresentInterval.One
        Graphics.CreateFlags = CreateFlags.HardwareVertexProcessing [COLOR=Blue]Or[/COLOR] CreateFlags.MultiThreaded
        Graphics.DeviceType = DeviceType.Hardware

        Graphics.GraphicsTarget = Target

        Graphics.GraphicsInitDevice()

        [COLOR=Green]This is the map object[/COLOR]
        Graphics.Map = [COLOR=Blue]New[/COLOR] GraphicsMap
        Graphics.Map.LoadMap(Graphics.Device, "testmap")

        Graphics.Player = [COLOR=Blue]New[/COLOR] GraphicsSprite(Graphics.Device, Application.StartupPath & "\objects\human1\human1.bmp", New Point(0, 0), 16, 16, &HFF00FF00)

        [COLOR=Green]//--------------------------------------------------
        // Different font types
        //--------------------------------------------------[/COLOR]
        Graphics.MessageText = [COLOR=Blue]New[/COLOR] GraphicsText(Graphics.Device, "Verdana", 8, FontStyle.Bold)
        Graphics.MissionText = [COLOR=Blue]New[/COLOR] GraphicsText(Graphics.Device, "Verdana", 18)
        Graphics.HUDText = [COLOR=Blue]New[/COLOR] GraphicsText(Graphics.Device, "Verdana", 22, FontStyle.Bold)

        [COLOR=Blue]While[/COLOR] Graphics.GraphicsRunning = [COLOR=Blue]True[/COLOR]
            [COLOR=Green]This is the function wich again calls the Overridable Render() function..[/COLOR]
[COLOR=Red][B] !! HERE !![/B][/COLOR]
            Graphics.GraphicsRenderLoop()

            Application.DoEvents()
        [COLOR=Blue]End While[/COLOR]

        Graphics.Player.Destroy()
        Graphics.Map.DestroyMap()

[COLOR=Blue]        End
    End Function

[COLOR=Red][B]!! AND HERE !![/B][/COLOR]
    Public Overrides Function[/COLOR] Render()
        [COLOR=Green]The message box wont pop up...[/COLOR]
        MessageBox.Show("Inside the correct render function!!")
        [COLOR=Blue]End[/COLOR]
    [COLOR=Blue]End Function

End Class[/COLOR]
 
At the top you have
Code:
Public Class GameEngine
    Inherits GraphicsEngine

    Public Graphics As GraphicsEngine
this means you are declaring a variable called Graphics of type GraphicsEngine

later on you have
Code:
While Graphics.GraphicsRunning = True
            This is the function wich again calls the Overridable Render() function..
            Graphics.GraphicsRenderLoop()

            Application.DoEvents()
        End While
in this code you are calling the GraphicsRenderLoop method of the Graphics object, within the Graphics objects GraphicsRenderLoop you are making a call to a Render method - because the variable Graphics was declared as type GraphicsEngine you get the GraphicsEngines version of this method.

If you want to call the overridden method declared in the GameEngine class then you need to declare a variable of Type GameEngine.

However because your GameEngine class inherits from GraphicsEngine you do not need to declare a variable to access the GraphicsEngines functionality.

You should be able to declare the code within the GameEngine like
Code:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.Direct3D.D3DX
Imports System.Math

Public Class GameEngine
    Inherits GraphicsEngine

    Public Function Init(ByVal Target As System.Windows.Forms.Form)
        BackBufferWidth = 800
        BackBufferHeight = 600
        BackBufferFormat = Format.A8R8G8B8
        BackBufferCount = 1

        SwapEffect = SwapEffect.Copy
        PresentInterval = PresentInterval.One
        CreateFlags = CreateFlags.HardwareVertexProcessing Or CreateFlags.MultiThreaded
        DeviceType = DeviceType.Hardware

        GraphicsTarget = Target

        GraphicsInitDevice()

        This is the map object
        Map = New GraphicsMap
        Map.LoadMap(Device, "testmap")

        Player = New GraphicsSprite(Device, Application.StartupPath & "\objects\human1\human1.bmp", New Point(0, 0), 16, 16, &HFF00FF00)

        //--------------------------------------------------
        // Different font types
        //--------------------------------------------------
        MessageText = New GraphicsText(Device, "Verdana", 8, FontStyle.Bold)
        MissionText = New GraphicsText(Device, "Verdana", 18)
        HUDText = New GraphicsText(Device, "Verdana", 22, FontStyle.Bold)

        While GraphicsRunning = True
            This is the function wich again calls the Overridable Render() function..
            GraphicsRenderLoop()

            Application.DoEvents()
        End While

        Player.Destroy()
        Map.DestroyMap()

        End
    End Function

    Public Overrides Function Render()
        The message box wont pop up...
        MessageBox.Show("Inside the correct render function!!")
        End
    End Function

End Class
 
Back
Top