Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DODEFAULT() vs :: Scope Resolution 3

Status
Not open for further replies.

Scott24x7

Programmer
Jul 12, 2001
2,828
JP
Can someone explain to me (like 3rd grader), what the difference between DODEFAULT() and :: is, and how to implement them? When/why would you call them? I've seen some code, and read some things about "Parent Events/Methods", but I don't get why I'd want to do such a thing???
-Scott
Please let me know if this has helped!
 
Hi Scott,

Since VFP is OOP. There are parent classes and subclassses.

When you subclass an exisitng class, you would want the original code in the parent class to be executed.

DODEFAULT() will execute the code in the parent class.

Example:

DEFINE CLASS MyForm AS Form
PROCEDURE Clcik
LOCAL llRetVal
IF llRetVal
WAIT WINDOW 'This is a message form the base class'
ENDIF
RETURN lLRetVal
ENDPROC
ENDDEFINE

DEFINE CLASS MySecondForm AS MyForm
PROCEDURE Click
LOCAL llRetVal
llRetVal = DODEFAULT()
IF llRetVal
WAIT WINDOW 'This is a subclass message'
ENDIF
ENDPROC
ENDDEFINE

If you would instantiate the second form and click on it, the code from the parent class would be executed becuase you issued a DODEFAULT() after that the rest of the code in the click event of the sub class would be executed.
In essence, you would see two messages.

This is the core of OOP, code once use many times.

If you would leave out the DODEFAULT() in the subclasses click event, the original code would not be executed, and you would see only one message, being the one of the subclass.

There is also a NDEFAULT statement, this will help you, explicitly not to execute the code of the parent classes.

Example, if you would issue a NODEFAULT in the QueryUnload() event of a form, pressing the X will not close the form.
This is very handy if you wish to check if data has been saved, and wish to ask the user if he/she wishes to save, cancel the edited data or stay in the form.

HTH,
Weedz (Wietze Veld)
My private project:CrownBase source code can be downloaded !!
 
:: is jut a way of calling the parent method, I must say that I have never used it.
I have seen it in VFP3.0 books. I always use DODEFAULT().

HTH,
Weedz (Wietze Veld)
My private project:CrownBase source code can be downloaded !!
 
Hi TheManiac,

If you are about to start using classes and sub-classes be aware of a popular gotcha ... ANY text in a sub-class method will prevent the parent class method from firing. This includes a simple space or a comment.
 
DODEFAULT and the scope resolution operator (aka "::")

This is NOT a third grade question...


DODEFAULT() means "run the code in the parent version of this class".
You need this when a subclass has *any* code in the event/method.

For example, say you had a command button class where the click() event ran a wave file that made a "click" sound.

When you dropped that command button onto a form and put some code in the button's click event, the wave file would not be run unless you included the DODEFAULT() command in the event code:

*** in MyClass.vcx
DEFINE CLASS cmdBtnClick AS cmdButton
... YadaYadaYada

FUNCTION Click

SET BELL TO Click.Wav
? CHR(7)
SET BELL TO

RETURN

... YadaYadaYada
ENDDEFINE

*** in MyForm.scx
DEFINE CLASS cmdHello AS cmdBtnClick
... YadaYadaYada

FUNCTION Click
*** there will be no "click" sound
WAIT WINDOW "Hello World"

RETURN

... YadaYadaYada
ENDDEFINE

*** in MyForm.scx
DEFINE CLASS cmdHello AS cmdBtnClick
... YadaYadaYada

FUNCTION Click
*** "click" sound
DODEFAULT()

WAIT WINDOW "Hello World"

RETURN

... YadaYadaYada
ENDDEFINE

That's the story for DODEFAULT().

********************************
Scope resolution operator

Using the classes from the previous DODEFAULT discussion, lets say that you subclassed cmdBtnClick so that it did some really neat stuff and looked really cool. But, being a miscevious sort, it also made a rude noise when clicked:

DEFINE CLASS cmdRazzer AS cmdBtnClick
... YadaYadaYada

FUNCTION Click

SET BELL TO Razzer.Wav
? CHR(7)
SET BELL TO
RETURN

... YadaYadaYada
ENDDEFINE

One day, you realized that you need the neat functionality of cmdRazzer for something, but would probably get fired if the new button made that rude noise. And it would be a very
good thing if the button made the nice little "click" sound... but you reeealy need that other neat stuff in cmdRazzer:

DEFINE cmdSaveMyButt AS cmdRazzer
... YadaYadaYada

FUNCTION Click
LOCAL lcParent, lcCallClick

WITH THIS
m.lcParent = .PARENT
m.lcCallClick = m.lcParent = "::CLICK()"
*** run the click event code in class cmdBtnClick
&lcCallClick
ENDWITH

RETURN

... YadaYadaYada
ENDDEFINE

So in summary, DODEFAULT() goes down the inheritance chain until it finds a parent/grandparent/greatgrandparent with event/method code to run and then VFP runs that code... VFP goes no further.

In contrast, if you want to run a specific level of inherited event code, you can specifiy that level with the scope resolution operator ("::").

I think a big problem with "::" is that MS has not implemented it very well in VFP (macros suck) and the documentation is even worse.

Regards,
Thom C.
 
One other thing... about NODEFAULT:

From the VFP help DEFINE CLASS:

"NODEFAULT

Prevents Visual FoxPro from performing its default event or method processing for Visual FoxPro events and methods. For example, if the KeyPress event occurs, including NODEFAULT in the KeyPress procedure or function prevents Visual FoxPro from placing the key press into the Visual FoxPro keyboard buffer. This allows you to create a KeyPress procedure that lets you test which key is pressed before the key is sent to the keyboard buffer.

NODEFAULT may be placed anywhere within the event or method procedure. Note that NODEFAULT may also be placed within an event or method procedure in the Form Designer.
"

This is a seperate issue from not using DODEFAULT() to keep code in a parent event from firing. Putting your code in an event will prevent a parent's code from firing, but it will NOT prevent the low-level event code in the VFP base class event from firing... that requires NODEFAULT.

Regards,
Thom C.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top