Solved

EInvalidOp

Posted on 2001-08-12
8
171 Views
Last Modified: 2010-04-06
Hello I get that stupid error if I do this
{---}
function error:extended;
begin
error:=1/2;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
xw:extended;
begin
xw:=error+xw;     // if you write xw:=1/2+xw  there is no compiler error!
end;

end.
{---}
You sure now why!
:)dluedi
0
Comment
Question by:dluedi
8 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 6377269
I'm not sure what you mean. You're talking about "EInvalidOp", which is an exception. Then you're talking about a compiler error, which is a totally different beast than an exception.

Furthermore your code doesn't make much sense. I mean in this line:

xw:=error+xw;     // if you write xw:=1/2+xw  there is no

The "xw" variable is undefined. So it's like using:

xw:=error+aWildValueNobodyKnows;

What's the sense of it?

Regards, Madshi.
0
 
LVL 1

Author Comment

by:dluedi
ID: 6377630
Sorry, I made a mistake, of course EInvalidOp is no compiler error, and the error I am talking about happens when program is running...this program looks quite wired, that's because I shortend it (in reality it sure doesn't look like this). I just wanted to make it simple...I think I need just to define xw in oncreate...I'll try it. I thaught that if a variable is not yet defined its 0
But isn't it still strange that xw:=1/2+xw doesn't create an EInvalidOp but xw:=error+xw does....its actually the same????
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6377714
just a guess

try this change

function error:extended;
begin
  Result:=1/2;
end;

just to say that error is a constant in the windows unit,
maybe this causes the problem

meikl ;-)
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6378053
xw := error + xw
xw-xw := error

0 <> 1/2 thus it is an invalid statement


You need to initialize error as a constant and xw needs a value assigned to it in order for the calculation to work.

Thas is basically what the other experts are saying.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Expert Comment

by:Madshi
ID: 6378698
Well, it's no mathematical formula here, it's an "assignment". I mean, in pascal we write ":=", we don't write "=" and that has a meaning! The purpose is to show that both sides may not be equal before the assignment. You can also do this:

var xw : integer;
begin
  xw := 5;
  xw := 10 + xw;

You could also say here xw-xw := 10 and 0 <> 10. But that doesn't matter, because it's an *assignment*.

Regards, Madshi.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 30 total points
ID: 6378712
I've checked the problem in my D6. The problem exception occurs only if you have not initialized xw before the assignment. If you initialize it before the assignment, everything works fine. I'm not sure about the reason. But the reason doesn't really matter. It's considered a *catastrophic* programming style to use uninitialized variables like that. My Delphi even throws out a warning when compiling.

Okay, I understand that you tried to cut down your sources. But in case that in your original problem sources you DID initialize xw, you'll have to do the cutting again, but this time cut it in a way, that Delphi doesn't show any warning when compiling -> do initialize all variables before you use them in a calculation.

Regards, Madshi.
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6379433
Your'e right Madshi! I realized the error after I posted.

But I do appreciate your excellent observation and discussion on the difference between '=' and ':='.

I realized my mistake when I thought about incrementing a number:

i := i+1;

anyway, you are correct the value must be initialized.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 6379530
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

20 Experts available now in Live!

Get 1:1 Help Now