|
-
May 13th, 2010, 08:57 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Need help to redim my array
I have the following array in my project that I'm trying to redim without losing the values.
The problem with redim preserve is I can only redim the last dimension of the array. I want to redim the 2nd dimension without losing data. How do I get this to work?
1 Code:
ReDim Preserve arMixedHolder(tCnt, LINES, 21)
-
May 13th, 2010, 09:22 AM
#2
Re: Need help to redim my array
You can't.
As you have already found out, Redim only works on the final dimension. The only solution is to create a new array of the new size and copy everything across, which can be done with nested loops, but obviously it isn't ideal.
On the other hand, there is rarely a situation where a multidimensional array is the best solution. In virtually every case where a multidimensional array can be used, a class can be created to hold the information and a List of that class can be used to hold a collection of those classes. That will generally be more efficient than any multidimensional array that has to be resized, but in some 2D cases, such as grids, it won't be as conceptually simple as a 2D array. Without knowing what you are actually doing, it is impossible to say more than that.
My usual boring signature: Nothing
 
-
May 13th, 2010, 09:34 AM
#3
Thread Starter
Addicted Member
Re: Need help to redim my array
I'm populating data in one datagrid and as soom as the keyword "MIXED" it entered as a value in a cell, it pops up a new datagridviewer where other data is captured.
Now as for the array (a, b, c)
a <-- This is the number of Mixed Columns (One datagrid2 for each)
b <-- This is the number of lines in each of those mixed columns
c <-- This holds the values of the cells.
I use this method for security reasons(Not the array, but holding all the values first)
Then after all is captured the data from the 1st grid is inserted to its datatable, same for the data of the mixed table now in the array.
Will a class work in this scenario?
-
May 13th, 2010, 09:39 AM
#4
Thread Starter
Addicted Member
Re: Need help to redim my array
Can you perhaps give me a code snipped of a class that can hold 3 sets of multiple values?
I'd like to check it out
-
May 13th, 2010, 10:00 AM
#5
Re: Need help to redim my array
I think you'll have to be alot clearer as to the purpose of your datagrid, what kind of information it is storing, etc.
-
May 13th, 2010, 11:20 AM
#6
Re: Need help to redim my array
It would be a class within a class arrangement to cover that.
Suppose you have a class that was MixedColumn which held a List of values. The list count would be the number of lines, which each individual element being one value. The MixedColumn class would need to expose either the inner list via a read only property, or methods to allow for additions to that list, which you would want to do if there was any validation being performed on the list.
Therefore, a List (of MixedColumn) would be the outer object. Whenever you got one of your "mixed" values, you would create a new MixedColumn and add it to the list. The MixedColumn could have identifiers associated with it as member variables, if needed, but in its constructor, it would at least initialize the list of values.
Working with lists means no Redim on anything.
I'll leave it at that point, for now, as I may have misunderstood your description and left out a level. The rough outline would be this:
Code:
Private OuterList As List(of MixedColumn)
Public Class MixedColumn
Private valList as New List(of Integer) 'Or whatever
Public ReadOnly Property MyList() As List(of Integer)
Get
Return valList
End Get
End Property
End Class
End Class
My usual boring signature: Nothing
 
