Solved

Setting a Default Value in a Function for Delphi

Posted on 2011-03-05
12
395 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
System restore point 4 78
Delphi cmd execution 6 41
code issue 8 85
Tviruailstringtree sort multi columns on header click 1 34
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now