1

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.

GoodDeeds
  • 7,956
  • 5
  • 34
  • 61

1 Answers1

3

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.

Louis M
  • 576
  • 2
  • 4
  • Thank you. I was not aware of that. However, this change only corrects the first error. Could you please tell what's causing the second error, and how to correct it? – GoodDeeds Nov 04 '16 at 14:45
  • 1
    Sorry, it slip out of my mind. I edit my answer to help you with both errors. – Louis M Nov 04 '16 at 15:35