Late binding problems?

  • Thread starter Thread starter jvb
  • Start date Start date
J

jvb

I didn't test this to see if it actually works, but it doesn't throw
the build error above...

Call this routine...

Private Sub SetMyClient(ByRef ClientToSet As
System.Net.Sockets.TcpClient, ByVal Value As
System.Net.Sockets.TcpClient)
ClientToSet = Value
End Sub

With this call...

Me.SetMyClient(CType(MyServerConnections(MyIndex),
PrivateServerConnectionStruct).client, < System.Net.Sockets.TcpClient

Why did you decide to go with a Structure instead of a class? I don't
believe you would have this problem with a class.
 
I want to prevent late binding of this statement:

MyServerConnections(Myindex).client = Value


But when I try with the code below, I get an editor error:
"Expression is a value and therefor cant be the target of an assignment"

CType(MyServerConnections(Myindex), PrivateServerConnectionStruct).Client =
value


What is going on here?



For reference, here is the struct definition:

Private Structure PrivateServerConnectionStruct
Dim client As System.Net.Sockets.TcpClient
Dim Connected_To_Server As Boolean
Dim Connected_User_Name As String
Dim Remote_port_num As Integer
Dim Remote_host_name As String
Dim MessageQueue As Queue
End Structure

Dim MyServerConnections As New ArrayList
 
This is what I am doing inside the class. There is probably some other way
to do this better, but I need a threadsafe way to get to
"MyServerConnections". I am using the PublicServerStruct as a "key" to look
up a connection. I thought an array pointer would be easy, but I cant
figure it out. So, below is a class that I am working on.

I will paste the class below:


Option Explicit On
'Option Strict On
Public Class COMMON_ServerConnections

Public Structure PublicServerStruct
Dim Remote_port_num As Integer
Dim Remote_host_name As String
End Structure

Private Structure PrivateServerConnectionStruct
Dim client As System.Net.Sockets.TcpClient
Dim Connected_To_Server As Boolean
Dim Connected_User_Name As String
Dim Remote_port_num As Integer
Dim Remote_host_name As String
Dim MessageQueue As Queue
End Structure

shared MyServerConnections As New ArrayList


#Region " Properties "

Public Property Client(ByVal PublicServerConnection As
PublicServerStruct) As System.Net.Sockets.TcpClient
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).client
Else
Return Nothing
End If
End Get
Set(ByVal Value As System.Net.Sockets.TcpClient)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
'Cant figure out how to get around late binding.
MyServerConnections(Myindex).client = Value
End If
End Set
End Property

Public Property Connected_To_Server(ByVal PublicServerConnection As
PublicServerStruct) As Boolean
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).Connected_To_Server
Else
Return Nothing
End If
End Get
Set(ByVal Value As Boolean)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
'Cant figure out how to get around late binding.
MyServerConnections(Myindex).Connected_To_Server = Value
End If
End Set
End Property

Public Property Connected_User_Name(ByVal PublicServerConnection As
PublicServerStruct) As String
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).Connected_User_Name
Else
Return Nothing
End If
End Get
Set(ByVal Value As String)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
'Cant figure out how to get around late binding.
MyServerConnections(Myindex).Connected_User_Name = Value
End If
End Set
End Property

Public Property MessageQueue(ByVal PublicServerConnection As
PublicServerStruct) As String
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).MessageQueue.Dequeue
Else
Return Nothing
End If
End Get
Set(ByVal Value As String)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).MessageQueue.Enqueue(Value)
End If
End Set
End Property

#End Region

Public Function Add(ByVal RemoteHostname As String, ByVal
RemotePortNumber As Integer) As PublicServerStruct

Dim ServerConnection As New PublicServerStruct
ServerConnection.Remote_host_name = RemoteHostname
ServerConnection.Remote_port_num = RemotePortNumber

If ReturnArrayIndexOfClient(ServerConnection) = -1 Then
Dim NewServerConnection As New PrivateServerConnectionStruct
NewServerConnection.Remote_host_name = RemoteHostname
NewServerConnection.Remote_port_num = RemotePortNumber
NewServerConnection.MessageQueue = New Queue

MyServerConnections.Add(NewServerConnection)
End If
End Function

Public Sub Remove(ByVal PublicServerConnection As PublicServerStruct)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
MyServerConnections.Add(Myindex)
End If
End Sub


Public Function ReturnAllConnections() As ArrayList

Dim MyConnections As ArrayList
Dim PrivateConnection As PrivateServerConnectionStruct

For Each PrivateConnection In MyServerConnections
Dim ServerConnection As New PublicServerStruct
ServerConnection.Remote_host_name =
PrivateConnection.Remote_host_name
ServerConnection.Remote_port_num =
PrivateConnection.Remote_port_num
MyConnections.Add(ServerConnection)
Next

Return MyConnections
End Function

Private Function ReturnArrayIndexOfClient(ByVal PublicServerConnection
As PublicServerStruct) As Integer
Dim ServerConnection As New PrivateServerConnectionStruct

Dim MyIndex As Integer = 0
For Each ServerConnection In MyServerConnections
MyIndex += 1
If ServerConnection.Remote_host_name =
PublicServerConnection.Remote_host_name Then
If ServerConnection.Remote_port_num =
PublicServerConnection.Remote_port_num Then
Return MyIndex
End If
End If
Next

'Cant find anythnig, so return ... -1

Return -1
End Function


End Class
 
Ok, this appears to be working. Its the wierdest thing I have seen yet in
..Net:

It appears that Directly manipulating the MyServerConnections ArrayList
confuses everything. So, creating a "pointer", updating the pointer then
copying back seems to be working. WHAT A PAIN.

Is this a bug? This is not how you would think it should work.

Public Function SetClient(ByVal PublicServerConnection As
PublicServerStruct, ByRef MyClient As System.Net.Sockets.TcpClient)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)

If Not IsNothing(Myindex) Then
Dim LocalPrivateServer As PrivateServerConnectionStruct
LocalPrivateServer = CType(MyServerConnections(Myindex - 1),
PrivateServerConnectionStruct)
LocalPrivateServer.client = MyClient
MyServerConnections(Myindex - 1) = LocalPrivateServer
End If
End Function
 
Back
Top