what collection obj can hold 3 parameters per index?

G

Guest

Yes, I need to store some values in an array type collection object that can
hold 3 or more parameters per index. I have looked at the collection object,
hashtable object and would prefer not to hassel with a multi-dimensional
array. Is there such an object in VB.Net?

Dim obj As someCollectionObj
obj.Add("parmA1", "parmA2", "parmA3")
obj.Add("parmB1", "parmB2", "parmB3")
....

retrieve items
s = obj.Item("parmA1", 1)
t = obj.Item("parmA1", 2)

or, can a Hashtable contain a hashtable?

Thanks,
Rich
 
G

Guest

I tried writing a collection type class, but it does not return anything.
What do I need to do to it so that it returns the values?

Sub Form_Load(...)
dim x As New clsSubScr
dim i As Integer, str1 As String
x.MailSrv = "test1"
x.MailSrv = "test2"
x.Mailsrv = "test3"
For i = 1 To 3
x.coIDx = i
str1 = x.Mailsrv '<---this gets nothing even though the collect object
contains
Console.Writeline(str1) ' the values "test1", "test2", "test3"
Next


Class clsSubScr
Dim colmailSrv As New Collection, colListNO As New Collection
Dim colID As New Collection
Public coIdx As Integer
Dim str1 As String

Property mailSrv() As String
Get
str1 = colmailSrv(coIdx)
End Get
Set(ByVal Value As String)
colmailSrv.Add(Value)
End Set
End Property

Property listNO() As String
Get
str1 = colListNO(coIdx)
End Get
Set(ByVal Value As String)
colListNO.Add(Value)
End Set
End Property

Property listID() As String
Get
str1 = colID(coIdx)
End Get
Set(ByVal Value As String)
colID.Add(Value)
End Set
End Property

End Class


Thanks,
Rich
 
C

Chris Dunaway

Why not make a structure for holding parameters and then add that to an
hashtable?

Public Structure Item
Dim ParamA2 As String
Dim ParamA3 As String

Public Sub New (a2 as String, a3 As String)
ParamA2 = a2
ParamA3 = a3
End Sub
End Structure

Dim obj As New HashTable

obj.Add("paramA1", New Item("paramA2", "paramA3"))
obj.Add("paramB1", New Item("paramB2", "paramB3"))

s = obj("paramA1").ParamA2
t = obj("paramA1").ParamA3



And yes, a hashtable can contain a hashtable.
 
G

Guest

Rich,

One option is to create a class with 3 properties representing the parameter
data.

Then create an object from the class, fill in the 3 properties with the
parameter values and add the object to an arraylist.

Kerry Moorman
 
J

Jay B. Harlow [MVP - Outlook]

Rich,
| I need to store some values in an array type collection object that can
| hold 3 or more parameters per index.

It sounds like you want a collection of collections. Is the outer collection
keyed or not keyed? Is the inner collection keyed or not keyed? In other
words is row one known by a specific Key or simply as row one?

| would prefer not to hassel with a multi-dimensional
| array.
Rather then a 2 dimensional array, have you considered an array of arrays?

Dim block(10,10) as String ' 2 dimensional array each row = 10, each
column is 10

Dim jagged(10)() As String ' ragged/jagged array, 10 rows or 0 or more
elements.


Have you looked at the NameValueCollection?

http://msdn.microsoft.com/library/d...sspecializednamevaluecollectionclasstopic.asp

It associates a String Key with one or more String values. If your values
and/or keys are not strings NameValueCollection probably won't work...

| retrieve items
| s = obj.Item("parmA1", 1)
| t = obj.Item("parmA1", 2)
You may need to define a custom collection that encapsulates one of the
above for storage.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Yes, I need to store some values in an array type collection object that
can
| hold 3 or more parameters per index. I have looked at the collection
object,
| hashtable object and would prefer not to hassel with a multi-dimensional
| array. Is there such an object in VB.Net?
|
| Dim obj As someCollectionObj
| obj.Add("parmA1", "parmA2", "parmA3")
| obj.Add("parmB1", "parmB2", "parmB3")
| ...
|
| retrieve items
| s = obj.Item("parmA1", 1)
| t = obj.Item("parmA1", 2)
|
| or, can a Hashtable contain a hashtable?
|
| Thanks,
| Rich
 
G

Guest

Thanks for your suggestion of the NameValueCollection. May I ask if you
could share an example how it is used?

As for keys, the first element in each row is unique so that could be a key.
Else, I was just going to loop through the collection. But I tried writing
a class with the 3 properties of the stuff I need to store. But I can't
retrieve the values. The class effort is my 2nd post in this thread. Would
you have any suggestions what I could do to the class to make it work?

One other suggestion I got was to use a Hashtable with a structure. The
also seems like a good idea.

Rich
 
G

Guest

Yes, I need to store some values in an array type collection object that
can
hold 3 or more parameters per index. I have looked at the collection object,
hashtable object and would prefer not to hassel with a multi-dimensional
array. Is there such an object in VB.Net?


