Nobody is perfect (except me, of course (^_^) )

LucaMs

Expert
Licensed User
Longtime User
New video: Collections Example.

Why redim variables inside loops? In those two cases it was not needed.

You should check also if the limit was set to 0 or negative numbers.



[just for fun, of course :D]


Very useful video, to learn about maps, ordered lists and also something about regex.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Good question. The correct way to create a new object instance is with Dim + Initialize.

It is true that in some cases it is enough to only call Initialize (in cases where the object is implemented as an object wrapper).
The example in the video is correct.

If you try this code in B4i:
B4X:
Dim m As Map
Dim list As List
list.Initialize
For i = 1 To 10
   m.Initialize
   m.Put(i, i)
   list.Add(m)
Next
Log(list)

The output is:
Application_Start
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
Application_Active

As you can see all items point to the same map.

Correct code:
B4X:
Dim list As List
list.Initialize
For i = 1 To 10
   Dim m As Map
   m.Initialize
   m.Put(i, i)
   list.Add(m)
Next

In B4i Map is not a wrapper object.
 

ilan

Expert
Licensed User
Longtime User
Good question. The correct way to create a new object instance is with Dim + Initialize.

It is true that in some cases it is enough to only call Initialize (in cases where the object is implemented as an object wrapper).
The example in the video is correct.

If you try this code in B4i:
B4X:
Dim m As Map
Dim list As List
list.Initialize
For i = 1 To 10
   m.Initialize
   m.Put(i, i)
   list.Add(m)
Next
Log(list)

The output is:
Application_Start
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
NSMapTable {
[6] 10 -> 10
}
Application_Active

As you can see all items point to the same map.

Correct code:
B4X:
Dim list As List
list.Initialize
For i = 1 To 10
   Dim m As Map
   m.Initialize
   m.Put(i, i)
   list.Add(m)
Next

In B4i Map is not a wrapper object.

Sorry but this code doesnot make any sense to me. If u initialize on each loop the map for what will u use it? It will contain 1 single value at the end of the loop since it was initialized on each loop.
 

ilan

Expert
Licensed User
Longtime User
The code tries to add 10 different maps to a list.

It shows that it is not always enough to call Initialize when you want to create a new instance. You need to call Dim + Initialize.

Sure, otherwise u will always add the last map to the list 10 times because it refers to the same map if u dont redeclare it.

Now i understand what u wanted to teach. ;) very impotant topic!
 
Top