-
Jun 10th, 2021, 12:52 PM
#1
Thread Starter
Member
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 ...
-
Jun 10th, 2021, 12:58 PM
#2
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.
-
Jun 11th, 2021, 04:50 AM
#3
Thread Starter
Member
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)
-
Jun 11th, 2021, 05:02 AM
#4
Thread Starter
Member
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
...
-
Jun 11th, 2021, 05:13 AM
#5
Thread Starter
Member
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!
-
Jun 11th, 2021, 05:13 AM
#6
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
-
Jun 11th, 2021, 05:17 AM
#7
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
-
Jun 11th, 2021, 05:22 AM
#8
Thread Starter
Member
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 !?)
-
Jun 11th, 2021, 05:26 AM
#9
Re: Explanation of bi-dimensional Arrays ... Please!
The part within the () when dealing with an array is the index. This must be a number.
-
Jun 11th, 2021, 05:30 AM
#10
Thread Starter
Member
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 !!
-
Jun 11th, 2021, 05:36 AM
#11
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
-
Jun 11th, 2021, 05:38 AM
#12
Thread Starter
Member
Re: Explanation of bi-dimensional Arrays ... Please!
-
Jun 11th, 2021, 05:49 AM
#13
Thread Starter
Member
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") = ...
-
Jun 11th, 2021, 05:54 AM
#14
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.
-
Jun 11th, 2021, 05:57 AM
#15
Thread Starter
Member
Re: Explanation of bi-dimensional Arrays ... Please!
Ok!
Ok!
Ok!
...
Any 'suggestions' about it !?
-
Jun 11th, 2021, 06:35 AM
#16
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.
-
Jun 11th, 2021, 07:03 AM
#17
Thread Starter
Member
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.
-
Jun 11th, 2021, 07:07 AM
#18
Thread Starter
Member
Re: Explanation of bi-dimensional Arrays ... Please!
-
Jun 11th, 2021, 11:07 AM
#19
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
-
Jun 11th, 2021, 12:45 PM
#20
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
-
Jun 11th, 2021, 09:19 PM
#21
Re: Explanation of bi-dimensional Arrays ... Please!
I think he could need just a collection.
And use X & "." & Y as the key.
-
Jun 14th, 2021, 06:56 AM
#22
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|