Bug? False "unused variable"

Nickelgrass

Active Member
Licensed User
Longtime User
Hi,
I am getting a false Warning #9 "Unused variable" in my program.
Here is a snipped that causes the warning:
B4X:
Dim rt As String
        For i = 0 To 5
            rt = "hello"
        Next
The variable is used in the for loop and the warning point to the line "Dim rt as string". I'm using 5.50.
Best regards!
 

DonManfred

Expert
Licensed User
Longtime User
"i" is not used
 

Nickelgrass

Active Member
Licensed User
Longtime User
Maybe, but the warning specifically says the string "rt" is not used with "Unused variable 'rt'. (warning #9)". So either way its a tiny bug as it then should say "unused variable i".
 

klaus

Expert
Licensed User
Longtime User
B4X:
Dim rt As String
For i = 0 To 5
    rt = "hello"
Next
If this is really your code, I don't understand the logic !
You go through the loop 6 times, but only the last one remains memorized in rt.
Why the loop !?
If it is not your real code, you must post the real code and not an uncomplete one having, for me, no real sense.
 

Nickelgrass

Active Member
Licensed User
Longtime User
The real code produced the same warning as the snipped.
I guess DonManfred has a point, assigning the Variable just isn't really using it. Thanks!
 

Nickelgrass

Active Member
Licensed User
Longtime User
@klaus: in the actual code the assignment of the string was each time different depending on the counter and not a constant.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
If all you ever do is assign the String variable some value, then it will be recognized as an unused variable. The code must use that value (as a parameter to a method, in a String comparison, etc...) for the variable to not be considered unused. And it's straightforward to see why this is so: that variable is like a black hole; values go in but never come out to be used again. It's a dead-end, having no effect on subsequent code. It might as well not exist.
 
Top