Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting a Default Value in a Function for Delphi

Posted on 2011-03-05
12
Medium Priority
?
402 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:zberg007
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 14

Expert Comment

by:systan
ID: 35044727
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;
...
...
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044780
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

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35044792
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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 24

Expert Comment

by:jimyX
ID: 35044807
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

0
 

Author Comment

by:zberg007
ID: 35044810
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!
0
 

Author Comment

by:zberg007
ID: 35044848
jimyX. thanks I'm trying this and I get a quick message of: Undeclared identifier: 'showmessage'

Thanks!
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 35044857
This version should work even if there are spaces in the original Oil Added field.

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

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044861
If you need it add "Dialogs" to the uses clause.
0
 

Author Comment

by:zberg007
ID: 35044903
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!
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35044916
Thanks.  Sometimes you just have to look at it from a different angle.  ;)
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044963
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.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35045003
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question