Link to home
Start Free TrialLog in
Avatar of dluedi
dluedi

asked on

EInvalidOp

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
Avatar of Madshi
Madshi

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.
Avatar of dluedi

ASKER

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????
Avatar of kretzschmar
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 ;-)
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

:-)