-
May 14th, 2010, 02:03 AM
#7
Thread Starter
Addicted Member
Re: Need help to redim my array
To clarify:
I have a form with a datagridviewer where I capture incoming freights. In this case it will be boxes.
Most of the boxes are filled with the same product, so this will be captured on the main form like normal.
As soon as the operator specifies that there is a box with mixed (Different) products inside, that all the cells in that line on the datagrid gets set the value "MIXED". Then Form2 pops up with datagridviewer2.
What happens on form load is: The first value of my array ( A, B, C ) gets set to "1" as this is the first mixed box in this order. Now I fill out datagridviewer2 with all the products in the box. In the 2nd value of the array ( A , B, C ) the value of the total lines used is stored.
vb Code:
(datagridviewer2.linecount) -1
then in each of the lines all the values of the cells are stored in the 3rd value of the array.
After all is done on the main form, the save begins for the main form and then I use a for loop to save all the data in the array to its correct place.
I dont know if you are going to understand this, but I'm going to post this loop so you can get a better idea of what i am doing.
vb Code:
For A As Integer = 1 To MixedTable Dim iCnt As Integer = arNumOfLines(A, 1) For B As Integer = 1 To iCnt Using connection As New MySql.Data.MySqlClient.MySqlConnection("Server=" & GEN_ServerIP & ";Port=3306;Database=ab;Uid=root;Pwd=12se1") Using command As New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO cb_trans_mixed (DOC_NO, DOC_TYPE, DOC_DATE, DOC, TOC, PD, YR, USER, " & _ "ACC_NO, LINE, CATEGORY, STK_CODE, ESTATE, CULTIVAR, VOLUME, PACK, QTY, LABELS," & _ " SOURCE, VALUE, COMMENTS) VALUES('" & strDOC_NO & "','" & arMixedHolder(A, B, 2) & _ "','" & arMixedHolder(A, B, 3) & "','" & arMixedHolder(A, B, 4) & "','" & arMixedHolder(A, B, 5) & _ "','" & arMixedHolder(A, B, 6) & "','" & arMixedHolder(A, B, 7) & "','" & arMixedHolder(A, B, 8) & _ "','" & arMixedHolder(A, B, 9) & "','" & arMixedHolder(A, B, 10) & "','" & arMixedHolder(A, B, 11) & _ "','" & arMixedHolder(A, B, 12) & "','" & arMixedHolder(A, B, 13) & "','" & arMixedHolder(A, B, 14) & _ "','" & arMixedHolder(A, B, 15) & "','" & arMixedHolder(A, B, 16) & "','" & arMixedHolder(A, B, 17) & _ "','" & arMixedHolder(A, B, 18) & "','" & arMixedHolder(A, B, 19) & "','" & arMixedHolder(A, B, 20) & _ "','" & arMixedHolder(A, B, 21) & "')", connection) command.Parameters.AddWithValue("MyParam", 1) connection.Open() command.ExecuteScalar() End Using End Using Next Next
-
May 14th, 2010, 02:16 AM
#8
Thread Starter
Addicted Member
Re: Need help to redim my array
Can I get this List to hold 3 sets of values that is directly related to each other like in the array?
If yes, how?
-
May 14th, 2010, 02:22 AM
#9
Thread Starter
Addicted Member
Re: Need help to redim my array
Wouldn't it be better to just create a datatable and dataset and just store the values in there for the time being?
-
May 14th, 2010, 02:31 AM
#10
Re: Need help to redim my array
Why can't you do what's already been suggested?
-
May 14th, 2010, 02:34 AM
#11
Thread Starter
Addicted Member
Re: Need help to redim my array
I'm not familiar with what has been suggested. If you could show me how to make a list that holds 3 sets of related values like in my array, i would be over the moon, because it does sound like the better option.
-
May 14th, 2010, 04:40 AM
#12
Re: Need help to redim my array
 Originally Posted by tgf-47
I'm populating data in one datagrid and as soom as the keyword "MIXED" it entered as a value in a cell, it pops up a new datagridviewer where other data is captured.
Now as for the array (a, b, c)
a <-- This is the number of Mixed Columns (One datagrid2 for each)
b <-- This is the number of lines in each of those mixed columns
c <-- This holds the values of the cells.
I use this method for security reasons(Not the array, but holding all the values first)
Then after all is captured the data from the 1st grid is inserted to its datatable, same for the data of the mixed table now in the array.
Will a class work in this scenario?
Well, if c is a value then what is stored in the array elements?
Say I have 2 columns with 2 cells in each then I would only need 2 dimensions:
array(2,2) = value
You can use nested lists for this, I think
Code:
Dim store As New List(Of List(Of Integer))
And generally - 2d array is a 'virtual' structure. Multi-dimensional arrays are represented in memory as 1d arrays anyway, several dimensions is simply added for more easier indexing.
Example:
Code:
This 2d array:
5 7
2 13
where element(1,1) = 13 is represented in memory like:
5 7 2 13
and thus using 1D notation you can use index = 3 instead of 1,1
You can store your data in a 1D array and if you need to index it using 3 elements simply use a helper function for that:
Code:
Function GetIndex(a,b) As Integer
' Convert a,b into an integer index of 1D array here
Return A * 1stDimensionSize + b
End Function
Or, you can use a dictionary using string keys like:
"1;6"
Or you can even use a class for "key":
Code:
Class key
Public a ' 1D
Public b ' 2D
End Class
Dim store As New Dictionary(Of Key, Integer)
Last edited by cicatrix; May 14th, 2010 at 04:44 AM.
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
|