Below is a rough draft of class HashTableStringArray. It uses a hashtable.
The key is a string, and the item stored for each key is an array of strings.
A brief test procedure is at the bottom. For each key string, your store
multiple values. For each key, you can retrieve all values or one value by
its zero based index.


Public Class HashTableStringArray
Public ht As New Hashtable ' Key-Item pairs where each Item is an array of
strings
Public Sub Add(ByVal Key As String, ByVal ParamArray Item() As String)
' if Key is found, replace Item with Data, otherwise add Key and Item
If ht.Contains(Key) Then
ht.Item(Key) = Item
Else
ht.Add(Key, Item)
End If
End Sub
Public Sub Remove(ByVal Key As String)
' if Key is found, remove it, otherwise no operation
If ht.Contains(Key) Then ht.Remove(Key)
End Sub
Public Function FetchArray(ByVal Key As String) As String()
' if Key is found, return its string array, otherwise return nothing
If ht.Contains(Key) Then
Return CType(ht.Item(Key), String())
Else
Return Nothing
End If
End Function
Public Function FetchIndex(ByVal Key As String, ByVal Index As Integer) As
String
' if Key is found and Index is in bounds, return Index'th string,
otherwise return ""
Dim a() As String = FetchArray(Key)
If a Is Nothing Then
Return ""
ElseIf Index < LBound(a) Or Index > UBound(a) Then
Return ""
Else
Return a(Index)
End If
End Function
Public Shared Sub Test()
' test
Dim zz As New HashTableStringArray
zz.Add("aaa", "x0", "x1")
zz.Add("bbb", "y0", "y1", "y2")
Dim s As String
s = zz.FetchIndex("aaa", 1) ' s="x1"
s = zz.FetchIndex("bbb", 6) ' s=""
s = zz.FetchIndex("bbb", 2) ' s="y2"
s = zz.FetchIndex("ccc", 1) ' s=""
s = ""
End Sub
End Class
 
J

Jay B. Harlow [MVP - Outlook]

| Thanks for your suggestion of the NameValueCollection. May I ask if you
| could share an example how it is used?

Dim list As New System.Collections.Specialized.NameValueCollection
list.Add("parmA1", "parmA2")
list.Add("parmA1", "parmA3")
list.Add("parmB1", "parmB2")
list.Add("parmB1", "parmB3")

Dim a1 As String
Dim b1 As String
a1 = list.Item("parmA1")
b1 = list.Item("parmB1")

Dim a1s() As String = list.Item("parmA1").Split(","c)
Dim b1s() As String = list.Item("parmB1").Split(","c)

If you like you could encapsulate some of the logic into a custom version of
NameValueCollection that supported adding a list of values for a key &
returning one value for a key, something like:

Public Class someCollectionObj
Inherits System.Collections.Specialized.NameValueCollection

Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
values() As String)
For Each value As String In values
Add(key, value)
Next
End Sub

Default Public Overloads ReadOnly Property Item(ByVal key As String,
ByVal index As Integer) As String
Get
Dim values() As String = Item(key).Split(","c)
Return values(index)
End Get
End Property

Public ReadOnly Property ItemCount(ByVal key As String) As Integer
Get
Return Item(key).Split(","c).Length
End Get
End Property

End Class

Then you could use the above, something like:

Dim list As New someCollectionObj
list.Add("parmA1", "parmA2", "parmA3")
list.Add("parmB1", "parmB2", "parmB3", "parmB4")

For Each key As String In list.Keys
Debug.WriteLine(Nothing, key)
Debug.Indent()
For index As Integer = 0 To list.ItemCount(key) - 1
Debug.WriteLine(list.Item(key, index), "item " & index)
Next
Debug.Unindent()
Next

I would consider adding an indexers like:

Default Public Overloads ReadOnly Property Item(ByVal key As String)
As String()
Get
Return MyBase.Item(key).Split(","c)
End Get
End Property

Default Public Overloads ReadOnly Property Item(ByVal index As
Integer) As String()
Get
Return MyBase.Item(index).Split(","c)
End Get
End Property

Which return an array of strings for a key/index rather then the comma
delimited string.

I might also consider an indexer something like:

Default Public Overloads ReadOnly Property Item(ByVal indexKey As
Integer, ByVal indexValue As Integer) As String
Get
Dim values() As String = MyBase.Item(indexKey).Split(","c)
Return values(indexValue)
End Get
End Property

