Results 1 to 22 of 22

Thread: Explanation of bi-dimensional Arrays ... Please!

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Explanation of bi-dimensional Arrays ... Please!

    It is possible to create a two-dimensional array variable, without declaring the number of the two variables to be used, i.e .(?):


    Private AA As Long
    Private BB As Long

    Private CC As Long

    Private double(5000, 5000) As Long
    Private single(5000) As Long


    but I want:


    Private double(AA, BB) As Long
    Private single(CC) As Long


    Visual Basic ask Constant ...

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Explanation of bi-dimensional Arrays ... Please!

    You need a dynamic array

    Code:
    Dim MyArray() as long
    ReDim MyArray(MySize)
    do not try to use single and double as array names, both are reserved.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Hello

    Thanks for the answer, but they were just sample names, to explain how I wanted them.

    Anyway, as you say, it doesn't give me the possibility to insert two variables (am I wrong !?).


    I wish I could do it like this:

    Public / Private variable (X, Y) as (something, preferably Long)

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Is this wrong!? (yes!)

    ------------------------------
    Private variable () As Long
    Private val_temp () As Long
    Private var_String () As String
    Dim X as Long
    Dim Y as Long
    Dim Z as String

    X = 1
    Y = Y + 1

    Z = X & "." & Y

    variable (X, Y) = variable (X, Y) + 1
    val_temp = variable (X, Y)
    var_String (Z) = var_String (Z) + 1

    ...

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Quite right!!!
    You were right, sorry !!

    I hadn't considered the ReDim

    By reworking everything (reorganizing the variables I needed) I solved!

    Thank you!


  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Explanation of bi-dimensional Arrays ... Please!

    You can have dynamic arrays.
    And you can change the last dimension of the dynamic array while preserving the content.

    You can not access arrays with string variables.
    Code:
    Dim lArray() As Long
    Dim i As Long
    
    ReDim lArray(9) ' an array with an index of 0..9
    For i = 0 To 9
      lArray(i) = i
    Next i
    
    ReDim Preserve lArray(99) ' increase the size to index 99 and will preserve the first 10 items
    
    For i = 0 To 9
      Debug.Print lArray(i)
    Next i
    
    ReDim lArray(3, 4) ' creates a 2 dimensional array 0..3, 0..4
    For i = 0 To 4
      lArray(0, i) = i
    Next i
    
    ReDim Preserve lArray(3, 6) ' increases the last dimension, keeping the original values

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,416

    Re: Explanation of bi-dimensional Arrays ... Please!

    As Arnout wrote: Once a dynamic 2D-Array (or more Dimensions) has been "ReDim"-ed you can only change the last Dimension in subsequent ReDim (Preserve)
    If you want/need a clean slate, you need to erase (hint hint) the Array first (and you lose its content)
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Last question:

    Is it possible to assign (/ associate) an array to a String variable?

    Or:

    Variable ("Name") = Variable ("Name") + 1
    I assign a number to the array variable named NAME!?


    (Am I saying something absurd !?)

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Explanation of bi-dimensional Arrays ... Please!

    The part within the () when dealing with an array is the index. This must be a number.

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    I explain why I asked this question:


    as you see in my example:

    Z = X & "." & Y

    Z is a string variable, made up of X and Y, separated by a period.

    But if I want to create another Array as a function of Z

    Variable (Z) =?

    VB gives me:

    A = 1
    B = 2
    Z = should be "1.2" ...


    But with my (wrong) calculations, I get Z = 12 ...

    And it doesn't work !!

  11. #11
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,416

    Re: Explanation of bi-dimensional Arrays ... Please!

    I haven't understood a single word of it
    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

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Zvoni ...


    Ahahahaha!

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Therefore:

    I have the X Value
    and the Y value

    Now, rightly so, if I create an Array that has values ​​(X, and Y), I get the variable result, for each position of X and Y.
    (example:
    var_of (X = 1, Y = 3) = 8

    And this value, I need for one thing.

    Now I would like to assign a 'further' value, depending on the association of the two variables, that is:

    X = 1
    Y = 3
    Z = X & "." & Y ("1.3")

    And at this point, I would need an Array that is a function of this result:


    Array_of_ (Z) = ...

    (in this case:
    Array_of _ ("1.3") = ...

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Explanation of bi-dimensional Arrays ... Please!

    The index can not be a string. It must be a whole number. so "1.3" is not valid 1.3 also is not valid.

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Ok!
    Ok!
    Ok!


    ...

    Any 'suggestions' about it !?

  16. #16
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Explanation of bi-dimensional Arrays ... Please!

    Is this hypothetical or do you have a special situation where you on the fly constantly need to alter the number of dimensions of your array?
    If you really need string indices then you can also use a dictionary or a hashtable.
    These objects don't care what you use as the key, as long it's a string.

  17. #17

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    Arnoutdv...


    Yes, I have a condition in which what has been described 'must happen to me'; it is not hypothetical!


    If you really need string indices then you can also use a dictionary or a hashtable.

    What you have suggested to me, actually, I don't know what it is.

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!


  19. #19
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Explanation of bi-dimensional Arrays ... Please!

    Not sure, but perhaps the OP wants to use Z as a kind of "LookupTable"?
    (to speedup string-concats or something)....

    Code:
    Option Explicit
    
    Private Z() As String 'make Z reachable at Form(Class)-level
    
    Private Sub Form_Load()
      ReDim Z(1 To 2, 0 To 1) 'define Z as a 2D LUT for String-content
    
      Dim x&, y& 'init (loop over) the defined dimensional bounds, to fill the LUT
      For x = LBound(Z, 1) To UBound(Z, 1)
          For y = LBound(Z, 2) To UBound(Z, 2)
              Z(x, y) = x & "." & y
          Next y
      Next x
      
      'after Z was filled, we can now access the LUTs content really fast
      Debug.Print Z(1, 0), Z(1, 1)
      Debug.Print Z(2, 0), Z(2, 1)
    End Sub
    Olaf

  20. #20
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,416

    Re: Explanation of bi-dimensional Arrays ... Please!

    This reminds me more of a class with a standard-property, where the Index-Argument is a variant.
    pass an integer, and it works like an index of an array
    pass a string it behaves like a dictionary with key-value-pair

    think recordsets:
    you can access a recordset-column passing its ordinal - MyRS(0)
    you can access a recordset-column passing its name - MyRS(„ID“)
    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

  21. #21
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Explanation of bi-dimensional Arrays ... Please!

    I think he could need just a collection.
    And use X & "." & Y as the key.

  22. #22

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    59

    Re: Explanation of bi-dimensional Arrays ... Please!

    First of all: thanks again for the answers !!

    Sorry for the delay in replying.

    I'm not very used to the method you recommended, but I'm 'studying' !!!

    Until next time!

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