MultiDimensional Array Syntax

J

Jack Leach

Hello, TIA

I'm trying to use my first two-dim array, and seem to be having some trouble
with the syntax.

My goal is to be able to store a list of X and Y dimensions in an array for
a group of points. For example, if I have a 4-point group centered around X0
Y0, with a radius of 1, starting at an angle of 0, I would have the following
four points:

1,0
0,1
-1,0
0,-1

I'm assuming this is what a two-dimensional array is used for, but am not
quite sure how to use it. Declaration of the array? How to set only the
"left" or "right" part of the array? Or am I completely wrong in my
assumtion that this is what we should be using a multidimensional array for?
I've just recently came across a term called a "jagged" array, which
apparently is an array whos elements are also arrays? Perhaps this would be
more apt? ex...

Array1() has two elements: 1,0
Array2() has two elements: 0,1
Array3() has two elements: -1,0
Array4() has two elemants: 0,-1
ArrayParent() has 4 elements, Arrays 1,2,3 and 4?


For those wondering, my ultimate goal here is to come up with some CAM
functions that I can use. Many operations (drilling a hole per say) are done
in repetition. I would like to store the point lists in an array so I can
loop the array and generate the required command at each point. I realize
that I can do this with a delimited list in a string, but would rather avoid
the typecasting and parsing that would be involved.

Many thanks to anyone who can give me some guidance.

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
J

Jack Leach

Nevermind, I got it...


Dim x(3,1) as Variant

x(0,0) = 0
x(0,1) = 1
x(1,0) = 1
x(1,1) = 0
x(2,0) = -1
x(2,1) = 0
x(3,0) = 0
x(3, 1) = -1


sorry about that... stupid question, finally found it on my own

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
T

Tony Toews [MVP]

Jack Leach said:
Dim x(3,1) as Variant

x(0,0) = 0
x(0,1) = 1
x(1,0) = 1
x(1,1) = 0
x(2,0) = -1
x(2,1) = 0
x(3,0) = 0
x(3, 1) = -1


sorry about that... stupid question, finally found it on my own

No such thing as a stupid question. Now note that you can Redim
Preserve the last dimension of that array dynamically. So say the
user decided to have 8 points. You would exchange the two dimensions
in your array.

x(0,0) = 0
x(1,0) = 1
x(0,1) = 1
x(1,1) = 0
x(0,2) = -1
x(1,2) = 0
.....
x(0,8) = 0
x(1,8) = -1

You can use Ubound to determine the number of elements in the last
dimension or use a global variable. You can also dimension the array
starting from 1 or 18 any other number rather than 0.

Also if you don't need to use a variant I'd use the appropriate data
type, ie long or single or whatever.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a free, convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/
Granite Fleet Manager http://www.granitefleet.com/
 
S

Stuart McCall

Jack Leach said:
Nevermind, I got it...


Dim x(3,1) as Variant

x(0,0) = 0
x(0,1) = 1
x(1,0) = 1
x(1,1) = 0
x(2,0) = -1
x(2,1) = 0
x(3,0) = 0
x(3, 1) = -1


sorry about that... stupid question, finally found it on my own

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
<snip>

Confusing at first, aren't they? Personally I think I would have gone for
your delimited list idea, then use the Split function to get the array from
it.
 
C

ChrisO

Perhaps something a little more esoteric...

Code:
Option Explicit
Option Compare Text


' The Linear dimension resolution.
Private Type LinearResolution
Val As Double
End Type

' A two dimensional point.
' One X and one Y.
Private Type Point
X As LinearResolution
Y As LinearResolution
End Type

' A Circle.
Private Type udtCircle
CentreX As Point
CentreY As Point
Radius As LinearResolution
End Type

' A hole.
Private Type udtHole
Hole As udtCircle
Depth As LinearResolution
End Type

' An array of holes.
Public Type udtHoleArray
HoleArray() As udtHole
End Type


Sub TestTheHoleArray()
Dim MyHoleArray As udtHoleArray

ReDim Preserve MyHoleArray.HoleArray(0)

With MyHoleArray.HoleArray(0)
.Hole.CentreX.X.Val = 123
.Hole.CentreY.Y.Val = 456.123
.Hole.Radius.Val = 0.0123456
.Depth.Val = 8.895
End With

MsgBox MyHoleArray.HoleArray(0).Hole.CentreX.X.Val & ",  " & _
MyHoleArray.HoleArray(0).Hole.CentreY.Y.Val & ",  " & _
MyHoleArray.HoleArray(0).Hole.Radius.Val & ",  " & _
MyHoleArray.HoleArray(0).Depth.Val

' Re-dimension and preserve all four parent elements.
ReDim Preserve MyHoleArray.HoleArray(1)

MsgBox MyHoleArray.HoleArray(0).Hole.CentreX.X.Val & ",  " & _
MyHoleArray.HoleArray(0).Hole.CentreY.Y.Val & ",  " & _
MyHoleArray.HoleArray(0).Hole.Radius.Val & ",  " & _
MyHoleArray.HoleArray(0).Depth.Val

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top