Android Question Class Public Subs

Stefano Di Chiano

Active Member
Licensed User
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:
Immagine.png


What am I doing wrong?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
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.
 
Upvote 0

Stefano Di Chiano

Active Member
Licensed User
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"?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
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"?
No, your code is correct.

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.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
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 ;)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'll need to learn what is a custom type and the difference between it and a class, I'll look for some documentation, thanks for the suggestion.
Custom Types are almost the same of classes but without code inside.

Main Activity
B4X:
Type tUser(Id As Int, Email As String, Name As String, PW As String, UserType As String, AccessMethod As Strng, Ucoin As Int)


Starter service
B4X:
Sub Process_Globals
    Public User As tUser
'...

Sub Service_Create
   User.Initialize


From anywhere
B4X:
Starter.User.Name = "Erel"
 
Upvote 0
Top