Android Question Initial state of variables

Paulo Rosa

Member
Licensed User
Hi,

Just a small doubt I couldn't find the answer for in B4X documentation:

In VB 6 variables are always initialized when they are declared. For instance, if I declare "Public myflag as boolean", I can be sure that myflag variable will have the initial state of false... and so on for the other types of variables (zero for numeric types, empty string for strings, etc.).

Do B4A, B4J, etc. have the same behaviour? Can I always assume that variables are initialized with their default values, at the moment they are declared?

Regards,
 

Cableguy

Expert
Licensed User
Longtime User
This is because Items is an array, not a simple variable of "simple" type.
No, it's because items is a field of a type and the Type object must be initialized
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
And stop making me hungry!
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@LucasMs
#24 that i meant there is a.IsInitialized=False but it can be used.
before i had this idea for you
B4X:
Type MyInt(Value As Int)
and thought it raised an error
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Try this:

B4X:
' Process_Globals
Type MyType (Name As String, Item As Int)


Sub SomeRoutine

   Dim a As MyType
   a.Name = "even more angry"
   a.Item = 2
 
   Log(a.Name & TAB & a.Item)
Just because it works and raised no errors does not mean it's the correct way to do it!
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@LucasMs
have a solution :) how about :
DimNotInitialized a as mytype
DimNotInitialized b as int
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@LucasMs
he will say no and maybe a compiler option DoNotInitialize is better.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
a compiler option DoNotInitialize is better.
upload_2018-9-8_13-45-16.png
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The rolling stones were right...
You can't always get what you want!
 
Upvote 0
Top