'gmapRooms.Get(3).As(clsRoom) = Null' *** --> Compile-time error message ***
I wanted to run a test: a CallSubDelayed on an object-variable referring to an object that "no longer exists" (?).
So I created a B4J project "on the fly" and I came across some rather strange things (one of which I don't even write... at least for now).
It is not easy to explain in words what the problems are, soI am attaching the project, which is very simple (B4J).
A class has an object variable which refers to an object instantiated elsewhere.
This object is set to null. If inside the class I use a CallSubDelayed "to the variable", the method is executed as if the object still exists.
Another problem is:
clsRoom exists, of course, and the object in "3" too.B4X:'gmapRooms.Get(3).As(clsRoom) = Null' *** --> Compile-time error message ***
I know that in B4A in order for the garbage collector, which by the way you cannot invoke directly, to eliminate an object it is necessary to eliminate any reference to that object, but then how to do it in a case like that?
No, because there is a reference inside the clsUser (object user).If you remove the item in the Map, that should be enough.
The case. The global map contains N room objects. One of this is "passed" (reference) to an object User. While the user is accidentally disconnected from the server (the client loses the connection for a few seconds) the server can decide to delete the room from the map. At that point for the server the room no longer exists, but the user still has a reference to that room and could run CallSubDelayed2(ThatRoom, "Method", ...) which will work - error.But why test it, it will be collected.
One question remains: I did not understand why this is not compiled:
B4X:'gmapRooms.Get(3).As(clsRoom) = Null' *** --> Compile-time error message ***
Map.Get is a function that returns an object that you should be able to cast to a defined type.Map.Get(Key) is a function.
It will return a value
Type typSomething(Name As String)
'...
Log(CreatetypSomething("Erel").As(typSomething).Name)
Public Sub CreatetypSomething (Name As String) As Object
Dim t1 As typSomething
t1.Initialize
t1.Name = Name
Return t1
End Sub
Map.Get is a function that returns an object that you should be able to cast to a defined type.
Tested (and it works):
B4X:Type typSomething(Name As String) '... Log(CreatetypSomething("Erel").As(typSomething).Name)
B4X:Public Sub CreatetypSomething (Name As String) As Object Dim t1 As typSomething t1.Initialize t1.Name = Name Return t1 End Sub
Absolutely correct ??You can absolutely cast it, but you are also trying to set it to Null.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?