S Stefano Di Chiano Active Member Licensed User Oct 8, 2020 #1 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: Code: Copy 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: Code: Copy Sub Process_Globals Dim Utente As LoggedUser End Sub But when I try to use the class methods, I get this: What am I doing wrong?
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: Code: Copy 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: Code: Copy Sub Process_Globals Dim Utente As LoggedUser End Sub But when I try to use the class methods, I get this: What am I doing wrong?
LucaMs Expert Licensed User Longtime User Oct 8, 2020 #2 ? Try to clean the project (CTRL+P). Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Oct 8, 2020 #3 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
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.
S Stefano Di Chiano Active Member Licensed User Oct 8, 2020 #4 LucaMs said: Try to clean the project (CTRL+P). Click to expand... Unfortunately it doesn't work.. Upvote 0
S Stefano Di Chiano Active Member Licensed User Oct 8, 2020 #5 Erel said: There must be no conflict between a property name and a variable name. You will need to change the variable names. Click to expand... Is there a conflict due to the fact that I set the value of variables in the Initialize sub? Upvote 0
Erel said: There must be no conflict between a property name and a variable name. You will need to change the variable names. Click to expand... Is there a conflict due to the fact that I set the value of variables in the Initialize sub?
Erel B4X founder Staff member Licensed User Longtime User Oct 8, 2020 #6 The conflict is here: setEmail / getEmail / Email global variable Change Email to mEmail. Upvote 0
LucaMs Expert Licensed User Longtime User Oct 8, 2020 #7 Erel said: Change Email to mEmail. Click to expand... My habit Erel said: 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. Click to expand... ... and, especially in this case, even better a custom Type. Upvote 0
Erel said: Change Email to mEmail. Click to expand... My habit Erel said: 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. Click to expand... ... and, especially in this case, even better a custom Type.
S Stefano Di Chiano Active Member Licensed User Oct 8, 2020 #8 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
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"?
LucaMs Expert Licensed User Longtime User Oct 8, 2020 #9 Stefano Di Chiano said: 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"? Click to expand... 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
Stefano Di Chiano said: 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"? Click to expand... 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.
S Stefano Di Chiano Active Member Licensed User Oct 8, 2020 #10 Yes, it makes sense. You are right. Thanks for the help! Upvote 0
LucaMs Expert Licensed User Longtime User Oct 8, 2020 #11 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
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
S Stefano Di Chiano Active Member Licensed User Oct 8, 2020 #12 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. Upvote 0
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.
LucaMs Expert Licensed User Longtime User Oct 8, 2020 #13 Stefano Di Chiano said: 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. Click to expand... Custom Types are almost the same of classes but without code inside. Main Activity B4X: Copy 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: Copy Sub Process_Globals Public User As tUser '... Sub Service_Create User.Initialize From anywhere B4X: Copy Starter.User.Name = "Erel" Upvote 0
Stefano Di Chiano said: 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. Click to expand... Custom Types are almost the same of classes but without code inside. Main Activity B4X: Copy 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: Copy Sub Process_Globals Public User As tUser '... Sub Service_Create User.Initialize From anywhere B4X: Copy Starter.User.Name = "Erel"
S Stefano Di Chiano Active Member Licensed User Oct 8, 2020 #14 It looks more fitting for my project, I'll use a custom type then, thanks again! Upvote 0