Click to See Complete Forum and Search --> : Create memory structure for receiving messages


remya1000
03-04-2008, 10:03 AM
i'm using vb.net application program. i need to create a memory structure like this.


<Collection>
<uniqueID ID="100">
<Item itemno="0" desc="coffee" />
<Item itemno="1" desc="tea" />
<Item itemno="2" desc="milk" />
'
'
'
'
</UniqueID>
<uniqueID ID="200">
<Item itemno="0" desc="cup" />
<Item itemno="1" desc="plate" />
<Item itemno="2" desc="spoon" />
'
'
'
'
</UniqueID>
</Collection>

or a memory structure like this.

[100]-->[0,Cofee]-->[1,Tea]-->[2,Milk]
[200]-->[0,cup]-->[1,Plate]-->[2,Spoon]


when ever i receive a message i need to add that message to this kind of formatted memory structure. The message i receive will be in this format.
for example 1: "100,0,Coffee"
for example 2: "200,1,plate"

for the first time while i run the program for example i receive a message like "100,0,Coffee". then 100 is the UniqueID for that message. so i need to create a header name 100 and put the message (0,Cofee) under that header. after that if i receive a message like "200,0,cup". then 200 is uniqueID for that message. so i need to check whether header 200 is already exist in that memory structure, if not then create a header name 200 and put the message (0,cup) under that header. after that if i receive a message like "100,1,tea". then need to check whether header 100 already exist in that memory structure, if exist then add the message (1,tea) under the uniqueID 100.

so if i receive an item who's UniqueID that's not exist in memory structure, then need to create a new header for that UniqueID and add the items under it. And if i receive an item who's UniqueID that's already exist in memory structure, then need to add that item under that UniqueID header.

so when i need, for example if i call the header 100 then i can access the items i received for header 100 ([0,Cofee], [1,Tea], [2,Milk], ...) will be accessed. so using each UniqueID we can access the entire messages received for that ID.

if you have any idea how to do this, please help me. i'm not getting any idea. if you can provide an example then it will be great help for me. please.

thanks in advance.

remya1000
03-04-2008, 02:23 PM
i check Dictionary properties and tried this codes.


Public Class Form1
#Region "Structure"
Structure Order

'Data members
Private OrderID As Integer
Private OrderDetail As SortedDictionary(Of String, Integer)

Public Sub PlaceOrder(ByVal ID As Integer, ByVal Product As String, ByVal quantity As Integer)
OrderID = ID
OrderDetail.Add(Product, quantity)
End Sub

Public Sub UpdateOrder(ByVal Product As String, ByVal quantity As Integer)
OrderDetail.Add(Product, quantity)
End Sub

Public Sub Init()
Me.OrderDetail = New SortedDictionary(Of String, Integer)
End Sub

Public ReadOnly Property ID() As Integer
Get
Return Me.OrderID
End Get
End Property

End Structure
#End Region

Private placedOrder As Order = Nothing
Private OrderList As List(Of Order) = Nothing

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'new instance of the order structure
placedOrder = New Order
placedOrder.Init()
'Place an order
Me.placedOrder.PlaceOrder(100, "Coffee", 200)
'Save the order in a list of placed order
Me.OrderList = New List(Of Order)
Me.OrderList.Add(placedOrder)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim placedID As Integer = 100
For Each o As Order In Me.OrderList
'check if the id already exist in the list of orders
If o.ID = placedID Then 'if exist update
o.UpdateOrder("tea", 10)
Else 'create
placedOrder = New Order
placedOrder.Init()
placedOrder.PlaceOrder(placedID, "Coffee", 20)
Me.OrderList.Add(placedOrder)
End If
Next o
End Sub

End Class


but i'm using vb.net 2003 with framework 1.1. vb.net 2003 don't have SortedDictionary at all. so i'm not able to continue right now. how can i proceed this in Vb.net 2003.

i'm adding this feature to an existing program. That existing program is in vb.net 2003 framwork 1.1. Right now i can't move forward to next version of .net.

if you have any idea please let me know and if you can provide and example then it will be great help for me. please.

Thank in advance.

remya1000
03-05-2008, 02:14 PM
i checked the Hashtable class. and i tried this code.



Private IDHashTable As New Hashtable
Private gArray As ArrayList

