Solved

EInvalidOp

Posted on 2001-08-12
8
172 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 140
FMX StringGrid1->Canvas->FillRect Problem 3 127
Multiple image collision 13 69
control image tags in a string ? 12 111
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

13 Experts available now in Live!

Get 1:1 Help Now