If I needed to index that way...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Thanks for your suggestion of the NameValueCollection. May I ask if you
| could share an example how it is used?
|
| As for keys, the first element in each row is unique so that could be a
key.
| Else, I was just going to loop through the collection. But I tried
writing
| a class with the 3 properties of the stuff I need to store. But I can't
| retrieve the values. The class effort is my 2nd post in this thread.
Would
| you have any suggestions what I could do to the class to make it work?
|
| One other suggestion I got was to use a Hashtable with a structure. The
| also seems like a good idea.
|
| Rich
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > Rich,
| > | I need to store some values in an array type collection object that
can
| > | hold 3 or more parameters per index.
| >
| > It sounds like you want a collection of collections. Is the outer
collection
| > keyed or not keyed? Is the inner collection keyed or not keyed? In other
| > words is row one known by a specific Key or simply as row one?
| >
| > | would prefer not to hassel with a multi-dimensional
| > | array.
| > Rather then a 2 dimensional array, have you considered an array of
arrays?
| >
| > Dim block(10,10) as String ' 2 dimensional array each row = 10, each
| > column is 10
| >
| > Dim jagged(10)() As String ' ragged/jagged array, 10 rows or 0 or
more
| > elements.
| >
| >
| > Have you looked at the NameValueCollection?
| >
| >
http://msdn.microsoft.com/library/d...sspecializednamevaluecollectionclasstopic.asp
| >
| > It associates a String Key with one or more String values. If your
values
| > and/or keys are not strings NameValueCollection probably won't work...
| >
| > | retrieve items
| > | s = obj.Item("parmA1", 1)
| > | t = obj.Item("parmA1", 2)
| > You may need to define a custom collection that encapsulates one of the
| > above for storage.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > ..NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | Yes, I need to store some values in an array type collection object
that
| > can
| > | hold 3 or more parameters per index. I have looked at the collection
| > object,
| > | hashtable object and would prefer not to hassel with a
multi-dimensional
| > | array. Is there such an object in VB.Net?
| > |
| > | Dim obj As someCollectionObj
| > | obj.Add("parmA1", "parmA2", "parmA3")
| > | obj.Add("parmB1", "parmB2", "parmB3")
| > | ...
| > |
| > | retrieve items
| > | s = obj.Item("parmA1", 1)
| > | t = obj.Item("parmA1", 2)
| > |
| > | or, can a Hashtable contain a hashtable?
| > |
| > | Thanks,
| > | Rich
| >
| >
| >
 
J

Jay B. Harlow [MVP - Outlook]

Doh!
| Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| values() As String)
| For Each value As String In values
| Add(key, value)
| Next
| End Sub
The above code assumes that not passing any values is NOT a problem. which
may or may not be a valid assumption.

Is:

list.Add("parmA1")

Valid?



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


