Solved

EInvalidOp

Posted on 2001-08-12
8
177 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
[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
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
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 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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

624 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