Re: Assigning values to a derived type (structure constructor?)



In article <1hnu0dk.1w7jpew1mdmb2yN%nospam@xxxxxxxxxxxxx>, Richard Maine wrote:
<andrew.mallner@xxxxxxxxxxx> wrote:

I have a derived type that has a number of properties:
TYPE Particle
REAL** :: X,Y,Z,U,V,W,E, ...
END TYPE
TYPE(Particle) :: Partcles(0:100)

After an interaction I want to adjust the U, V, W, and E values:
Particles(i)%U = NewU
Particles(i)%V = NewV
Particles(i)%W = NewW
Particles(i)%E = NewE

Yes. That's one way.

I know I can assign all the values at once like:
Particles(i) = Particle(InitX, InitY, InitZ, InitU, InitV, InitW,
InitE, ...)

And that's another, provided you want to assign to all the components.

Other languages have something like this (VB Example)
With Particles(I)
.E = NewE
.U = NewU
....
End With

Does Fortran have something similar?

F2003 (which has no compilers yet) has the associate construct, which is
similar. In f90/f95, the closest you can come is to have a pointer with
particles(i) as its target. That is

type(particle), pointer :: p
...
p => particles(i)
p.e = NewE
p.u = NewU
...

Oops? s/./%/ :-)


--
Janne Blomqvist
.



Relevant Pages