|
-
Jan 29th, 2019, 09:19 AM
#1
Thread Starter
Fanatic Member
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
-
Jan 29th, 2019, 09:27 AM
#2
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?
-
Jan 29th, 2019, 09:35 AM
#3
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
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jan 29th, 2019, 09:39 AM
#4
Thread Starter
Fanatic Member
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
-
Jan 29th, 2019, 09:41 AM
#5
Thread Starter
Fanatic Member
Re: Array 2 dimensions to collection or dictionary
 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
-
Jan 29th, 2019, 02:12 PM
#6
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.
Last edited by Elroy; Jan 29th, 2019 at 02:55 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 29th, 2019, 02:22 PM
#7
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.
Last edited by Elroy; Jan 29th, 2019 at 02:49 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 29th, 2019, 10:24 PM
#8
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.
-
Jan 29th, 2019, 10:54 PM
#9
Re: Array 2 dimensions to collection or dictionary
 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.
Last edited by LaVolpe; Jan 29th, 2019 at 11:03 PM.
Reason: changed post 5 to post 4
-
Jan 30th, 2019, 02:10 AM
#10
Re: Array 2 dimensions to collection or dictionary
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.
-
Jan 30th, 2019, 12:31 PM
#11
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
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|