Sub Class Globals
Public Const ITERATE_ASCENDING As Int = 2
Public Const ITERATE_DESCENDING As Int = 4
Private IterationDirection As Int
End Sub
Public Sub Initialize(Direction As Int)
IterationDirection = Direction
End Sub
B4X:
Dim AClass As MyClass
AClass.Initialize(AClass.ITERATE_DESCENDING)
Direction And IterationDirection are 0. They should be 4. Surely a constant should be constant and not dependent on the object being initialized?
I don't fully understand your code example... (although I think I understand what you are describing)
But in your code example, you try to reset your constant IterationDirection to be equal to the value passed to the initialize method, which I think is wrong coding.
what happens if you try to log the constants from another module, like Main?
Instead of Constant you could use Private int and create a getter for it to return the value.
B4X:
Sub Class Globals
private ITERATE_ASCENDING As Int = 2
Private ITERATE_DESCENDING As Int = 4
Private IterationDirection As Int
End Sub
public sub getAscending as Int
return ITERATE_ASCENDING
end sub
public sub getDescending as Int
return ITERATE_DESCENDING
end sub
Public Sub Initialize(Direction As Int)
IterationDirection = Direction
End Sub
This time
B4X:
Dim AClass As MyClass
AClass.Initialize(AClass.DESCENDING)
should work.
You also can just not use Int declarations at all. Just return the value from the Getter. As here is not Setter the User can not change it (looks like an Constant)...
B4X:
public sub getAscending as Int
return 2
end sub
public sub getDescending as Int
return 4
end sub
He want to Initialze the Class with the Direction to use (Setting IterationDirection to the Value of one of the Constants). #4 should help here i guess (just wrote the code here in the Editor, not in an real project with a Class)
He want to Initialze the Class with the Direction to use (Setting IterationDirection to the Value of one of the Constants). #4 should help here i guess (just wrote the code here in the Editor, not in an real project with a Class)
Correct. Your suggestion doesn't work however for the same reason the constants don't work. Until you call the Initialize Sub variables aren't set to their initial values. I was expecting constants to be initialized. Looking at the generated Java code the constants are initialized as follows:
B4X:
public int _iterate_ascending = 0;
public int _iterate_descending = 0;
They aren't actually set to their respective values until the innerInitialize method is called from the _initialize method. In effect in the Java code a constant is being treated exactly like a variable.
If you don't need to initialize your class then change it to be a code module.
A class will always need to be initialized and only exists onwards. ( That is also why you can have multiple instances of a class in the same project)