message || Thanks for your suggestion of the NameValueCollection. May I ask if you
|| could share an example how it is used?
|
| Dim list As New System.Collections.Specialized.NameValueCollection
| list.Add("parmA1", "parmA2")
| list.Add("parmA1", "parmA3")
| list.Add("parmB1", "parmB2")
| list.Add("parmB1", "parmB3")
|
| Dim a1 As String
| Dim b1 As String
| a1 = list.Item("parmA1")
| b1 = list.Item("parmB1")
|
| Dim a1s() As String = list.Item("parmA1").Split(","c)
| Dim b1s() As String = list.Item("parmB1").Split(","c)
|
| If you like you could encapsulate some of the logic into a custom version
of
| NameValueCollection that supported adding a list of values for a key &
| returning one value for a key, something like:
|
| Public Class someCollectionObj
| Inherits System.Collections.Specialized.NameValueCollection
|
| Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| values() As String)
| For Each value As String In values
| Add(key, value)
| Next
| End Sub
|
| Default Public Overloads ReadOnly Property Item(ByVal key As
String,
| ByVal index As Integer) As String
| Get
| Dim values() As String = Item(key).Split(","c)
| Return values(index)
| End Get
| End Property
|
| Public ReadOnly Property ItemCount(ByVal key As String) As Integer
| Get
| Return Item(key).Split(","c).Length
| End Get
| End Property
|
| End Class
|
| Then you could use the above, something like:
|
| Dim list As New someCollectionObj
| list.Add("parmA1", "parmA2", "parmA3")
| list.Add("parmB1", "parmB2", "parmB3", "parmB4")
|
| For Each key As String In list.Keys
| Debug.WriteLine(Nothing, key)
| Debug.Indent()
| For index As Integer = 0 To list.ItemCount(key) - 1
| Debug.WriteLine(list.Item(key, index), "item " & index)
| Next
| Debug.Unindent()
| Next
|
| I would consider adding an indexers like:
|
| Default Public Overloads ReadOnly Property Item(ByVal key As
String)
| As String()
| Get
| Return MyBase.Item(key).Split(","c)
| End Get
| End Property
|
| Default Public Overloads ReadOnly Property Item(ByVal index As
| Integer) As String()
| Get
| Return MyBase.Item(index).Split(","c)
| End Get
| End Property
|
| Which return an array of strings for a key/index rather then the comma
| delimited string.
|
| I might also consider an indexer something like:
|
| Default Public Overloads ReadOnly Property Item(ByVal indexKey As
| Integer, ByVal indexValue As Integer) As String
| Get
| Dim values() As String = MyBase.Item(indexKey).Split(","c)
| Return values(indexValue)
| End Get
| End Property
|
| If I needed to index that way...
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| || Thanks for your suggestion of the NameValueCollection. May I ask if you
|| could share an example how it is used?
||
|| As for keys, the first element in each row is unique so that could be a
| key.
|| Else, I was just going to loop through the collection. But I tried
| writing
|| a class with the 3 properties of the stuff I need to store. But I can't
|| retrieve the values. The class effort is my 2nd post in this thread.
| Would
|| you have any suggestions what I could do to the class to make it work?
||
|| One other suggestion I got was to use a Hashtable with a structure. The
|| also seems like a good idea.
||
|| Rich
||
|| "Jay B. Harlow [MVP - Outlook]" wrote:
||
|| > Rich,
|| > | I need to store some values in an array type collection object that
| can
|| > | hold 3 or more parameters per index.
|| >
|| > It sounds like you want a collection of collections. Is the outer
| collection
|| > keyed or not keyed? Is the inner collection keyed or not keyed? In
other
|| > words is row one known by a specific Key or simply as row one?
|| >
|| > | would prefer not to hassel with a multi-dimensional
|| > | array.
|| > Rather then a 2 dimensional array, have you considered an array of
| arrays?
|| >
|| > Dim block(10,10) as String ' 2 dimensional array each row = 10,
each
|| > column is 10
|| >
|| > Dim jagged(10)() As String ' ragged/jagged array, 10 rows or 0 or
| more
|| > elements.
|| >
|| >
|| > Have you looked at the NameValueCollection?
|| >
|| >
|
http://msdn.microsoft.com/library/d...sspecializednamevaluecollectionclasstopic.asp
|| >
|| > It associates a String Key with one or more String values. If your
| values
|| > and/or keys are not strings NameValueCollection probably won't work...
|| >
|| > | retrieve items
|| > | s = obj.Item("parmA1", 1)
|| > | t = obj.Item("parmA1", 2)
|| > You may need to define a custom collection that encapsulates one of the
|| > above for storage.
|| >
|| > --
|| > Hope this helps
|| > Jay [MVP - Outlook]
|| > ..NET Application Architect, Enthusiast, & Evangelist
|| > T.S. Bradley - http://www.tsbradley.net
|| >
|| >
|| > || > | Yes, I need to store some values in an array type collection object
| that
|| > can
|| > | hold 3 or more parameters per index. I have looked at the collection
|| > object,
|| > | hashtable object and would prefer not to hassel with a
| multi-dimensional
|| > | array. Is there such an object in VB.Net?
|| > |
|| > | Dim obj As someCollectionObj
|| > | obj.Add("parmA1", "parmA2", "parmA3")
|| > | obj.Add("parmB1", "parmB2", "parmB3")
|| > | ...
|| > |
|| > | retrieve items
|| > | s = obj.Item("parmA1", 1)
|| > | t = obj.Item("parmA1", 2)
|| > |
|| > | or, can a Hashtable contain a hashtable?
|| > |
|| > | Thanks,
|| > | Rich
|| >
|| >
|| >
|
|
 
G

Guest

------------------------------------------------------------------------------
Doh!
| Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| values() As String)
| For Each value As String In values
| Add(key, value)
| Next
| End Sub
The above code assumes that not passing any values is NOT a problem. which
may or may not be a valid assumption.

Is:

list.Add("parmA1")

Valid?

-----------------------------------------------------------------------
<<

if you have

Function Add(ByVal key As String, ByVal ParamArray args As String()) As String
Dim s, t As String
For Each s In args
t += ", " & s
Next
Return key & t
End Function

Sub x()
Dim str1 As String
str1 = Add("1", "a, b, c, d")
returns "1, a. b. c, d"

str1 = Add("1")
returns "1"

Are you suggesting this would be a problem in your sample class?



Jay B. Harlow said:
Doh!
| Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| values() As String)
| For Each value As String In values
| Add(key, value)
| Next
| End Sub
The above code assumes that not passing any values is NOT a problem. which
may or may not be a valid assumption.

Is:

list.Add("parmA1")

Valid?



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


