Array 2 dimensions to collection or dictionary
Hi
Is possible to transform a array 2 dimension to a collection or dictionary and vice versa (collection to array)
Code:
Private Sub Form_Load()
Dim myArray() As Variant
ReDim myArray(2, 3)
myArray(0, 0) = "title 01"
myArray(0, 1) = 10
myArray(0, 2) = 20
myArray(0, 3) = 30
myArray(1, 0) = "title 02"
myArray(1, 1) = 20
myArray(1, 2) = 40
myArray(1, 3) = 60
myArray(2, 0) = "title 03"
myArray(2, 1) = 30
myArray(2, 2) = 60
myArray(2, 3) = 80
End Sub
Re: Array 2 dimensions to collection or dictionary
Before I just give a sample of what could be possible, what will be the purpose of storing a matrix in a collection?
What do you want to do with the collection?
Re: Array 2 dimensions to collection or dictionary
2 ways:
1) Create a class with Fields for your Array-Members, transfer the content of your array to the class-instance(s), and then add the class-instance(s) to your collection/dictionary
2) Concatenate (Join-Function) your array-Members in to a single string with a delimiter, and add that string to your collection/dictionary
Re: Array 2 dimensions to collection or dictionary
Hi
although you need the two (collection to array and array to collection), I need more of the first because of a recordset that will fill an array and then a grid, but the first column each line has a name, then the recordset would fetch data like this
Title1 , value 01, value02, value03
XPTO , value04, value05, value06
Title2, value7, value8, value09
ABCD, value10, value11, null
...etc
Then using FIrst column, I update collection values, after would migrate to array and populate grid
Re: Array 2 dimensions to collection or dictionary
Quote:
Originally Posted by
Zvoni
2 ways:
1) Create a class with Fields for your Array-Members, transfer the content of your array to the class-instance(s), and then add the class-instance(s) to your collection/dictionary
2) Concatenate (Join-Function) your array-Members in to a single string with a delimiter, and add that string to your collection/dictionary
I have no idea how to do this :blush:
Re: Array 2 dimensions to collection or dictionary
Easiest way ... store a collection in each item of a parent collection. No biggie. I'll put together an example in a moment.
You could nest this as deeply as you liked to have as many "dimensions" as you want.
Re: Array 2 dimensions to collection or dictionary
Code:
Option Explicit
Dim collParent As New Collection
Private Sub Form_Load()
Dim collChild As Collection
Dim i As Long
Dim j As Long
Dim s As String
Dim vParent As Variant
Dim vChild As Variant
' Let's put five new child collections in our parent collection.
For i = 1 To 5
Set collChild = New Collection
collParent.Add collChild, CStr(i)
Next i
Set collChild = Nothing ' Just a touch of clean-up. End Sub would do the same thing.
' Now, let's add three items to each of the child collections.
For i = 1 To 5
For j = 1 To 3
collParent.Item(CStr(i)).Add CStr(Int(Rnd * 5000 + 1000))
Next
Next
' And finally, let's dump it and see what we have.
i = 0
For Each vParent In collParent
i = i + 1
Debug.Print "Parent #"; Format$(i)
For Each vChild In vParent
Debug.Print " "; vChild
Next
Next
End Sub
There ya go,
Elroy
EDIT1: And one of the nice things about this approach is that each child collection doesn't have to have the same number of items, whereas a child dimension (aka, second dimension) of an array must always have the same number of items. If you wish for this to be a criteria in your nested collections, it would be something that you would impose with your code.
EDIT2: Just as a note, everything we built survives the Form_Load event, and would be available so long as the COM/Code object of Form1 was instantiated. And it has a module level scope within Form1.
Re: Array 2 dimensions to collection or dictionary
Without a statement of the problem all we can do is fling possible solutions into the air for you to blast away at with a shotgun.
In a lot of cases you could just use a disconnected Recordset and be done with it. It isn't clear why there is an array involved here at all.
If you provide more information on how you will use this we might be able to offer better answers.
Re: Array 2 dimensions to collection or dictionary
Quote:
Originally Posted by
dilettante
In a lot of cases you could just use a disconnected Recordset and be done with it. It isn't clear why there is an array involved here at all.
Agreed. In post #4 Mutley says he is already using a database/recordset. Not enough details to know why that database can't be used to populate a grid and/or be updated, both without need for an array.
Re: Array 2 dimensions to collection or dictionary
Quote:
Then using FIrst column, I update collection values, after would migrate to array and populate grid
Lost me too, from input (whether it be a CSV or DB table) to grid doesn't need all those steps.
Plenty of samples on the forum how to populate a datagrid of msflexgrid from a recordset or a text file.
Re: Array 2 dimensions to collection or dictionary
Truth be told, from other posts, it sounds like he's trying to write an Excel-like cell/row/col formatting ability in VB6. And that would be quite an undertaking. Excel uses LOTS of tricks to get that done, such as a single property setting for a whole row, whole column, or some range of cells, in addition to property settings possibly for individual cells. And then, you've got the whole ZOrder problem when you have crossing properties, such as one property setting for a row, and another for a column. How is the intersection handled?
However, all I did in post #7 was attempt to answer the question posed in the OP. It seemed like an easy answer to me. I'm not sure how it would be applied to the Excel-like formatting problem, but I do believe it answers the posed question.
Y'all Take Care,
Elroy