The Basic Zone

    unit Tips&Tricks.pas
      {Delphi Tips & Tricks}

The Following Tips and Tricks are Available :

If you have any Tips and Tricks yourself, please mail them to : matd@oocities.com

Bugs in Setting Default values for properties

I don't know wether I did something wrong or not but the 'default' reserved word doesn't do a thing when I use it in my components , what's more, when I try to set the property to it's default value in design time, it completely ignores that value at run-time, causing errors that are completely unexpected and, ofcoarse hard to trace. Lets see an example of this :

My code was :
     property Steps: Integer read valSteps write valSteps default 10;
This looks OK to me, but when I dropped the Component on a Form, the Steps value read 0 instead of 10.
Now, when I set Steps to 10 at desing time and run my application, the run-time value is 0 which, in my case, caused a 'EZeroDivide : floating point division by zero' error.

If you experience trouble like this, you should stop using the 'default' reserved word and use the constructor to set set your property values. Like this:

constructor TTransitionEffect.Create(AOwner: TComponent);
Begin
 inherited Create(AOwner);
  valSteps:=10;
end;

Preventing Stack-Errors

Ever had a stack-error ? You know, these errors that show these nice win95 error boxes and force you to shut down Delphi without first saving your work. If you were unlucky you probably had to shut down windows or even your computer ( this is, if you work with windows 95, I suppose that NT doesn't give that much troubles ).

These errors are most commonly caused by a never ending loop. What do you say ? You aren't so stupid that you cause a neverending loop ? Possible, but you forget that a neverending loop is also what you get if you've got a function that calls itself and, for that matter, functions that call themselfs include property set procedures. The following will cause such an error :

...
property SomeThing: variable read valSomeThing write setSomeThing;
...
procedure setSomeThing(V: variable);
begin
  SomeThing:=V;
end;

The line that causes the error is :

Because this try's to set the property, which runs the setSomething property. That way this is a selfcalling function but a camouflaged one. What should be there instead is:


Back to the Basic Zone Home Page
Back to Geocities
Back up