How to Reference a Class in Class Hierarchy Generically (128076)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0

This article was previously published under Q128076

SUMMARY

You can call methods and properties of a class from a derived class with the scope resolution operator (::). This article demonstrates a generic method you can use to refer to a class that is one level higher in the class hierarchy tree.

MORE INFORMATION

When a class is defined, it inherits the properties, events, and methods associated with the class from which it is derived -- the parent class.

However, writing any code in a method of a class overrides the code that was defined in the parent class. There might be cases where the method of a class has to be an extension of the method in the parent class and include the tasks defined in the parent class. The method in the parent class can be called with the scope resolution operator (::).

You can generically refer to a parent class by evaluating the parent class property. The following example demonstrates how to call a parent class from a class generically. It uses the EVALUATE() function.

Code Sample

In this example, a Building class is defined as a custom class. The House class is derived from the Building class. An instance of the House class is created, and the RingtheBell method is called. When you run this sample code, a WAIT WINDOW should be displayed. This is the code defined in the Building class. Once the WAIT WINDOW is cleared off the screen, the bell should ring. This is the code defined in the House class.
   oNewObject= CREATEOBJECT ("House")
   oNewObject.RingThebell()

   * Class definition
   DEFINE CLASS building AS CUSTOM
      Value="Building"

      PROCEDURE RingtheBell
         WAIT WINDOW "This is a " + this.value
      ENDPROC
   ENDDEFINE

   DEFINE CLASS House AS Building
      Value ="House"

      PROCEDURE RingtheBell
         =EVALUATE(this.parent.class+'::RingtheBell()')
         && Generically calls the method in the parent class
         ?CHR(7)
      ENDPROC
   ENDDEFINE
				

Modification Type:MajorLast Reviewed:2/12/2000
Keywords:kbcode KB128076