'New' not behaving as exprected. Advice needed urgently.

G

Guest

Hi

Long Post - Please read to end to understand my question.

I am writing a telnet server in VB.net

(code samples lower down)

I connect two hyperterm sessions to the code. (2 telnet sessions.)

The state object created when the socket connects as such

Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
Dim State As New StateObject()
State.sb = ""
State.UserData = New StateObject.CollectData()
.....
end

This triggers the new statement for
Public Class CollectData.
When I examine the memory of these items (using memory window)
they are assigned "nothing" before the call new new.

After the call to new the are assigned the same memory address for both
threads.

Can anyone please tell me why this is happening. The other items in the
structer
StockTakeNo
BranchCodeId
StockRoomId
UserName
Password
all appear correct and contail the data givvent to them by the new session.
The collections (having the same address in both threads) have a combined
pool of information. This is breaking the app as each session deals with and
writes different data to the database.


Thread 1
colBarCodeSize 0x0210A794
colBarCode 0x0210A790

Thread 2
colBarCodeSize 0x0210A794
colBarCode 0x0210A790

Other code can be posted as required. Any mindfull insight into this will be
appretiated.



The following are structures extracted from the code.

Public Class BarCodeSizeData
Public BarCodePrdSizeID As Long
Public BarCodeQty As Integer
End Class

Public Class BarCodeData
Public BarCodeString As String
Public BarCodePrdColourID As Long
Public TotPrdCodeQty As Integer
Public ArrLen As Integer
Public aListOfItems(50) As Integer
End Class

Public Class StateObject
Public Class CollectData
Public StockTakeNo As String
Public BranchCodeId As Long
Public StockRoomId As Long
Public UserName As String
Public Password As String
Public BinNo As String
Public ToCount As String
Public Counter As Integer
Public Shared colBarCode As BarCodeDataCollection
Public Shared colBarCodeSize As BarCodeSizeCollection

Sub New()
colBarCode = New BarCodeDataCollection()
colBarCodeSize = New BarCodeSizeCollection()
End Sub

End Class

Public Class BarCodeSizeCollection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal SizeData As BarCodeSizeData)
' Invokes Add method of the List object to add a widget.
List.Add(SizeData)
End Sub

Public Sub Remove(ByVal index As Integer)
' Check to see if there is a widget at the supplied index.
If index > Count - 1 Or index < 0 Then
Else
List.RemoveAt(index)
End If
End Sub

Public Property Item(ByVal index As Integer) As BarCodeSizeData
Get
' The appropriate item is retrieved from the List object and
' explicitly cast to the Widget type, then returned to the
' caller.
Return CType(List.Item(index), BarCodeSizeData)
End Get
Set(ByVal Value As BarCodeSizeData)
List.Item(index) = Value
End Set
End Property
end class

Public Class BarCodeDataCollection
Inherits System.Collections.CollectionBase

Public Sub Add(ByVal CodeData As BarCodeData)
List.Add(CodeData)
End Sub

Public Sub Remove(ByVal index As Integer)
' Check to see if there is a widget at the supplied index.
If index > Count - 1 Or index < 0 Then
Else
List.RemoveAt(index)
End If
End Sub

Public Property Item(ByVal index As Integer) As BarCodeData
Get
' The appropriate item is retrieved from the List object and
' explicitly cast to the Widget type, then returned to the
' caller.
Return CType(List.Item(index), BarCodeData)
End Get
Set(ByVal Value As BarCodeData)
List.Item(index) = Value
End Set
End Property

End Class
 
G

Guest

Hi,

Add a new procedure to your class to assign a value to the
variables even if it is a 0 or "" that way they are not nothing. I prefer to
use public properties instead of public variables for passing data to and
from the class. The properties allow you to validate the data being sent in.

Ken
-------------------------
 
G

Guest

Ken, thanks for the reply.

Towards the bottom of the post is the following :


Public Class StateObject
Public Class CollectData
Public StockTakeNo As String
Public BranchCodeId As Long
Public StockRoomId As Long
Public UserName As String
Public Password As String
Public BinNo As String
Public ToCount As String
Public Counter As Integer
Public Shared colBarCode As BarCodeDataCollection
Public Shared colBarCodeSize As BarCodeSizeCollection

Sub New()
colBarCode = New BarCodeDataCollection()
colBarCodeSize = New BarCodeSizeCollection()
End Sub

End Class

The 'new' is called when an instance of CollectData is created.
When a second instance of CollectData is created in another thread (another
connection) the addresses of colBarCode and colBarCodeSize are the same as
the previous thread. They were nothing before new got called . My problem :
Why are they pointing to the same memory. It appears that the new is not
creating a new instance of the collections but rather seeing that there is
one in memory already is using it instead.
 
G

Guest

Just relialised that myselef,
However when I remove the shared I get an error (probably why it was there
in the first place).

Found that too. Was using the variable type and not the varialble name in
the code.

Many thanks Ken.

This is appretiated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top