I use this code, but when run it has error like this:
Syntax error at line 22 in class EXCEPTIONHAN
-- Client deposit money into his account.
local account_number : INTEGER is 10
-----------------------------------------------^
i : INTEGER
See my code:
feature Client_Deposit is
-- Client deposit money into his account.
local account_number : INTEGER is 10
i : INTEGER
amount : REAL
do
rescue
end -- client Deposit.
It's meant we can't construct for local variable in eiffel.
In java, it's very easy.
void Client_Deposit(){
int account_number = 10;
.....
}
I use this way, but it's not comfortable.
That is a constant, not a local
I'm not sure what you mean by "constructing" a local variable. I think you mean you want to initialise the local variable.
This is how you initialise the local integer variable to 10:
The syntax that you used is wrong for two reasons. First of all, it is declaring a constant, and constants cannot be declared within a routine. Secondly, the is keyword is obsolete; for declaring a constant, use =.
Please do not use the blog section to submit question. Instead go to http://forums.eiffel.com.
it's not comfortable.
Deal Peter Gummer ! Thanks for your guiding. But when use this way to initialise a local variable:local
account_number: INTEGER
do
account_number := 10
when post-violation is occured and excute the retry instruction, it's initialise this variable again ( your way). it's not proper, i mean. When the retry is excute, the variables should have their current value and not be initialised again.
I know that when we declare :local
account_number: INTEGER
do
variable account_number is initialised the value 0. And when retry is excuted, account_number isn't initialised value 0 again. it's comfortable.
But we always have to use this defaut initialization, don't we? If it were true, it would be uncomfortable.
In many case We wanna initialise proper local value. And when retry instruction is excuted, the local values have their current value.
In C, I can do that by use "static" keyword in front of the local variables. It's comfortable in many case.
You can use a local to control extent of retry
You have full control over whether local variables are re-intialized or not. The usual technique is to declare a local boolean variable and set it to true in the rescue clause:
e.g.local
l_retried: BOOLEAN
l_account_number: INTEGER
do
if not l_retried then
l_account_number := 10
...
end
...
rescue
l_retried := True
retry
end
Colin Adams
I know this way to initialise local value. But if use this way, we have to use another boolean value to mark.
Firstly, it's waste of storage.
Secondly, When rely instruction is excuted, the most above condition instruction is always excuted. And the the instruction that set value to true is also. It's waste of time.if not l_retried then -- This condition is always excuted.
I meant, Why eiffel doesn't supply the way to initialise local value ?