Calling an object from Hashtable in a Class by Key -- how to?

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Hello,

In VB2005 (Winforms), I have a Class where I plan on storing a custom
object in a Hashtable, but I'm not sure how to retrieve the object via
the Class by key.

Here's my Class containing the Hashtable:

Public Class Account
Private _Transaction as Transaction
Private _TransactionHashTable as HashTable

Public Property NewTransaction() as Transaction
Get
Return _Transaction
End get
Set (ByVal value as Transaction)
_TransactionHashTable.Add(value.iTransactionID, value)
End Set
End Property
End Class

Transaction is an object containing Transaction data plus
TransactionID. I want to use TransactionID as the Key in the Hash
Table plus the entire object Transaction as the value.

Outside of using a Class I know I can use
_TransactionHashTable.Item(TransactionID)) to get the Transaction
object from the table, but how do I do this from a Class? I want to
pass the TransactionID to the Account Class and return the Transaction
object where the key patches, but VB wants the value in the Set to
match the value being returned.

I'm still green to Visual Basic, so this might have a simple solution
I'm not seeing, but I would enjoy any assistance or direction on how
to make this work.

Thanks --

Alex
 
Hello,

In VB2005 (Winforms), I have a Class where I plan on storing a custom
object in a Hashtable, but I'm not sure how to retrieve the object via
the Class by key.

Here's my Class containing the Hashtable:

Public Class Account
Private _Transaction as Transaction
Private _TransactionHashTable as HashTable

Public Property NewTransaction() as Transaction
Get
Return _Transaction
End get
Set (ByVal value as Transaction)
_TransactionHashTable.Add(value.iTransactionID, value)
End Set
End Property
End Class

Transaction is an object containing Transaction data plus
TransactionID. I want to use TransactionID as the Key in the Hash
Table plus the entire object Transaction as the value.

Outside of using a Class I know I can use
_TransactionHashTable.Item(TransactionID)) to get the Transaction
object from the table, but how do I do this from a Class? I want to
pass the TransactionID to the Account Class and return the Transaction
object where the key patches, but VB wants the value in the Set to
match the value being returned.

I'm still green to Visual Basic, so this might have a simple solution
I'm not seeing, but I would enjoy any assistance or direction on how
to make this work.

Thanks --

Alex

I've always preferred to use a dictionary (typed in message):

///////////////////////
Dim transactions as new Dictionary(Of String, Transaction)
transactions.Add(value.iTransactionID, value)
Dim val2 As Transaction = transactions("My Transaction Id")
///////////////////////

Thanks,

Seth Rowe [MVP]
 
Back
Top