Results 1 to 12 of 12

Thread: [RESOLVED] Need help to redim my array

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [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:
    1. ReDim Preserve arMixedHolder(tCnt, LINES, 21)

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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

  3. #3

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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?

  4. #4

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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

  5. #5
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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

  7. #7

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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:
    1. (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:
    1. For A As Integer = 1 To MixedTable
    2.             Dim iCnt As Integer = arNumOfLines(A, 1)
    3.             For B As Integer = 1 To iCnt
    4.                 Using connection As New MySql.Data.MySqlClient.MySqlConnection("Server=" & GEN_ServerIP & ";Port=3306;Database=ab;Uid=root;Pwd=12se1")
    5.                     Using command As New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO cb_trans_mixed (DOC_NO, DOC_TYPE, DOC_DATE, DOC, TOC, PD, YR, USER, " & _
    6.                                                                          "ACC_NO, LINE, CATEGORY, STK_CODE, ESTATE, CULTIVAR, VOLUME, PACK, QTY, LABELS," & _
    7.                                                                          " SOURCE, VALUE, COMMENTS) VALUES('" & strDOC_NO & "','" & arMixedHolder(A, B, 2) & _
    8.                                                                          "','" & arMixedHolder(A, B, 3) & "','" & arMixedHolder(A, B, 4) & "','" & arMixedHolder(A, B, 5) & _
    9.                                                                          "','" & arMixedHolder(A, B, 6) & "','" & arMixedHolder(A, B, 7) & "','" & arMixedHolder(A, B, 8) & _
    10.                                                                          "','" & arMixedHolder(A, B, 9) & "','" & arMixedHolder(A, B, 10) & "','" & arMixedHolder(A, B, 11) & _
    11.                                                                          "','" & arMixedHolder(A, B, 12) & "','" & arMixedHolder(A, B, 13) & "','" & arMixedHolder(A, B, 14) & _
    12.                                                                          "','" & arMixedHolder(A, B, 15) & "','" & arMixedHolder(A, B, 16) & "','" & arMixedHolder(A, B, 17) & _
    13.                                                                          "','" & arMixedHolder(A, B, 18) & "','" & arMixedHolder(A, B, 19) & "','" & arMixedHolder(A, B, 20) & _
    14.                                                                          "','" & arMixedHolder(A, B, 21) & "')", connection)
    15.                         command.Parameters.AddWithValue("MyParam", 1)
    16.                         connection.Open()
    17.                         command.ExecuteScalar()
    18.                     End Using
    19.                 End Using
    20.             Next
    21.         Next

  8. #8

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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?

  9. #9

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help to redim my array

    Why can't you do what's already been suggested?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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.

  12. #12
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Need help to redim my array

    Quote Originally Posted by tgf-47 View Post
    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)

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
  •  



Click Here to Expand Forum to Full Width