RandomCoder Well-Known Member Licensed User Longtime User Sep 4, 2015 #1 Sample code to re-create the problem... Code: Sub Activity_Create(FirstTime As Boolean) For X = 0 To 5 Log(X) Next For Y = 0 To 5 Dim X As Int = Y Log(X) Next End Sub NOTE: Only happens when trying to run in 'Rapid Debug' mode. In Rapid Debug this code will fail to compile with the following error message "An error occurred. Error compiling program. Current declaration does not match the previous one. Make sure to declare the variable before it is used in a For loop. Occurred on Line: 27 Dim X As Int = Y" Click to expand... In both Legacy Debug and Release mode it will compile without error and run just fine on the device. PS. I'm pretty careful to always fully declare my variables and only fell foul of this whilst modifying someone else's code. The sample above is meaningless and purely for the purpose of recreating the error.
Sample code to re-create the problem... Code: Sub Activity_Create(FirstTime As Boolean) For X = 0 To 5 Log(X) Next For Y = 0 To 5 Dim X As Int = Y Log(X) Next End Sub NOTE: Only happens when trying to run in 'Rapid Debug' mode. In Rapid Debug this code will fail to compile with the following error message "An error occurred. Error compiling program. Current declaration does not match the previous one. Make sure to declare the variable before it is used in a For loop. Occurred on Line: 27 Dim X As Int = Y" Click to expand... In both Legacy Debug and Release mode it will compile without error and run just fine on the device. PS. I'm pretty careful to always fully declare my variables and only fell foul of this whilst modifying someone else's code. The sample above is meaningless and purely for the purpose of recreating the error.
LucaMs Expert Licensed User Longtime User Sep 4, 2015 #2 If I'm not mistaken, if you do not declare the variable type, B4A implies String. The second declaration is equivalent to a Redim (VB.net) but you have changed the type to Int. As you rightly wrote, it is better to explicitly write the type.
If I'm not mistaken, if you do not declare the variable type, B4A implies String. The second declaration is equivalent to a Redim (VB.net) but you have changed the type to Int. As you rightly wrote, it is better to explicitly write the type.
LucaMs Expert Licensed User Longtime User Sep 4, 2015 #3 Code: Dim X As Int For X = 0 To 5 Log(X) Next For Y = 0 To 5 Dim X As Int = Y Log(X) Next If you try this (added only the first line) there will be no errors.
Code: Dim X As Int For X = 0 To 5 Log(X) Next For Y = 0 To 5 Dim X As Int = Y Log(X) Next If you try this (added only the first line) there will be no errors.
RandomCoder Well-Known Member Licensed User Longtime User Sep 4, 2015 #4 LucaMs said: Code: Dim X As Int For X = 0 To 5 Log(X) Next For Y = 0 To 5 Dim X As Int = Y Log(X) Next If you try this (added only the first line) there will be no errors. Click to expand... Hi @LucaMs I realise that declaring the type before the first For loop will cure the problem and this is how I usually code. But my concern was that the problem is only hightlighted in Rapid Debug mode. The other methods of compiling work without error. It's also interesting that if you change the "Int" in Dim X As Int = Y to "String" or "Double" or "Object" it still throws errors. I wonder what X is initially defined as when used in the For loop without explicitly declaring the type?
LucaMs said: Code: Dim X As Int For X = 0 To 5 Log(X) Next For Y = 0 To 5 Dim X As Int = Y Log(X) Next If you try this (added only the first line) there will be no errors. Click to expand... Hi @LucaMs I realise that declaring the type before the first For loop will cure the problem and this is how I usually code. But my concern was that the problem is only hightlighted in Rapid Debug mode. The other methods of compiling work without error. It's also interesting that if you change the "Int" in Dim X As Int = Y to "String" or "Double" or "Object" it still throws errors. I wonder what X is initially defined as when used in the For loop without explicitly declaring the type?
moster67 Expert Licensed User Longtime User Sep 4, 2015 #5 If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. This means that X in most cases is Int in for statements and also in this example.
If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. This means that X in most cases is Int in for statements and also in this example.
RandomCoder Well-Known Member Licensed User Longtime User Sep 4, 2015 #6 moster67 said: If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. This means that X in most cases is Int in for statements and also in this example. Click to expand... I can agree with that as changing the type when declaring X in the second For loop throws a cast type error. Also it is quite happy to compile in Release and Legacy Debug. The problem is with Rapid Debug where it seems to be a little over zealous.
moster67 said: If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. This means that X in most cases is Int in for statements and also in this example. Click to expand... I can agree with that as changing the type when declaring X in the second For loop throws a cast type error. Also it is quite happy to compile in Release and Legacy Debug. The problem is with Rapid Debug where it seems to be a little over zealous.
LucaMs Expert Licensed User Longtime User Sep 5, 2015 #7 RandomCoder said: But my concern was that the problem is only hightlighted in Rapid Debug mode. The other methods of compiling work without error. Click to expand... Yes, I'd understand; you've noticed that there is no "consistency". moster67 said: If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. Click to expand... Yes, you're right. I have a motto: "Try" ; so I tried and GetType (x) returns "java.lang.Integer" (as default). If you do not define the type of a parameter of a routine, B4A uses String. I supposed it was the same for variables (though I doubted: a loop with a String as counter? )
RandomCoder said: But my concern was that the problem is only hightlighted in Rapid Debug mode. The other methods of compiling work without error. Click to expand... Yes, I'd understand; you've noticed that there is no "consistency". moster67 said: If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. Click to expand... Yes, you're right. I have a motto: "Try" ; so I tried and GetType (x) returns "java.lang.Integer" (as default). If you do not define the type of a parameter of a routine, B4A uses String. I supposed it was the same for variables (though I doubted: a loop with a String as counter? )
RandomCoder Well-Known Member Licensed User Longtime User Sep 5, 2015 #8 Well I never knew there was a GetType Method... GetType (object As Object) As String Returns a string representing the object's java type. Time for bed now, but at least I can go to sleep knowing that I have learnt something new today
Well I never knew there was a GetType Method... GetType (object As Object) As String Returns a string representing the object's java type. Time for bed now, but at least I can go to sleep knowing that I have learnt something new today
LucaMs Expert Licensed User Longtime User Sep 5, 2015 #9 RandomCoder said: Well I never knew there was a GetType Method... GetType (object As Object) As String Returns a string representing the object's java type. Time for bed now, but at least I can go to sleep knowing that I have learnt something new today Click to expand... I, too, should go to sleep; instead I'm going to write a post that will be hated and controversial
RandomCoder said: Well I never knew there was a GetType Method... GetType (object As Object) As String Returns a string representing the object's java type. Time for bed now, but at least I can go to sleep knowing that I have learnt something new today Click to expand... I, too, should go to sleep; instead I'm going to write a post that will be hated and controversial
Erel B4X founder Staff member Licensed User Longtime User Sep 6, 2015 #10 The type of undeclared variables in For loops is Int. https://www.b4x.com/android/forum/pages/results/?query=for The issue you encounter here is indeed specific to the rapid debugger. The error message tells you how to fix it. You need to declare the variable before the first for loop and it will work. It is difficult to explain the reason for this error without writing several pages that explain how the rapid debugger works. It is related to an optimization done by the compiler in the first loop.
The type of undeclared variables in For loops is Int. https://www.b4x.com/android/forum/pages/results/?query=for The issue you encounter here is indeed specific to the rapid debugger. The error message tells you how to fix it. You need to declare the variable before the first for loop and it will work. It is difficult to explain the reason for this error without writing several pages that explain how the rapid debugger works. It is related to an optimization done by the compiler in the first loop.