message || Thanks for your suggestion of the NameValueCollection. May I ask if you
|| could share an example how it is used?
|
| Dim list As New System.Collections.Specialized.NameValueCollection
| list.Add("parmA1", "parmA2")
| list.Add("parmA1", "parmA3")
| list.Add("parmB1", "parmB2")
| list.Add("parmB1", "parmB3")
|
| Dim a1 As String
| Dim b1 As String
| a1 = list.Item("parmA1")
| b1 = list.Item("parmB1")
|
| Dim a1s() As String = list.Item("parmA1").Split(","c)
| Dim b1s() As String = list.Item("parmB1").Split(","c)
|
| If you like you could encapsulate some of the logic into a custom version
of
| NameValueCollection that supported adding a list of values for a key &
| returning one value for a key, something like:
|
| Public Class someCollectionObj
| Inherits System.Collections.Specialized.NameValueCollection
|
| Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| values() As String)
| For Each value As String In values
| Add(key, value)
| Next
| End Sub
|
| Default Public Overloads ReadOnly Property Item(ByVal key As
String,
| ByVal index As Integer) As String
| Get
| Dim values() As String = Item(key).Split(","c)
| Return values(index)
| End Get
| End Property
|
| Public ReadOnly Property ItemCount(ByVal key As String) As Integer
| Get
| Return Item(key).Split(","c).Length
| End Get
| End Property
|
| End Class
|
| Then you could use the above, something like:
|
| Dim list As New someCollectionObj
| list.Add("parmA1", "parmA2", "parmA3")
| list.Add("parmB1", "parmB2", "parmB3", "parmB4")
|
| For Each key As String In list.Keys
| Debug.WriteLine(Nothing, key)
| Debug.Indent()
| For index As Integer = 0 To list.ItemCount(key) - 1
| Debug.WriteLine(list.Item(key, index), "item " & index)
| Next
| Debug.Unindent()
| Next
|
| I would consider adding an indexers like:
|
| Default Public Overloads ReadOnly Property Item(ByVal key As
String)
| As String()
| Get
| Return MyBase.Item(key).Split(","c)
| End Get
| End Property
|
| Default Public Overloads ReadOnly Property Item(ByVal index As
| Integer) As String()
| Get
| Return MyBase.Item(index).Split(","c)
| End Get
| End Property
|
| Which return an array of strings for a key/index rather then the comma
| delimited string.
|
| I might also consider an indexer something like:
|
| Default Public Overloads ReadOnly Property Item(ByVal indexKey As
| Integer, ByVal indexValue As Integer) As String
| Get
| Dim values() As String = MyBase.Item(indexKey).Split(","c)
| Return values(indexValue)
| End Get
| End Property
|
| If I needed to index that way...
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| || Thanks for your suggestion of the NameValueCollection. May I ask if you
|| could share an example how it is used?
||
|| As for keys, the first element in each row is unique so that could be a
| key.
|| Else, I was just going to loop through the collection. But I tried
| writing
|| a class with the 3 properties of the stuff I need to store. But I can't
|| retrieve the values. The class effort is my 2nd post in this thread.
| Would
|| you have any suggestions what I could do to the class to make it work?
||
|| One other suggestion I got was to use a Hashtable with a structure. The
|| also seems like a good idea.
||
|| Rich
||
|| "Jay B. Harlow [MVP - Outlook]" wrote:
||
|| > Rich,
|| > | I need to store some values in an array type collection object that
| can
|| > | hold 3 or more parameters per index.
|| >
|| > It sounds like you want a collection of collections. Is the outer
| collection
|| > keyed or not keyed? Is the inner collection keyed or not keyed? In
other
|| > words is row one known by a specific Key or simply as row one?
|| >
|| > | would prefer not to hassel with a multi-dimensional
|| > | array.
|| > Rather then a 2 dimensional array, have you considered an array of
| arrays?
|| >
|| > Dim block(10,10) as String ' 2 dimensional array each row = 10,
each
|| > column is 10
|| >
|| > Dim jagged(10)() As String ' ragged/jagged array, 10 rows or 0 or
| more
|| > elements.
|| >
|| >
|| > Have you looked at the NameValueCollection?
|| >
|| >
|
http://msdn.microsoft.com/library/d...sspecializednamevaluecollectionclasstopic.asp
|| >
|| > It associates a String Key with one or more String values. If your
| values
|| > and/or keys are not strings NameValueCollection probably won't work...
|| >
|| > | retrieve items
|| > | s = obj.Item("parmA1", 1)
|| > | t = obj.Item("parmA1", 2)
|| > You may need to define a custom collection that encapsulates one of the
|| > above for storage.
|| >
|| > --
|| > Hope this helps
|| > Jay [MVP - Outlook]
|| > ..NET Application Architect, Enthusiast, & Evangelist
|| > T.S. Bradley - http://www.tsbradley.net
|| >
|| >
|| > || > | Yes, I need to store some values in an array type collection object
| that
|| > can
|| > | hold 3 or more parameters per index. I have looked at the collection
|| > object,
|| > | hashtable object and would prefer not to hassel with a
| multi-dimensional
|| > | array. Is there such an object in VB.Net?
|| > |
|| > | Dim obj As someCollectionObj
|| > | obj.Add("parmA1", "parmA2", "parmA3")
|| > | obj.Add("parmB1", "parmB2", "parmB3")
|| > | ...
|| > |
|| > | retrieve items
|| > | s = obj.Item("parmA1", 1)
|| > | t = obj.Item("parmA1", 2)
|| > |
|| > | or, can a Hashtable contain a hashtable?
|| > |
|| > | Thanks,
|| > | Rich
|| >
|| >
|| >
|
|
 