Sub Add(ByVal Message As String)

If IDHashTable.ContainsKey(myMSG.UniqueOrderID) = True Then
gArray = New ArrayList
gArray.Add(myMSG.UniqueOrderID.ToString() & "," & myMSG.Itemno.ToString() & "," & myMSG.Description.ToString())

IDHashTable.Add(myMSG.UniqueOrderID, gArray)
gArray = Nothing
Else
gArray = New ArrayList
gArray.Add(myMSG.UniqueOrderID.ToString() & "," & myMSG.CurrOrderNo.ToString())
IDHashTable.Add(myMSG.UniqueOrderID, gArray)
End If
End Sub


but once i create a key, then i cannot add items to that key later.

when i try to create a new key for an item, its ok. but second time when i try to add a second item to that key then error occurs that


"Item has already been added. Key in dictionary: "253683" Key being added: "253683""


so i cannot add all messages comming for an ID as group.


<Collection>
<uniqueID ID="100">
<Item itemno="0" desc="coffee" />
<Item itemno="1" desc="tea" />
<Item itemno="2" desc="milk" />
'
'
'
'
</UniqueID>
<uniqueID ID="200">
<Item itemno="0" desc="cup" />
<Item itemno="1" desc="plate" />
<Item itemno="2" desc="spoon" />
'
'
'
'
</UniqueID>
</Collection>



when every i get a message, check whether that Id already exist and if exist add the new item under that id and if its not already exist need to create new ID. something like that.

Please help me. i'm not getting any idea right now. if you have any idea please let me know and if you can provide an example then that will be great help for me.

Thanks in advance.

remya1000
03-06-2008, 02:28 PM
i'm receiving message from a different program. when that program sends message to my current program, i need to save it, so that i can call those messages later. and later i need to send those saved message from my current program to another program where i need to display those messages seperately.


For Example:
PGM-1 --> is the program that sends messages
PGM-2 --> is the program that receives messages from P1 and save it depends upon the Unique ID.
PGM-3 --> is the program that receives message from P2 and displays the message in a screen
CMP-1 and CMP-2 --> Two different computers that runs PGM-1


PGM-1 can be run in two different computers (for example: CMP-1 and CMP-2). so PGM-2 receives messages from both CMP-1 and CMP-2 at a time. depend upon the UniqueID we can determine which message belongs to which ID.


