Addobject

derez

Expert
Licensed User
Longtime User
In b4ppc it is possible to create and refer to an object using control("...")
I want to create objects while the program runs, and give each new object a name and a number (which is not known in advance), like control("xxx" & i ,object type)

Thinking about network example with comparison to B4ppc example which had this:
B4X:
Sub WaitForConnection_Tick   ' used to collect all the clients
If Server.Pending = True Then
   t = Now 'Used to distinguish between the objects names.
      'For each client two objects are created, a Client object and a BinaryFile object.
   AddObject("client" & t,"Client")
   AddObject("bin" & t,"BinaryFile")
   Control("client" & t,Client).New1
   Control("client" & t,Client).Value = Server.Accept 
   Control("bin" & t,Binaryfile).New1(Control("client" & t,Client).GetStream,False)
   AlClients.Add(t)
......

What is the way to do it in B4A ?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no need for AddObjet and Control keywords in Basic4android because objects are "first class citizens" here.
There are several ways to implement it.
If you want to refer to the object with a string then you can use a Map.

B4X:
For i = 0 to 1000
 Dim b As Button
 b.Initialize("")
 Map1.Put("b" & i, b)
Next

Sub GetButtonFromName(Name As String) As Button
 Dim b As Button
 b = Map1.Get(Name)
 Return b
End Sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
After thinking while driving I have found something better for this use:

B4X:
Sub Process_Globals
   Dim ServerSocket1 As ServerSocket
   Dim Socket1(10) As Socket
   Dim count As Int
.....

and

B4X:
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
   If Successful Then   
      Socket1(count) = NewSocket
      Timer1.Enabled = True
      InputStream1 = Socket1(count).InputStream
      OutputStream1 = Socket1(count).OutputStream
      ToastMessageShow("Connected", True)
   Else
      Msgbox(LastException.Message, "Error connecting")
   End If
   ServerSocket1.Listen 'Continue listening to new incoming connections
   count = count + 1
End Sub

This handles the sockets and I'll do the same to the streams where necessary. Of course there will be checking of count not to exceed 9 and to use the number of closed connections.

The above will work only if this
The server side application can handle new connections. It will just replace the previous connection with the new one.
is true in the case that the same socket is getting the newsocket, not as it is here where another socket is allocated. I can't test it.
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Tested, it works.
 

Attachments

  • network.jpg
    network.jpg
    57.9 KB · Views: 244
Upvote 0

derez

Expert
Licensed User
Longtime User
I want to connect the emulator as a client to the server running on the desktop (using B4ppc program).
When using the IP reported by the server (169.254.2.2) I dont achieve connection.
When I use 10.0.2.2 the client connects but probably not to this server because there is no response from the server to this connection.
How to set it ?
 
Upvote 0
Top