Android Question Double variables

welu1805

Active Member
Licensed User
Longtime User
Sorry, but I'm confused: Is is possible to declare a variable twice.

Dim i As Int : i = 1
Dim i As Int : i = 2
Msgbox(i, "")

shows "2"
No compiler error!

In Delphi there would be a compiler error! Whats this?
 

udg

Expert
Licensed User
Longtime User
Hi welu1805,
consider that Basic is not Pascal. If you're used to Wirth's rule of declaring (in that order) labels, constants, types, variables, procedures and functions even Delphi doesn't strictly adhere to it.
In B4A, dimming a variable is like declaring it. So dimming it twice is sort of re-declaring it where first instance is left to the garbage collector to be collected for eventual working memory reuse.

udg
 
Last edited:
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
I found an example like this:

'Initialize the panels we use for the pages and put them in the container
container.Initialize
For i = 0 To 5
Dim pan As Panel

Select i
Case 0
pan = CreatePanel(TYPE_HELLO_WORLD, "Hello World")
container.AddPage(pan,"Page " & i)
Case 1
pan = CreatePanel(TYPE_SETTINGS, "Settings")
container.AddPage(pan,"Settings")
Case Else
pan = CreatePanel(TYPE_LISTVIEW, "ListView " & (i - 1))
container.AddPage(pan,"ListView " & i)
End Select
Next

In this loop the variable "pan" is declared for 6 times with the same name. Why? Can I declare it one time before the loop?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
No you cant. Because you are actually creating 6 different panels. You are re-using ONE variable to reference all 6 panels. Each time you Dim it, it is creating a new panel, which is what we want. Otherwise if you put it outside the loop, you essentially have only ONE variable and ONE panel, and within the loop you keep modifying the same panel.
 
Upvote 0
Top