Method Declaration

Action

Within a window or window class declaration, declares a method.

Syntax

[gui-specifier] [return-type] method-id ([arguments]) 
statements
Variable Description
gui-specifier Optional: Specifies the GUIs that the declaration applies to. See window declaration. If omitted, the method applies to all GUIs.
return-type Optional: The data type of the value returned by the method, or void (the default) if the method does not return a value.
method-id An identifier that specifies the name of the method. You can use the same method name in different window or window class declarations.
arguments Optional: A list of the arguments that the method expects. The syntax for method arguments is the same as that of function arguments. See Function declaration.
statements The statements that make up the body of the method; they tell the compiler what actions you want the method to perform.

Notes

  • When you declare a method within a window class declaration, the method is available for use in all windows of that class.

  • When you declare a method within a window declaration, the method is available for use only by that window. The method declaration within a window overrides any method of the same name in the window’s class.

  • For examples of declaring methods, see winclass Declaration.

Referring to a Window with the Keyword "this"

When defining methods within a window class declaration, you use the keyword this to refer to the window that the method or property will refer to at runtime.

Deriving a New Method from an Existing Method

To derive a new method from an existing method, you can use the derived keyword followed by the scope resolution operator (::).

The following example defines a GetCaption method for DialogBox that prints the string "Dialog" before calling the built-in GetCaption method (defined in the AnyWin class) and printing its return value:

[-] winclass DialogBox : DialogBox
	[-] GetCaption ()
		[ ] Print ("Dialog")
		[ ] Print (derived::GetCaption ())

Defining "Virtual" Methods

You can define a method as being virtual, which means that the version of the method that is executed is determined at runtime, not at compile time (4Test virtual methods are analogous to virtual member functions in C++). You would want to define a method as virtual if, in a derived class, you want to override a method of a parent class that will be called by another method of the parent class.

You use the virtual keyword to define a virtual method for a class. You only need to use the virtual keyword with the top-level method definition. A derived class can provide its own instance of a virtual method or it can inherit the virtual method from its parent class.

Example

[-] winclass MyWin : AnyWin
	[-] WhatAmI () 
		[ ] Print ("I am a MyWin") 
	[ ] 
	[-] GetInfo () 
		[ ] WhatAmI () 
	[ ] 
[-] winclass MySubWin : MyWin
	[-] WhatAmI () 
		[ ] Print ("I am a MySubWin") 
	[ ] 
[ ] window MySubWin WinInstance
[-] main ()
	[ ] WinInstance.GetInfo ( )

This prints I am a MyWin, because the call to WhatAmI in GetInfo is resolved at compile time to the implementation of WhatAmI in the MyWin class.

By making WhatAmI a virtual method, resolution of WhatAmI calls are deferred until runtime, as shown in the following example:

[-] winclass MyWin : AnyWin
	[-] virtual WhatAmI () 
		[ ] Print ("I am a MyWin")
	[ ] 
	[-] GetInfo () 
		[ ] WhatAmI ()
	[ ] 
[-] winclass MySubWin : MyWin
	[-] WhatAmI () 
		[ ] Print ("I am a MySubWin") 
	[ ] 
[ ] window MySubWin WinInstance
[-] main ()
	[ ] WinInstance.GetInfo ( )

This prints I am a MySubWin because the version of WhatAmI is determined at runtime, when the class of object calling WhatAmI can be identified as a MySubWin, so the MySubWin version of WhatAmI is used.