- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am not sure about the syntax for assigning a default value to a component of a derived type variable. I want all new instances of my derived type have some of their components assigned a default value.
For instance:
TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED
INTEGERT
...
END TYPE MY_TYPE
Now, when I declare a variable A of type MY_TYPE, I want to make sure that the component IS_IT_INITIALIZED is set to .FALSE. and T to 1000 (for instance).
Is there a way to do this? Or should I manually initialize every new instance of MY_TYPE when I declare it?
Thanks!
Olivier
For instance:
TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED
INTEGERT
...
END TYPE MY_TYPE
Now, when I declare a variable A of type MY_TYPE, I want to make sure that the component IS_IT_INITIALIZED is set to .FALSE. and T to 1000 (for instance).
Is there a way to do this? Or should I manually initialize every new instance of MY_TYPE when I declare it?
Thanks!
Olivier
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - opmkl
I am not sure about the syntax for assigning a default value to a component of a derived type variable. I want all new instances of my derived type have some of their components assigned a default value.
For instance:
TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED
INTEGERT
...
END TYPE MY_TYPE
Now, when I declare a variable A of type MY_TYPE, I want to make sure that the component IS_IT_INITIALIZED is set to .FALSE. and T to 1000 (for instance).
Is there a way to do this? Or should I manually initialize every new instance of MY_TYPE when I declare it?
Thanks!
Olivier
For instance:
TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED
INTEGERT
...
END TYPE MY_TYPE
Now, when I declare a variable A of type MY_TYPE, I want to make sure that the component IS_IT_INITIALIZED is set to .FALSE. and T to 1000 (for instance).
Is there a way to do this? Or should I manually initialize every new instance of MY_TYPE when I declare it?
Thanks!
Olivier
Yes you can do it.
TYPE MY_TYPE
LOGICAL IS_IT_INITIALIZED = .FALSE.
INTEGER T = 1000
...
END TYPE MY_TYPE
Then TYPE(MY_TYPE) AnOther will take the default values
Les
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Almost. You need to use:
LOGICAL :: IS_IT_INITIALIZED = .FALSE.
INTEGER :: T = 1000
If you're going to supply an initial value, the :: delimiter is required.
LOGICAL :: IS_IT_INITIALIZED = .FALSE.
INTEGER :: T = 1000
If you're going to supply an initial value, the :: delimiter is required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Aah... that's the thing I was also missing... The "::" delimiter...
Thanks a lot Steve. This is a very convenient feature.
Olivier

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page