Undeclared Variable Detect?

Scantech

Well-Known Member
Licensed User
Longtime User
Undeclared variable gets compiled successfully? I was not aware of it. Can we turn this feature off? Option Explicit perhaps?

I do not want to make a mistake like this.

Dim TypeRequest as int
RequestType = 1

It will compile ok. Funny thing is I just noticed it today. I need to double check my spelling.
 
Last edited:

vndnguyen

Member
I have also the problem with variable.

According to B4A, the global variables (declared in Process_Globals) can be accessed from all modules. But in fact I cannot access them from other Activity.

Any help?
 
Upvote 0

DemoFreak

Member
Licensed User
Longtime User
This is planned for the next version.

In V1.8 this code still compiles successfully.

B4X:
Dim L As Label
Dim GetIP As InputDialog

L = Sender
s = GetType(L.Tag)
If s = "android.widget.TextView" Then L = L.Tag

i = L.Tag

s and i are undeclared.
 
Last edited:
Upvote 0

DemoFreak

Member
Licensed User
Longtime User
But you should see s and i in red showing that these variables are not declared.

Yes, of course. :)

I'm asking just out of curiosity. I hadn't expected this behaviour at all, and stumbled over it when I tried your ScrollView example this morning (and wondered why it is operating well despite some red colored variables). :D
 
Upvote 0

DemoFreak

Member
Licensed User
Longtime User
But you should see s and i in red showing that these variables are not declared.
And just I've noticed, that FOR does an implicit DIM, or the syntax coloring has a tiny glitch:

B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim top As Int
For i = 0 To 15
   Dim ID, IP As Label
   ID.Initialize("Device")
   IP.Initialize("Device")
   ID.tag = IP
   IP.tag = i
   ID.TextSize = 29
   IP.TextSize = 29
   top = i * 30dip
'...
Next

Here the i isn't colored in red. But, when I comment out the FOR line, the i gets colored instantly.
 
Upvote 0

DemoFreak

Member
Licensed User
Longtime User
Basic4android allows you to use undeclared variables which are treated as string variables.
Thanks for that hint. :)

I was bitten by that just now, as I read back a map with File.Readmap.

The map consists of a byte-type key and some user-defined type value.

B4X:
Dim mapIPAdresses As Map
mapIPAdresses.Initialize

' populate map with already known IP addresses from global DSPs map
For i = 0 To Main.DSPs.Size - 1
   Dim x As typeDSP
' [some other code]
' Gettype(Main.DSPs.GetKeyAt(i)) results in "java.lang.byte"
   mapIPAdresses.Put(Main.DSPs.GetKeyAt(i), x.IP)
Next

' write IP configuration to file
File.WriteMap(File.DirInternal, "devices.map", mapIPAdresses)

When I read back that map, I stumbled over my assumption, that the key type of the read map is byte, as it was written as byte.

B4X:
Dim DeviceMap As Map
DeviceMap = File.ReadMap(File.DirInternal, "devices.map")
For i = 0 To DeviceMap.Size - 1
   Dim DSPData As typeDSP
   DSPData.IP = DeviceMap.GetValueAt(i)

   Log(GetType(DeviceMap.GetKeyAt(i)))
   ' results in "java.lang.String"

   Dim tempByte As Byte
   tempByte = DeviceMap.GetKeyAt(i)
   Log(GetType(tempByte))
   ' results in "java.lang.Byte"

   DSPs.Put(tempByte, DSPData)

   ' the following line leads to some confusion as the keys that
   ' was read in was of type "String" and other (further added) keys
   ' was of type "Byte" as desired, so some key comparison failed
   ' and I've got a map with two keys "0" and "0", one of type "Byte"
   ' and the other one of type "String"
   ' DSPs.Put(DeviceMap.GetKeyAt(i), DSPData)
Next

Now, as I explicitely cast the key to byte, all works as expected. :)
 
Upvote 0
Top