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,
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Simple variables (e.g. int, Boolean, long, string, etc.) behave that way. Object variables (e.g. List, Map, etc.) usually have an "Initialize" method you must invoke before you may use them. For example:
B4X:
Dim MyInt as Int
Dim MyList as List

MyInt = 1
Mylist.Initialize
MyList.Add(MyInt)

@Erel has many videos (https://www.b4x.com/etp.html) that you may find quite informative.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
BASIC is the best gift for lazy.

Personally, I prefer strict style. Вut from another side, the code should not be redundant.
When absolutelly clear that B4A translates, for example,
B4X:
Dim intSeconds As Int
to
B4X:
int _intseconds = 0;
it's very hard to force yourself to write
B4X:
Dim intSeconds As Int
intSeconds = 0
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
in short :)
B4X:
Dim intSeconds As Int = 123

if the basic init the default value and yourself again the code would be
B4X:
Dim intSeconds As Int
intSeconds = 0
intSeconds = 0
that would be a reason why i not init it twice.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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?
Variables are set to the default values in B4A, B4i and B4J. This is not the case with local variables in B4R.

Dim intSeconds As Int
intSeconds =
0
Better to write:
B4X:
Dim intSeconds As Int = 0
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
This is not the case with local variables in B4R.
I was unware of that distinction ;)
giphy-downsized.gif
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Anyway, one of the (many?) good practices recommended is to always initialize variables
A bug in my b4a project, found right now (names changed):

B4X:
' zzz global variable:
Public MyVar As Int

Sub Remove(Var As Int)

Sub xxx
   Remove(zzz.MyVar)

Well, MyVar is initialized by the "system", so it's value is 0 (zero) and my project works bad, because I forget to set the value of MyVar where needed so Remove(MyVar) removes the item zero, instead of raise an exception like:

MyVar was not initialized

I would have found the bug in a moment, thanks to this error message.

[Of course, if I had initialized MyVar to 0, I would have had the same problem (so it is better to initialize number variables to -1)]


So: it would be much better if no variables were initialized by "the system".
(but I know that most or all of today's languages do this)
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@LucasMs
i do not want exception for everything.
bizarrely a value inside a type can be used without error and the type itself said IsInitialized = False

[ (so it is better to initialize number variables to -1)]
NO (i meant if the compiler do this)

you can handle this mentioned situation in other way.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Types are a whole different story:
You define a type and then you declare an object of that type and initialize it.
You can declare a single type and declare hundreds of objects of that type.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
NO

you can handle this mentioned situation in other way.
Sure, I could do a thousand ways, I could even create an external tool that reads all my sources and goes to see if I forgot to initialize some variables :p

The ideal would be that you had to initialize all the variables, of any type. In my opinion, I insist, initializing numerical variables to -1 is a good practice (just in relation to structures that have base 0).
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
yes, but, similarly to classes, it would be better if it was mandatory to initialize objects based on types.
Oh but it is! You need to initialize the objet if it comes from a custom type definition
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@LucasMs
the state seems wrong at types if they are auto initialized

(so it is better to initialize number variables to -1)]
if you do this by yourself then it is ok ;)
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User

From the Type Keyword help you can read...

Method_636.png
Type

Declares a structure.
Can only be used inside sub Globals or sub Process_Globals.
Syntax:
Type type-name (field1, field2, ...)
Fields include name and type.
Example:
Type MyType (Name As String, Items(10) As Int)
Dim a, b As MyType
a.Initialize
a.Items(2) = 123
 
Upvote 0
Top