Create memory structure for receiving messages

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
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.
 
i check Dictionary properties and tried this codes.

VB.NET:
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.
 
Look through the classes in System.Collections namespace, also the System.Collections.Specialized classes is worth a glance. You have several dictionary type classes available for your framework version.
 
i checked the Hashtable class. and i tried this code.

VB.NET:
 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.
 
What you can't do is add the same key twice as you try to, you can always get the "item" by key and modify it.
VB.NET:
If dic.ContainsKey("key") Then
  DirectCast(dic("key"), ArrayList).Add("new arraylist item")
If you're holding collections of strings you should use StringCollection instead of ArrayList.
 
as you said i tried this code.

VB.NET:
 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())

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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles Button1.Click
        Dim tmp As New ArrayList
        Dim sListName As String
        Dim x As Byte

        tmp = IDHashTable.Item(TextBox1.Text)

        For x = 0 To tmp.Count - 1
            sListName = tmp.Item(x)
        Next
    End Sub

when i try to get values added to Key myMSG.UniqueOrderID, this error occurs.

An unhandled exception of type 'System.NullReferenceException' occurred in PGM-2.exe

Additional information: Object reference not set to an instance of an object.

when i try to run the line
tmp = IDHashTable.Item(TextBox1.Text)
tmp is nothing.


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.
 
What you are doing here is to add a new arraylist to the dic-arraylist each time, ie arraylist within arraylist within dictionary:
DirectCast(IDHashTable(myMSG.UniqueOrderID), ArrayList).Add(gArray)
Is that really the intention? The structure of the data is more like only one array for each dictionary entry:
VB.NET:
Sub Add(ByVal myMSG As MSG)
    Dim s As String
    If IDHashTable.ContainsKey(myMSG.UniqueOrderID) Then
        s = String.Format("{0},{1},{2}", myMSG.UniqueOrderID, myMSG.Itemno, myMSG.Description)
        gArray = IDHashTable(myMSG.UniqueOrderID)
        gArray.Add(s)
    Else
        s = String.Format("{0},{1}", myMSG.UniqueOrderID, myMSG.CurrOrderNo)
        gArray = New ArrayList
        gArray.Add(s)
        IDHashTable.Add(myMSG.UniqueOrderID, gArray)
    End If
    gArray = Nothing
End Sub

tmp = IDHashTable.Item(TextBox1.Text)
tmp is nothing.
I see you use UniqueOrderID.ToString places and do IDHashTable.Add(myMSG.UniqueOrderID, obj). Since it is pointless to do ToString with a something that is already a string this lead me to believe UniqueOrderID is not a string. Above you are trying to get an item by a string id. Run this code and see what you get:
Dim i As Integer = 1
MsgBox(i.GetHashCode)
Dim s As String = "1"
MsgBox(s.GetHashCode)
Both 1 and "1" can be added as keys to the same HashTable because they are different values that give different unique hash codes.
 
Back
Top