Hi,
I'm trying to make a class "User" where I save the logged user data. Then I wrote some public set/get subs for all the private attributes, but I can't access to these subs from other activities where I defined a variable of that class.
This is the code of the class:
LoggedUser class:
Sub Class_Globals
Private Id As Int
Private Email As String
Private Username As String
Private Psw As String
Private User_Type As String
Private Method_Access As String
Private Ucoin As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
Id = 0
Email = ""
Username = ""
Psw = ""
User_Type = ""
Method_Access = ""
Ucoin = 0
End Sub
'metodi get/set
Public Sub setId(num As Int)
Id = num
End Sub
Public Sub getId As Int
Return Id
End Sub
Public Sub setEmail(str As String)
Email = str
End Sub
Public Sub getEmail As String
Return Email
End Sub
Public Sub setPsw(str As String)
Psw = str
End Sub
Public Sub getPsw As String
Return Psw
End Sub
Public Sub setUsername(str As String)
Username = str
End Sub
Public Sub getUsername As String
Return Username
End Sub
Public Sub setTyp(str As String)
User_Type = str
End Sub
Public Sub getTyp As String
Return User_Type
End Sub
Public Sub setMethod(str As String)
Method_Access = str
End Sub
Public Sub getMethod As String
Return Method_Access
End Sub
Public Sub setUcoin(num As Int)
Ucoin = num
End Sub
Public Sub getUcoin As Int
Return Ucoin
End Sub
In the main activity I defined a global variable as:
LoggedUser instance:
Sub Process_Globals
Dim Utente As LoggedUser
End Sub
But when I try to use the class methods, I get this:
There must be no conflict between a property name and a variable name. You will need to change the variable names. For example change Id to mId.
Personally, I would never create such getters and setters unless you actually need to add logic in these methods.
Start with public fields and later when needed switch to properties. The other modules source code will stay the same.
Personally, I would never create such getters and setters unless you actually need to add logic in these methods.
Start with public fields and later when needed switch to properties. The other modules source code will stay the same.
So if I need a property I don't need to declare it in Sub Class_Globals, but I just have to define a get/set sub? Then if I need its value I write "object.property"?
So if I need a property I don't need to declare it in Sub Class_Globals, but I just have to define a get/set sub? Then if I need its value I write "object.property"?
Erel means that if you don't need to insert code inside the property-routines and the class has no methods, you can avoid to create a class, it's easier to declare public variables, for example in a code module or in the Starter service and access them directly.
In your case, I would create a custom type declared in the Activity Main (so it will also be possible to serialize it) and an instance of this type in the Starter service (just like you would create an instance of the class).
EDIT: it's B4XPages "era" : custom type declaration and public instance in B4XMainPage