Android Question Raise an event on a value change

hatzisn

Well-Known Member
Licensed User
Longtime User
Hello everyone,

What I want to ask, I searched for it in the forum but my search produced no satisfying results...
I would like to ask if anyone of you knows if it is possible to call a sub in a class (within the class) on a change of a value of the class.

Let's say that the class exposes a public variable like that:

B4X:
Sub Class_Globals
      Public lng as Long
End Sub

when we initialize the class with ClassName.Initialize is it possible to do that just bye setting the value lng?

f.e.
B4X:
ClassName.lng = 100

and that would call:
B4X:
Sub HandleChange
     'Do something with lng
End Sub

which is included in the class.

Perhaps someone would like to redirect me to a thread that I did not notice.

Thanks in advance
 

MarkusR

Well-Known Member
Licensed User
Longtime User
i think you need something like this and make the variable private
B4X:
Public Sub HandleChange(newvalue as long)   
    lng = newvalue
    'Do something with lng
End Sub
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
i think you need something like this and make the variable private
B4X:
Public Sub HandleChange(newvalue as long)  
    lng = newvalue
    'Do something with lng
End Sub

Hi Markus, thanks for the reply. I saw an example similar to your code in Classes tutorial but it is not what I am looking for (although it does the job).
What I am actually looking for (to increase my B4A knowledge) is some kind of handler that will handle the change of a value automatically. Is there such a thing?
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i believe you can not catch changes at primitives types.
this is an other way to catch changes
B4X:
ClassName.lng = MyChangedEvent(ClassName,100)
other option will be a own long class, means using a non-primitives type.
CallSubDelayed is also useful in your case, its closer to a event system.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Thanks for answering Markus. I was away from my computer and I just read it. Searching the forum I came along the speedometer view which does exactly that. When setting its value it redraws automatically the view so I believe if Johan has done it, it can be done. I will contact him tomorrow as it is very late now and my bed is looking forward to see me in person. Thank you for your time.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Make it a property.
Set your variable as Private.
Private lng as Long

Add this routine in your class:
B4X:
Public Sub setlng(Longitute as Long)
    lng = Longitude
    'your additional code
End Sub

In the calling code, when you set ClassName.lng = 100, the sub above will be called.

You may have a look at the B4X CustomView Booklet.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Thanks Klaus for answering. I will try that.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Make it a property.
Set your variable as Private.
Private lng as Long

Add this routine in your class:
B4X:
Public Sub setlng(Longitute as Long)
    lng = Longitude
    'your additional code
End Sub

In the calling code, when you set ClassName.lng = 100, the sub above will be called.

Hi klaus or everyone if someone else is aware of an answer for this question,

I do not understand the implementation of what is written above. If the variable lng is private how is it supposed to be called in an activity where the class is initialized? Am I missing something obvious here?

Thanks in advance
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Am I missing something obvious here?
I'm afraid yes:).

In classes you can define properties.
These routines define a MinValue property:
B4X:
Public Sub getMinValue As Double
    Return cMinValue
End Sub

Public Sub setMinValue(MinValue As Double)
    cMinValue = MinValue
    'additional code
End Sub

cMinValue is the class internal variable.
Routines beginning with 'get', lower case, define a readable property.
Routines beginning with 'set', lower case, define a writable property.
This property can be accessed with MyClass.MinValue = 123

Explained in chapter 4.4 Add Properties in the B4X CustomViews booklet.
 
Last edited:
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I'm afraid yes:).

In classes you can define properties.
These routines define a MinValue property:
B4X:
Public Sub getMinValue As Double
    Return cMinValue
End Sub

Public Sub setMinValue(MinValue As Double)
    cMinValue = MinValue
    'additional code
End Sub

cMinValue is the class internal variable.
Routines beginning with 'get', lower case, define a readable property.
Routines beginning with 'set', lower case, define a writable property.
This property can be accessed with MyClass.MinValue = 123

Explained in chapter 4.4 Add Properties in the B4X CustomViews booklet.


Thank you for answering Klaus and also thank you for your time. I tried that and it works. I was missing a very critical thing named 'Property'. I would like to ask though if the code is compiled to a library and not used by me, how will the user know that this property exist since the intelligence of the class does not show the properties (at least in B4A 7.80 which is my version). (Intelligence I mean if you define and initialize a class named 'cl' for example and then try to write 'cl' followed by the fullstop the property does not appear).
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi DonManfred,

I suppose judging by the capital letters you are having a hard day and I completely got in to your nerves with my question...
But watch this screens:


upload_2018-12-3_15-57-56.png upload_2018-12-3_15-58-26.png upload_2018-12-3_16-1-11.png

What can you make out of this? Have I done a major mistake that I do not see for the time being?

Thanks

PS. Also by saying user I mean the programmer that will uses my lib.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
What can you make out of this?
nothing. There is no project attached which i could open in B4A.
Ther images are so small, i can not read anything in them (old eyes).
The Capital letter YOU was only to let you know that it is you who need them as app-author. The App-USER does not see anything from the class.

The problem is that you defined the methods (getter and setter) as PRIVATE inside the class. So they are only useable from within the class.
A private method/property is not listed... Make them public.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Duuuhhh? o_Oo_Oo_Oo_Oo_O:):):):):):):):):):):):)

And the Oscar of stupidity goes to : ...........................................Me, myself and I...!!!!!!!!!
It was too obvious for me to see it... :D

Thank you DonManfred
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The internal help woks!!!
With this code in the Test class:
B4X:
'sets or gets the MinValue property.
Public Sub setMinValue(MinValue As Double)
    cMinValue = MinValue
End Sub

Public Sub getMinValue As Double
    Return cMinValue
End Sub

And in the calling Module:
upload_2018-12-3_17-7-30.png


You see what you need!
You see the property MinValue and the internal help.

The comments you put before the routine in the class is displayed as the help.
'sets or gets the MinValue property. in our example.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
would be nice that the pop up window also show the private stuff with a hint ;) and with a click navigate to the source.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
the list can be grouped by public/private but at least all subs should be shown from the class in any form.
it also happens that u need to change private to public. so a click into the source would be helpful.
yes, i wrote a new wish ... hope dies last.
all stumbling blocks should be removed.
if u type the dot that means that u will access some method from there.
a click in the popup list do not need to write the name there, instead it can go to the class.
the popup list can also have 2 check boxes at top to filter the list, or a search.
 
Upvote 0
Top