G

Guest

OK. I tried your class with these args


list.Add("parmA1", "parmA2", "parmA3")
list.Add("parmB1", "parmB2", "parmB3", "parmB4")

The object returns

parmA1:
item 0: parmA2
item 1: parmA3
parmB1:
item 0: parmB2
item 1: parmB3
item 2: parmB4

I don't see any problems.

Jay B. Harlow said:
Doh!
| Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| values() As String)
| For Each value As String In values
| Add(key, value)
| Next
| End Sub
The above code assumes that not passing any values is NOT a problem. which
may or may not be a valid assumption.

Is:

list.Add("parmA1")

Valid?



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


message || Thanks for your suggestion of the NameValueCollection. May I ask if you
|| could share an example how it is used?
|
| Dim list As New System.Collections.Specialized.NameValueCollection
| list.Add("parmA1", "parmA2")
| list.Add("parmA1", "parmA3")
| list.Add("parmB1", "parmB2")
| list.Add("parmB1", "parmB3")
|
| Dim a1 As String
| Dim b1 As String
| a1 = list.Item("parmA1")
| b1 = list.Item("parmB1")
|
| Dim a1s() As String = list.Item("parmA1").Split(","c)
| Dim b1s() As String = list.Item("parmB1").Split(","c)
|
| If you like you could encapsulate some of the logic into a custom version
of
| NameValueCollection that supported adding a list of values for a key &
| returning one value for a key, something like:
|
| Public Class someCollectionObj
| Inherits System.Collections.Specialized.NameValueCollection
|
| Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| values() As String)
| For Each value As String In values
| Add(key, value)
| Next
| End Sub
|
| Default Public Overloads ReadOnly Property Item(ByVal key As
String,
| ByVal index As Integer) As String
| Get
| Dim values() As String = Item(key).Split(","c)
| Return values(index)
| End Get
| End Property
|
| Public ReadOnly Property ItemCount(ByVal key As String) As Integer
| Get
| Return Item(key).Split(","c).Length
| End Get
| End Property
|
| End Class
|
| Then you could use the above, something like:
|
| Dim list As New someCollectionObj
| list.Add("parmA1", "parmA2", "parmA3")
| list.Add("parmB1", "parmB2", "parmB3", "parmB4")
|
| For Each key As String In list.Keys
| Debug.WriteLine(Nothing, key)
| Debug.Indent()
| For index As Integer = 0 To list.ItemCount(key) - 1
| Debug.WriteLine(list.Item(key, index), "item " & index)
| Next
| Debug.Unindent()
| Next
|
| I would consider adding an indexers like:
|
| Default Public Overloads ReadOnly Property Item(ByVal key As
String)
| As String()
| Get
| Return MyBase.Item(key).Split(","c)
| End Get
| End Property
|
| Default Public Overloads ReadOnly Property Item(ByVal index As
| Integer) As String()
| Get
| Return MyBase.Item(index).Split(","c)
| End Get
| End Property
|
| Which return an array of strings for a key/index rather then the comma
| delimited string.
|
| I might also consider an indexer something like:
|
| Default Public Overloads ReadOnly Property Item(ByVal indexKey As
| Integer, ByVal indexValue As Integer) As String
| Get
| Dim values() As String = MyBase.Item(indexKey).Split(","c)
| Return values(indexValue)
| End Get
| End Property
|
| If I needed to index that way...
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| || Thanks for your suggestion of the NameValueCollection. May I ask if you
|| could share an example how it is used?
||
|| As for keys, the first element in each row is unique so that could be a
| key.
|| Else, I was just going to loop through the collection. But I tried
| writing
|| a class with the 3 properties of the stuff I need to store. But I can't
|| retrieve the values. The class effort is my 2nd post in this thread.
| Would
|| you have any suggestions what I could do to the class to make it work?
||
|| One other suggestion I got was to use a Hashtable with a structure. The
|| also seems like a good idea.
||
|| Rich
||
|| "Jay B. Harlow [MVP - Outlook]" wrote:
||
|| > Rich,
|| > | I need to store some values in an array type collection object that
| can
|| > | hold 3 or more parameters per index.
|| >
|| > It sounds like you want a collection of collections. Is the outer
| collection
|| > keyed or not keyed? Is the inner collection keyed or not keyed? In
other
|| > words is row one known by a specific Key or simply as row one?
|| >
|| > | would prefer not to hassel with a multi-dimensional
|| > | array.
|| > Rather then a 2 dimensional array, have you considered an array of
| arrays?
|| >
|| > Dim block(10,10) as String ' 2 dimensional array each row = 10,
each
|| > column is 10
|| >
|| > Dim jagged(10)() As String ' ragged/jagged array, 10 rows or 0 or
| more
|| > elements.
|| >
|| >
|| > Have you looked at the NameValueCollection?
|| >
|| >
|
http://msdn.microsoft.com/library/d...sspecializednamevaluecollectionclasstopic.asp
|| >
|| > It associates a String Key with one or more String values. If your
| values
|| > and/or keys are not strings NameValueCollection probably won't work...
|| >
|| > | retrieve items
|| > | s = obj.Item("parmA1", 1)
|| > | t = obj.Item("parmA1", 2)
|| > You may need to define a custom collection that encapsulates one of the
|| > above for storage.
|| >
|| > --
|| > Hope this helps
|| > Jay [MVP - Outlook]
|| > ..NET Application Architect, Enthusiast, & Evangelist
|| > T.S. Bradley - http://www.tsbradley.net
|| >
|| >
|| > || > | Yes, I need to store some values in an array type collection object
| that
|| > can
|| > | hold 3 or more parameters per index. I have looked at the collection
|| > object,
|| > | hashtable object and would prefer not to hassel with a
| multi-dimensional
|| > | array. Is there such an object in VB.Net?
|| > |
|| > | Dim obj As someCollectionObj
|| > | obj.Add("parmA1", "parmA2", "parmA3")
|| > | obj.Add("parmB1", "parmB2", "parmB3")
|| > | ...
|| > |
|| > | retrieve items
|| > | s = obj.Item("parmA1", 1)
|| > | t = obj.Item("parmA1", 2)
|| > |
|| > | or, can a Hashtable contain a hashtable?
|| > |
|| > | Thanks,
|| > | Rich
|| >
|| >
|| >
|
|
 
