Android Question [SOLVED] Initialising a Type

JMB

Active Member
Licensed User
Hi there.

I've created a Type like this:

B4X:
Type FlightWaypoint(Name as String, Position as Location)
The problem is that I have to do two Initialize calls...
B4X:
Dim FW as FlightWaypoint
FW.Initialize
FW.Position.Initialize
Aside from creating a class, is there a way of avoiding this double initialize requirement?

Thanks,

JMB
 

emexes

Well-Known Member
Licensed User
Aside from creating a class
I wondered this early on too, but not as deeply as you.

I think you may have answered your own question.

But it's not all bad. About half of the time, I end up adding methods or extra fields that need non-blank initializing to the "type"... at which point I'm like: how lucky was that?!?!
 
Last edited:
  • Like
Reactions: JMB

klaus

Expert
Licensed User
What you do to you need FW.Position.Initialize?
When you set a location, which must be initialized no need to initialize FW.Position.
I tried the code below without FW.Position.Initialize.
B4X:
Private FW As FlightWaypoint
FW.Initialize
FW.Name = "MyPoint"
Private loc As Location
loc.Initialize2(45.0, 7.5)
FW.Position = loc
Log(FW.Position.Latitude)
Or am I missing something?
 

LucaMs

Expert
Licensed User
It is obvious that if a Type contains objects they must be initialized.

In this case Klaus's answer is correct, in my opinion.
 
  • Like
Reactions: JMB

agraham

Expert
Licensed User
When the FlightWaypoint Type is instantiated by Dim it contains an instance of a Location as its Position item. If, as in Klaus' case, you are immediately going to overwrite that with a different instance of a Location then you do not need to Initialize it. On the other hand if you are going to use the pre-provided instance you will need to Initialize it otherwise you will get a run-time NotInitialized error. One way or another you will need to always Initialize each instance of a Location, B4A will not do it for you.
 

JMB

Active Member
Licensed User
Thank you all once again for that information - that has resulted in much greater understanding.

JMB
 
Top