In Eiffel Studio, I have been trying to access the fields of an object of a class I have defined from another class. However, it keeps giving errors that I am not able to understand and solve. The following is a snippet of example code:
Class where object is being created:
class
TEST1
feature
object: TEST2
-- object of type TEST2
function(val: INTEGER)
-- Assign
do
object.value:=val
end
end
Class whose object is being created:
class
TEST2
feature
value: INTEGER
end
The error messages are as follows:
Error code: VBAC(2)
Error: target of assigner call has no associated assigner command.
What to do: add an assigner mark to the declaration of the target feature or use a dot form of a call.
Class: TEST1
Feature: function
Line: 10
do
-> object.value:=val
end
and
Error code: VEVI
Error: variable is not properly set.
What to do: ensure the variable is properly set by the correspondig setter instruction.
Class: TEST1
Source class: ANY
Feature: default_create
Attribute(s): object
Line: 331
do
-> end
It seems that there is some problem with the assignment statement. However, I haven't been able to understand what is wrong.
The classes have been defined in different files under the same cluster of the same project. I am new to Eiffel, so I don't know if this could be the problem.
Thank you.
In Eiffel, every attributes are considerate as Read-Only. This remove the need to create getters like you do in other languages like Java. To assign a value to an attribute using the ":=" syntaxe, you will need an assigner. Here an example:
class
TEST2
feature
value:INTEGER assign set_value
set_value(a_value:INTEGER)
do
value := a_value
end
end
Then, you will be able to use the line:
object.value:=val
For the second error, by default, EiffelStudio is what we call Void Safe. This is a mecanism that ensure that an attribute that is not considerated as "detachable" will never be Void (similar to NULL in other languages). By default, every class have the default constructor called "default_create" and this constructor does not do anything. What you have to do, is creating you own constructor in the {TEST1} class that instanciate every attribute inside it. Here is an example:
class
TEST1
create
make
feature
make
do
create object
end
object: TEST2
-- object of type TEST2
function(val: INTEGER)
-- Assign
do
object.value:=val
end
end
In the preceding example, I created a method call make, specify that the method is the constructor and in this method, I make sure that the object attribute is correctly instanciate.