Wiki

Clone wiki

krome_bootcamp_2016_ex / how_access_procedures_and_attributes

Back to main

How to access to class attributes and procedures


Access to procedures

We define a procedure or a method to be any function or subroutine of the class.
Imagine that you have a class called myClass with two methods, one subroutine called myMethod and one function called myOtherMethod.
Here is a simple example that creates a new instance of the class and calls these two methods:

#!fortran
!import the class
use myClass

!define the variable class
type(myClass)::p

!create a new object of the class
p = myClass()

!call the subroutine method
call p%myMethod(arguments)

!call the function method
a = p%myOtherMethod(arguments)
This is what you will find in main.f90. Note that arguments can be empty.


Define a procedure and access class attributes

A procedure (a subroutine in this example) is defined in the class file as:

#!fortran
subroutine my_method(this,arguments)
 !define reference to class
 class(myClass)::this

 !print a variable value
 print *,this%myVariable

end subroutine my_method
Note that there is an additional argument this that represents the class itself. This argument is mandatory when using class procedures. In the above example, calling the myMethod subroutine will print the value of the class variable called myVariable. . These variables are also called attributes.


Additional references

See also how to create a new procedure.

More details on "type-bound procedures" (methods) and "components" (attributes) can be found here.

Updated