Android Question IDE warning unused variable

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Have code that alters a variable and saves it to KVS:

B4X:
Sub UpdateSettingAndKVS(strVarKey As String, oVar As Object, oVal As Object)
    
    oVar = oVal
    cMP.UpdateKVS(strVarKey, oVal)
    
End Sub

This will give the warning:

Unused variable 'ovar'. (warning #9)
Object

Clearly the variable is used and the code works fine and I wonder if this might be a minor bug.

RBS
 

Daestrum

Expert
Licensed User
Longtime User
If you read your code - you set oVar then dont use it in the sub. That's what triggers the message.
If this code is how you want it (like you set the passed variable), just add 'ignore to the line.

B4X:
oVar = oVal'ignore
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
If you read your code - you set oVar then dont use it in the sub. That's what triggers the message.
If this code is how you want it (like you set the passed variable), just add 'ignore to the line.

B4X:
oVar = oVal'ignore
It is used though, no?
The value of the variable (oVar) is changed.

RBS
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
It is used though, no?
The value of the variable (oVar) is changed.

RBS
A value is set to the variable, like X = 30 but it is never USED like myView.height = X.
You can add #ignorewarnings: XXX were xxx is the error number, and it will ignore all similar errors
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
A value is set to the variable, like X = 30 but it is never USED like myView.height = X.
You can add #ignore XXX were xxx is the error number, and it will ignore all similar errors
>>never USED like myView.height = X
It is not used in that way, no, but it is used.

>> You can add #ignore XXX
I don't want to do that as it might be a genuine warning, that is the argument was really not used at all.

I have put:
'ignore
after the Sub declaration

Putting it after oVar = oVal doesn't work.

RBS
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
>>never USED like myView.height = X
It is not used in that way, no, but it is used.

>> You can add #ignore XXX
I don't want to do that as it might be a genuine warning, that is the argument was really not used at all.

I have put:
'ignore
after the Sub declaration

Putting it after oVar = oVal doesn't work.

RBS
Warnings are "module" dependent, meaning that if you set a variable inside main (or any other module) but only use it inside a different module, then the warning will exist in the first module!
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Warnings are "module" dependent, meaning that if you set a variable inside main (or any other module) but only use it inside a different module, then the warning will exist in the first module!
Not sure I quite understand that one.
Either way, this still looks like a minor bug to me.

RBS
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Not sure I quite understand that one.
Either way, this still looks like a minor bug to me.

RBS
not a bug, it's the way it works... it's like if you write a phone number in a piece of paper... but you never call the number, you never USED the info, although you SET it!!
SO the IDE just warns you that you have a variable in your code, but never used it anywhere else. The IDE makes the assumption that any variables set in the code are meant to be used as values for something else, like a height, a color, a string, etc!
You can try to use dim x as int' ignore so that the IDE doesn't take into account that variable... but then, you will not be warned if for some reason you didn't used it.
This warnings allow for 2 things: Warn you, specially in large projects, that a variable, i.e: MyColor, is not used, but somewhere you used MyColors... And also so that unused variable, that MAY not be needed for the code to work, exists, so you can decide to keep it or delete it!
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
not a bug, it's the way it works... it's like if you write a phone number in a piece of paper... but you never call the number, you never USED the info, although you SET it!!
SO the IDE just warns you that you have a variable in your code, but never used it anywhere else. The IDE makes the assumption that any variables set in the code are meant to be used as values for something else, like a height, a color, a string, etc!
You can try to use dim x as int' ignore so that the IDE doesn't take into account that variable... but then, you will not be warned if for some reason you didn't used it.
This warnings allow for 2 things: Warn you, specially in large projects, that a variable, i.e: MyColor, is not used, but somewhere you used MyColors... And also so that unused variable, that MAY not be needed for the code to work, exists, so you can decide to keep it or delete it!
This is getting a bit iffy now.
The fact is that the Sub argument oVar is used in the Sub code.
The warning seems to originate from the Sub declaration as the 'ignore needs to be put after the Sub declaration, so to say that oVar is not used
seems somewhat wrong to me. Certainly it doesn't seem helpful.

RBS
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
It all depends how you interpret the word: used.

In any case the posted code is flawed as the actual variable passed (oVar) is not changed as intended.

B4X:
Sub UpdateSettingAndKVS(strVarKey As String, oVar As Object, oVal As Object) 'ignore
    
    If strVarKey = "Enums.strDialogButtonTextColour" Then
        Log("Before, Enums.strDialogButtonTextColour: " & Enums.strDialogButtonTextColour)
        Log("Before, oVar: " & cMP.CStr(oVar))
        Log("Before, oVal: " & cMP.CStr(oVal))
    End If
    
    oVar = oVal
    cMP.UpdateKVS(strVarKey, oVal)
    
    If strVarKey = "Enums.strDialogButtonTextColour" Then
        Log("After, Enums.strDialogButtonTextColour: " & Enums.strDialogButtonTextColour)
        Log("After, oVar: " & cMP.CStr(oVar))
        Log("After, oVal: " & cMP.CStr(oVal))
    End If
    
End Sub

Used like this:

B4X:
UpdateSettingAndKVS("Enums.strDialogButtonTextColour", Enums.strDialogButtonTextColour, "255,255,165,0")

The log will show:

Before, Enums.strDialogButtonTextColour: 255,255,165,200
Before, oVar: 255,255,165,200
Before, oVal: 255,255,165,0
After, Enums.strDialogButtonTextColour: 255,255,165,200
After, oVar: 255,255,165,0
After, oVal: 255,255,165,0

So the variable I wanted to change (Enums.strDialogButtonTextColour) has not changed, so this was faulty code.

So, the warning was good, but perhaps for a different reason.

RBS
 
Upvote 0
Top