Android Question Declare variables as same name in IF statements?

techknight

Well-Known Member
Licensed User
Longtime User
So, I ran into a new problem.

Here is the problem code:

B4X:
Sub cmdCancelSelection_Click
    Dim CapturedButton As Label = Sender
    If CapturedButton.Tag = 1 Then 
        Dim Blank As QuickHitInfo
        Blank.Initialize
        SelectedMedia.QuickHit = Blank
    Else
        Dim Blank As HeadShotInfo
        Blank.Initialize
        SelectedMedia.HeadShot = Blank
    End If
    RefreshSelectedPlaylists
End Sub

This code basically creates a blank CustomType and assigns it to a global variable.

However, the above gives me an error that the "Current declaration does not match the previous one"

That doesn't make any sense to me, because "logically", the two variables cant be declared at the same time anyways because its in an IF/ELSE block.

So why does the compiler care/complain that I am keeping the same variable name with two different types? again they can never be declared at the same time if you were to look at this logically.

Bug?
 

techknight

Well-Known Member
Licensed User
Longtime User
But I am not redimming any global variables, or any arrays.

This is completely illogical to me.

I ended up changing the code to something different anyways, but its just these little roadblocks that are stupid IMHO.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
And thats "worse" in your opinion because?

Program flow determines how the variable is going to be declared, and should only be good for that particular code flow and thats it, should be destroyed and cleared by the GC as its a local variable so in my mind it all seems logical to me.

Thats the way every other version of BASIC I have ever used worked. Why this one has to be "different" is beyond me.

Now if I was trying to declare over top of a global of the same Name, or declaring another of the same local variable of a different type outside of a conditional statement, Then I would understand as its trying to allocate something different over top of something that is already allocated. But in my mind, not an IF. Its either going to allocate as that, or as this. But never both.
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
redefining WHAT global variable? I am declaring a new one that doesnt exist in globals.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
a new one that doesnt exist in globals
You misunderstand how variable scope works in B4X, it does not work in the same way as it does in VB.NET or other BASICs. Variables declared anywhere in a procedure may be accessed from anywhere within that procedure. For example, the following will print "Var is 2" to the log in B4A:
B4X:
 Dim piIndex As Int = 5
 If piIndex = 1 Then
  Dim piVar As Int
  piVar = 1
 Else
  piVar = 2
 End If
 Log("Var is " & piVar)
This code would not compile in VB.NET.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User

Yuck. No wonder this stuff keeps throwing me off.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Also, (off-topic, I know) I think if your custom type variables are strongly typed (i.e. declared as "QuickHitInfo" and "HeadShotInfo" and not just "Object") then you can change:
B4X:
If CapturedButton.Tag = 1 Then
        Dim Blank As QuickHitInfo
        Blank.Initialize
        SelectedMedia.QuickHit = Blank
    Else
        Dim Blank As HeadShotInfo
        Blank.Initialize
        SelectedMedia.HeadShot = Blank
     End If
to just
B4X:
If CapturedButton.Tag = 1 Then
     SelectedMedia.QuickHit.Initialize
Else
    SelectedMedia.HeadShot.Initialize
End If
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Thats what I ended up doing.

I had to be careful though because it was erasing the slot in my list as well, as the value was put into this variable from a for-each loop. Because of the way non-primitives work I guess.

So I had to create a ByVal function which creates a "copy" of the data instead of the reference pointer.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…