Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting a Default Value in a Function for Delphi

Posted on 2011-03-05
12
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 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