Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having an object remove itself from a collection?

Status
Not open for further replies.

WilliamUT

IS-IT--Management
Oct 8, 2002
182
US
I am writing a server application that can handle multiple connections. When a client connects I stick add it into a collection. What im getting stuck on is when the client disconnects I need the client object to remove itself out of the collection. I am passing the parent collection refrence as a param but the problem is how does the client object know what index it is in the collection?
 
It is a custom collection type. It is a collection that I call clientobj which holds the socket connection and stream reader and writer. When the client logs off i want the object to remove itself from the collection and finalize.
 
Not exactly sure about that I am still fairly new with working with collections and am working off some code examples. Here is my code so far i have cut out most of the irrelivant stuff :



Public Class clsClientObj

Public Sub New(ByVal ParentCollectionRef As clsClientCol)
Col = ParentCollectionRef
End Sub
'accepts the incomming connection, opens the stream and starts the dataarrival thread
Public Sub Accept(ByVal Connection As System.Net.Sockets.TcpListener)

'accept the incomming connection
tcpClient = Connection.AcceptSocket

'open the stream to the socket so we can sent data back and forth
Stream = New NetworkStream(tcpClient)
StreamWriter = New BinaryWriter(Stream)
StreamReader = New BinaryReader(Stream)

'start the dataarrival thread so it always reads from the stream when data comes in from the
'client
DataArrivalThread = New Thread(AddressOf DataArrival)
DataArrivalThread.Priority = ThreadPriority.Lowest
DataArrivalThread.Start()

End Sub


...........................





Public Class clsClientCol
Inherits System.Collections.CollectionBase


Public Overridable Function Add(ByVal value As clsClientObj) As Integer
MyBase.List.Add(value)
End Function


Public Overridable Function Remove(ByVal value As clsClientObj) As Integer
MyBase.List.Remove(value)
End Function

Default Public Overridable Property Item(ByVal index As Integer) As clsClientObj
Get
Return DirectCast(MyBase.List.Item(index), clsClientObj)
End Get
Set(ByVal value As clsClientObj)
MyBase.List.Item(index) = value
End Set
End Property


End Class






Public Class Form1
Inherits System.Windows.Forms.Form


Private Client As New clsClientCol()
Private ClientObj As New clsClientObj(Client)


Private Sub Listener_ClientConnecting(ByVal Connection As System.Net.Sockets.TcpListener) Handles Listener.ClientConnecting
ClientObj.Accept(Connection) 'accept the incomming connection
Client.Add(ClientObj) 'add the client that just connected to the collection
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Listener.Start_Server() 'start the multithreaded listener
End Sub

Private Sub btnMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMsg.Click
MsgBox(Client.Count)

End Sub



End Class
 
To remove the object, just call your collections Remove method.

By the way. your collection is based on (inherits) System.Collections.CollectionBase

Public Class clsClientCol
Inherits System.Collections.CollectionBase

In multi-threading scenarios, be sure to protect (lock) your collection during Adds and Removes.


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thats the problem. The remove method requires an index number to be passed to it. I am trying to have one of the actual objects remove itself from the list. How does the object know what index belongs to it?
 
I see what your saying no index is passed i am actually passing refrence to the object. So I should be able to say Remove(Me) right?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top