J

Jay B. Harlow [MVP - Outlook]

But if I have:

| list.Add("parmA1", "parmA2", "parmA3")
| list.Add("parmB1", "parmB2", "parmB3", "parmB4")
list.Add("parmC1")

The object returns:

| parmA1:
| item 0: parmA2
| item 1: parmA3
| parmB1:
| item 0: parmB2
| item 1: parmB3
| item 2: parmB4

Notice there is no output for parmC1, as parmC1 has no values, so there is
nothing to return. parmC1 is NOT placed into the collection.

Is no output what you would expect for parmC1? If not, what is the expected
output?

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| OK. I tried your class with these args
|
|
| list.Add("parmA1", "parmA2", "parmA3")
| list.Add("parmB1", "parmB2", "parmB3", "parmB4")
|
| The object returns
|
| parmA1:
| item 0: parmA2
| item 1: parmA3
| parmB1:
| item 0: parmB2
| item 1: parmB3
| item 2: parmB4
|
| I don't see any problems.
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > Doh!
| > | Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| > | values() As String)
| > | For Each value As String In values
| > | Add(key, value)
| > | Next
| > | End Sub
| > The above code assumes that not passing any values is NOT a problem.
which
| > may or may not be a valid assumption.
| >
| > Is:
| >
| > list.Add("parmA1")
| >
| > Valid?
| >
| >
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > ..NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > message | > || Thanks for your suggestion of the NameValueCollection. May I ask if
you
| > || could share an example how it is used?
| > |
| > | Dim list As New
System.Collections.Specialized.NameValueCollection
| > | list.Add("parmA1", "parmA2")
| > | list.Add("parmA1", "parmA3")
| > | list.Add("parmB1", "parmB2")
| > | list.Add("parmB1", "parmB3")
| > |
| > | Dim a1 As String
| > | Dim b1 As String
| > | a1 = list.Item("parmA1")
| > | b1 = list.Item("parmB1")
| > |
| > | Dim a1s() As String = list.Item("parmA1").Split(","c)
| > | Dim b1s() As String = list.Item("parmB1").Split(","c)
| > |
| > | If you like you could encapsulate some of the logic into a custom
version
| > of
| > | NameValueCollection that supported adding a list of values for a key &
| > | returning one value for a key, something like:
| > |
| > | Public Class someCollectionObj
| > | Inherits System.Collections.Specialized.NameValueCollection
| > |
| > | Public Overloads Sub Add(ByVal key As String, ByVal ParamArray
| > | values() As String)
| > | For Each value As String In values
| > | Add(key, value)
| > | Next
| > | End Sub
| > |
| > | Default Public Overloads ReadOnly Property Item(ByVal key As
| > String,
| > | ByVal index As Integer) As String
| > | Get
| > | Dim values() As String = Item(key).Split(","c)
| > | Return values(index)
| > | End Get
| > | End Property
| > |
| > | Public ReadOnly Property ItemCount(ByVal key As String) As
Integer
| > | Get
| > | Return Item(key).Split(","c).Length
| > | End Get
| > | End Property
| > |
| > | End Class
| > |
| > | Then you could use the above, something like:
| > |
| > | Dim list As New someCollectionObj
| > | list.Add("parmA1", "parmA2", "parmA3")
| > | list.Add("parmB1", "parmB2", "parmB3", "parmB4")
| > |
| > | For Each key As String In list.Keys
| > | Debug.WriteLine(Nothing, key)
| > | Debug.Indent()
| > | For index As Integer = 0 To list.ItemCount(key) - 1
| > | Debug.WriteLine(list.Item(key, index), "item " & index)
| > | Next
| > | Debug.Unindent()
| > | Next
| > |
| > | I would consider adding an indexers like:
| > |
| > | Default Public Overloads ReadOnly Property Item(ByVal key As
| > String)
| > | As String()
| > | Get
| > | Return MyBase.Item(key).Split(","c)
| > | End Get
| > | End Property
| > |
| > | Default Public Overloads ReadOnly Property Item(ByVal index As
| > | Integer) As String()
| > | Get
| > | Return MyBase.Item(index).Split(","c)
| > | End Get
| > | End Property
| > |
| > | Which return an array of strings for a key/index rather then the comma
| > | delimited string.
| > |
| > | I might also consider an indexer something like:
| > |
| > | Default Public Overloads ReadOnly Property Item(ByVal indexKey
As
| > | Integer, ByVal indexValue As Integer) As String
| > | Get
| > | Dim values() As String =
MyBase.Item(indexKey).Split(","c)
| > | Return values(indexValue)
| > | End Get
| > | End Property
| > |
| > | If I needed to index that way...
| > |
| > | --
| > | Hope this helps
| > | Jay [MVP - Outlook]
| > | .NET Application Architect, Enthusiast, & Evangelist
| > | T.S. Bradley - http://www.tsbradley.net
| > |
| > |
| > | | > || Thanks for your suggestion of the NameValueCollection. May I ask if
you
| > || could share an example how it is used?
| > ||
| > || As for keys, the first element in each row is unique so that could be
a
| > | key.
| > || Else, I was just going to loop through the collection. But I tried
| > | writing
| > || a class with the 3 properties of the stuff I need to store. But I
can't
| > || retrieve the values. The class effort is my 2nd post in this thread.
| > | Would
| > || you have any suggestions what I could do to the class to make it
work?
| > ||
| > || One other suggestion I got was to use a Hashtable with a structure.
The
| > || also seems like a good idea.
| > ||
| > || Rich
| > ||
| > || "Jay B. Harlow [MVP - Outlook]" wrote:
| > ||
| > || > Rich,
| > || > | I need to store some values in an array type collection object
that
| > | can
| > || > | hold 3 or more parameters per index.
| > || >
| > || > It sounds like you want a collection of collections. Is the outer
| > | collection
| > || > keyed or not keyed? Is the inner collection keyed or not keyed? In
| > other
| > || > words is row one known by a specific Key or simply as row one?
| > || >
| > || > | would prefer not to hassel with a multi-dimensional
| > || > | array.
| > || > Rather then a 2 dimensional array, have you considered an array of
| > | arrays?
| > || >
| > || > Dim block(10,10) as String ' 2 dimensional array each row = 10,
| > each
| > || > column is 10
| > || >
| > || > Dim jagged(10)() As String ' ragged/jagged array, 10 rows or 0
or
| > | more
| > || > elements.
| > || >
| > || >
| > || > Have you looked at the NameValueCollection?
| > || >
| > || >
| > |
| >
http://msdn.microsoft.com/library/d...sspecializednamevaluecollectionclasstopic.asp
| > || >
| > || > It associates a String Key with one or more String values. If your
| > | values
| > || > and/or keys are not strings NameValueCollection probably won't
work...
| > || >
| > || > | retrieve items
| > || > | s = obj.Item("parmA1", 1)
| > || > | t = obj.Item("parmA1", 2)
| > || > You may need to define a custom collection that encapsulates one of
the
| > || > above for storage.
| > || >
| > || > --
| > || > Hope this helps
| > || > Jay [MVP - Outlook]
| > || > ..NET Application Architect, Enthusiast, & Evangelist
| > || > T.S. Bradley - http://www.tsbradley.net
| > || >
| > || >
| > || > | > || > | Yes, I need to store some values in an array type collection
object
| > | that
| > || > can
| > || > | hold 3 or more parameters per index. I have looked at the
collection
| > || > object,
| > || > | hashtable object and would prefer not to hassel with a
| > | multi-dimensional
| > || > | array. Is there such an object in VB.Net?
| > || > |
| > || > | Dim obj As someCollectionObj
| > || > | obj.Add("parmA1", "parmA2", "parmA3")
| > || > | obj.Add("parmB1", "parmB2", "parmB3")
| > || > | ...
| > || > |
| > || > | retrieve items
| > || > | s = obj.Item("parmA1", 1)
| > || > | t = obj.Item("parmA1", 2)
| > || > |
| > || > | or, can a Hashtable contain a hashtable?
| > || > |
| > || > | Thanks,
| > || > | Rich
| > || >
| > || >
| > || >
| > |
| > |
| >
| >
| >
 

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