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

x
?
Solved

EInvalidOp

Posted on 2001-08-12
8
Medium Priority
?
181 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
Technology Partners: 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!

 
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 120 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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