Link to home
Start Free TrialLog in
Avatar of zberg007
zberg007

asked on

Setting a Default Value in a Function for Delphi

I have this Parameter which is part of function:

ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));

Open in new window


I need to set a default value of 0 (zero) when this field is empty. Otherwise it outputs whatever the value is in the field.

How best to do this please?
Avatar of systan
systan
Flag of Philippines image

var v_oil:integer;
...
...
v_oil := StrToInt(StripIT(fieldByName('Oil Added').AsString));
if v_oil <= 0 then
ParamByName('@FluidAdded').Value := 0
else
ParamByName('@FluidAdded').Value := v_oil;
...
...
Avatar of jimyX
jimyX

You can test the value of the field first as follows:
if fieldByName('Oil Added').AsString = '' then
  ParamByName('@FluidAdded').Value := 0
else
  ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));

Open in new window


or
if fieldByName('Oil Added').IsNull then
  ParamByName('@FluidAdded').Value := 0
else
  ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));

Open in new window

A bit of a hack, but if Oil Added is always positive or blank, prepend a leading zero to make it always a number.

ParamByName('@FluidAdded').Value := StrToInt(StripIT('0'+fieldByName('Oil Added').AsString));

Open in new window

You need to check the value of the field "Oil Added" whether it is convertible to Integer, to avoid error's messages:
if fieldByName('Oil Added').IsNull then
  ParamByName('@FluidAdded').Value := 0
else
  begin
    try
      ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));
    except
      showmessage(fieldByName('Oil Added').AsString +' is a non-integer value!');
    end;
  end;

Open in new window

Avatar of zberg007

ASKER

Hey systan, when I run your solution, I get this error while running the application:

raised exception error class EConvertError with message '"is not a valid integer value'.

Any ideas if I have something wrong?

Thanks!
jimyX. thanks I'm trying this and I get a quick message of: Undeclared identifier: 'showmessage'

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you need it add "Dialogs" to the uses clause.
sjklein42, You're solution works as is, easy and short with no errors!

Otherwise, I kept getting '"is not a valid integer value'

Thanks again!
Thanks.  Sometimes you just have to look at it from a different angle.  ;)
First congrats for the points sjklein42.

What angle was that? if you have spaces or non-convertible-to-integer value you will get an error.

This will get you an error:
showmessage(inttostr(strtoint('0'+'1 ')));

I am posting this for the records and to clarify for the future viewers. But anyway that angle seems to suit the author of this question.
jimyX - You're absolutely right.  I hope you saw the smiley face.  One doesn't need to be much of a purist to be rankled.  But it just seemed so simple I had to throw it in.  Sorry - I didn't mean to snipe you.

It is a pragmatic solution (ie: hack) that works just fine in this case, since the number is always positive.  Spaces and nonsense, it may also handle, if the StripIT function does what I'm guessing and strips out non-numeric characters.