When new UniqueID is created by CMP-1 or CMP-2 then a message will be passed to PMG-2 like this "1000,S". so "S - Start" means new Id 1000 created. when i receive a message like "1000,A,0,Coffee", so "A - Add" means added new item for order 1000. when i receive a message like "1000,C", so "C- Closed" means the uniqueID 1000 is done (so no more items will added again to that ID.


if First i receive an Id from CMP-1 as "2000,S" and then received a message as "1500, S" from CMP-2, then Id 2000 should be saved first and then 1500. Id should be saved depends upon the first New ID received by PGM-2. and later if i receive a message like "1500,A,0,Coffee" then need to add that item under ID 1500.


and PGM-3 can display only 2 UniqueID items at a time. and PGM-3 need to display a Closed UniqueId items in screen for 30 seconds. that's why i created PGM-2 to save items.

for example: CMP-1 and CMP-2 send 5 uniqueID items like 400, 500, 100, 300 and 200 to PGM-2 and PGM-2 receives something like this.


<Collection>
<uniqueID ID="400">
<Item id="400" itemno="1" desc="DDDD" />
<Item id="400" itemno="2" desc="EEEE" />
<Item id="400" itemno="3" desc="FFFF" />
<Item id="400" type="C" />
</UniqueID>
<uniqueID ID="500">
<Item id="500" itemno="1" type="A" desc="NNNN" />
<Item id="500" itemno="2" type="A" desc="MMMM" />
<Item id="500" type="C" />
</UniqueID>
<uniqueID ID="100">
<Item id="100" itemno="1" type="A" desc="coffee" />
<Item id="100" itemno="2" type="A" desc="tea" />
<Item id="100" type="C" />
</UniqueID>
<uniqueID ID="300">
<Item id="300" itemno="1" type="A" desc="AAAA" />
<Item id="300" itemno="2" type="A" desc="BBBB" />
'
'
'
(Not closed)
'
'
'
</UniqueID>
<uniqueID ID="200">
<Item id="200" itemno="1" desc="cup" />
<Item id="200" itemno="2" desc="plate" />
<Item id="200" itemno="3" desc="spoon" />
'
'
'
(Not closed)
'
'
'
</UniqueID>
</Collection>


so here ID 400, 500 and 100 were closed ones. so when PGM-3 runs, we need to send messages for ID 400 and 500 first to PGM-3. and i need to send the message to PGM-3 as if i receive from PGM-1. for eg: for ID 400, i need to send messages like ("400,s"), ("400,A,1,DDDD"), ("400,A,2,EEEE"), ("400,A,3,FFFF"), ("400,C") to PGM-3. and once both 400 and 500 closed messages were displayed in PGM-3 for 30 seconds i need to send next 2 ID items to PGM-3. ID 100 is closed one, so it can be displayed for 30 seconds and remove from screen. but Id 300 and 200 need to display untill thoses Id's were closed.

so i need to save the received messages seperately under corresponding UniqueId. so that later while i need to send each message received for that ID to PGM-3, i can call that ID and send each messages received for that ID seperately like example 400 sending messages ("400,s"), ("400,A,1,DDDD"), ("400,A,2,EEEE"), ("400,A,3,FFFF"), ("400,C").

Hope you understand what i'm tring to do and that's why i'm tring to add items like this.

if you have any idea please let me know and if you can provide and example then it will be great help for me. please.

Thank in advance.

chazzy
03-06-2008, 06:36 PM
I apologized, in my haste I figured someone was looking at this already since there were replied going around.

Anyways in the example with Hashtable, couldn't you just use set? Like this..


Private IDHashTable As New Hashtable
Private gArray As ArrayList

Sub Add(ByVal Message As String)

If IDHashTable.ContainsKey(myMSG.UniqueOrderID) = True Then
gArray = New ArrayList
gArray.Add(myMSG.UniqueOrderID.ToString() & "," & myMSG.Itemno.ToString() & "," & myMSG.Description.ToString())

IDHashTable.Set(myMSG.UniqueOrderID, gArray)
gArray = Nothing
Else
gArray = New ArrayList
gArray.Add(myMSG.UniqueOrderID.ToString() & "," & myMSG.CurrOrderNo.ToString())
IDHashTable.Add(myMSG.UniqueOrderID, gArray)
End If
End Sub


Also, I don't know if it's plausible or not (I'm not seeing anything in the .net reference guide) but couldn't the value in the key/value pair be a reference, and then you just modify the array list you're using? again, I'm not sure - i'm not a huge .net developer, i just know enough to debug.

Cstick
03-10-2008, 08:11 PM
How about this? You could probably pull it off with a dataset too.


Public Class MessageList

Private List As ArrayList

Public Sub New()
Me.List = New ArrayList
End Sub

Public Function Add(ByVal message As Message) As Integer
If Me.List.Contains(message) Then
Return -1
Else
Return Me.List.Add(message)
End If
End Function

Public Function Add(ByVal uniqueID As Integer, ByVal itemNo As Integer, ByVal description As String) As Integer
Return Me.Add(New Message(uniqueID, itemNo, description))
End Function

Public Function GetMessages(ByVal uniqueID As Integer) As Message()
Dim result As New ArrayList

For Each message As Message In Me.List
If message.UniqueID = uniqueID Then
result.Add(message)
End If
Next

Return result.ToArray
End Function


Public Class Message

Public Sub New(ByVal uniqueID As Integer, ByVal itemNo As Integer, ByVal description As String)
Me.UniqueID = UniqueID
Me.ItemNo = ItemNo
Me.Description = Description
End Sub

Private mUniqueID As Integer
Public Property UniqueID() As Integer
Get
Return mUniqueID
End Get
Set(ByVal value As Integer)
mUniqueID = value
End Set
End Property

Private mItemNo As Integer
Public Property ItemNo() As Integer
Get
Return mItemNo
End Get
Set(ByVal value As Integer)
mItemNo = value
End Set
End Property

Private mDescription As String
Public Property Description() As String
Get
Return mDescription
End Get
Set(ByVal value As String)
mDescription = value
End Set
End Property

End Class

End Class