zberg007
asked on
Setting a Default Value in a Function for Delphi
I have this Parameter which is part of function:
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?
ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));
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?
You can test the value of the field first as follows:
or
if fieldByName('Oil Added').AsString = '' then
ParamByName('@FluidAdded').Value := 0
else
ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));
or
if fieldByName('Oil Added').IsNull then
ParamByName('@FluidAdded').Value := 0
else
ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));
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));
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;
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!
raised exception error class EConvertError with message '"is not a valid integer value'.
Any ideas if I have something wrong?
Thanks!
ASKER
jimyX. thanks I'm trying this and I get a quick message of: Undeclared identifier: 'showmessage'
Thanks!
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you need it add "Dialogs" to the uses clause.
ASKER
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!
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(strto int('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.
What angle was that? if you have spaces or non-convertible-to-integer
This will get you an error:
showmessage(inttostr(strto
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.
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.
...
...
v_oil := StrToInt(StripIT(fieldByNa
if v_oil <= 0 then
ParamByName('@FluidAdded')
else
ParamByName('@FluidAdded')
...
...