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,
 

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
Top