Passing array data

Passing array data

Anonymous
Not applicable
365 Views
5 Replies
Message 1 of 6

Passing array data

Anonymous
Not applicable
Could someone give me a hand passing array data from one function to
another similiar to this?

Type LayerData
Name As String
Color As String
Linetype As String
End Type

' ----- Read data file
Function Function_1 (strFName As String)
Dim i As Integer
Dim lyrData As LayerData

Open strFName For Input As #1
' Loop until end of file
i = 0
ReDim lyrData(i)
On Error Resume Next
Do While Not EOF(1)
' Read data
ReDim Preserve lyrData(i)
Input #1, lyrData(i).Name, lyrData(i).Color, lyrData(i).Linetype
i = i + 1
Loop

' Close file
Close #1

' Here I would like to pass LyrData to Function_2
Function_2 lyrData

End Function

' ----- Function to receive array data
Function Function_2 (aryData)
Dim i As Integer
' do something with the array data...
For i = 0 To UBound(aryData)
Debug.Print aryData(i).Name
Next
End Function
0 Likes
366 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Trevor-

Here are some tips.

1) You need to declare lyrData as an array like this:

Dim lyrData() As LayerData

2) You need to use ReDim Preserve lyrData(0 to i) INSIDE the loop where you
read the file.

3) I'd declare the argument in the second function as:

Function Function_2 (aryData() As LayerData)

I'd also declare a local variable in it as LayerData

4) I'd use the FreeFile function instead of hardcoding the file as #1.

5) Instead of using a Type, I'd create a Class. Once you have done a few,
they are no harder to build, and are much more robust.

Mark Holder

Trevor Willmer wrote in message
news:[email protected]...
> Could someone give me a hand passing array data from one function to
> another similiar to this?
>
> Type LayerData
> Name As String
> Color As String
> Linetype As String
> End Type
>
> ' ----- Read data file
> Function Function_1 (strFName As String)
> Dim i As Integer
> Dim lyrData As LayerData
>
> Open strFName For Input As #1
> ' Loop until end of file
> i = 0
> ReDim lyrData(i)
> On Error Resume Next
> Do While Not EOF(1)
> ' Read data
> ReDim Preserve lyrData(i)
> Input #1, lyrData(i).Name, lyrData(i).Color, lyrData(i).Linetype
> i = i + 1
> Loop
>
> ' Close file
> Close #1
>
> ' Here I would like to pass LyrData to Function_2
> Function_2 lyrData
>
> End Function
>
> ' ----- Function to receive array data
> Function Function_2 (aryData)
> Dim i As Integer
> ' do something with the array data...
> For i = 0 To UBound(aryData)
> Debug.Print aryData(i).Name
> Next
> End Function
>
0 Likes
Message 3 of 6

Anonymous
Not applicable
Tony Tanzillo once pointed out to me that as long as you're going through
the process of collecting information in a loop, you might as well perform
your actions within the same loop. This of course assumes that your purpose
is more than just retrieving a list of layer names. It looks like you're
using an external text file to first create an array containing layer info
then using another function to actually create the layers. Combine the
routines and create the layers as you read the information. It's a lot more
efficient.

As for your query on passing arrays, I usually use a variant as the input
parameter of a function that expects an array. This allows you to pass any
type of array and by avoiding the call for a specific data type, you won't
have to rewrite your function just because you changed data types. One other
note: do not assume that arrays always start at 0. If you ever use Option
Base 1 or give your code to someone who does, your routine will bomb.
Instead, construct your For Next loop using LBound and UBound.

--
"I have noted that persons with bad judgment are most insistent that we do
what they think best."
Visit AcadXtreme for a chance to win a copy of AutoCAD LT 2000
Contest ends 5/26/00
http://www.acadx.com

"Trevor Willmer" wrote in message
news:[email protected]...
> Could someone give me a hand passing array data from one function to
> another similiar to this?
>
> Type LayerData
> Name As String
> Color As String
> Linetype As String
> End Type
>
> ' ----- Read data file
> Function Function_1 (strFName As String)
> Dim i As Integer
> Dim lyrData As LayerData
>
> Open strFName For Input As #1
> ' Loop until end of file
> i = 0
> ReDim lyrData(i)
> On Error Resume Next
> Do While Not EOF(1)
> ' Read data
> ReDim Preserve lyrData(i)
> Input #1, lyrData(i).Name, lyrData(i).Color, lyrData(i).Linetype
> i = i + 1
> Loop
>
> ' Close file
> Close #1
>
> ' Here I would like to pass LyrData to Function_2
> Function_2 lyrData
>
> End Function
>
> ' ----- Function to receive array data
> Function Function_2 (aryData)
> Dim i As Integer
> ' do something with the array data...
> For i = 0 To UBound(aryData)
> Debug.Print aryData(i).Name
> Next
> End Function
>
0 Likes
Message 4 of 6

Anonymous
Not applicable
Mark,

Declaring the argument in the second function as you suggested was just what I
needed. Thanks for the FreeFile tip. I haven't made any attempts at creating a
Class... yet.

Mark Holder wrote:

> Trevor-
>
> Here are some tips.
>
> 1) You need to declare lyrData as an array like this:
>
> Dim lyrData() As LayerData
>
> 2) You need to use ReDim Preserve lyrData(0 to i) INSIDE the loop where you
> read the file.
>
> 3) I'd declare the argument in the second function as:
>
> Function Function_2 (aryData() As LayerData)
>
> I'd also declare a local variable in it as LayerData
>
> 4) I'd use the FreeFile function instead of hardcoding the file as #1.
>
> 5) Instead of using a Type, I'd create a Class. Once you have done a few,
> they are no harder to build, and are much more robust.
>
> Mark Holder
0 Likes
Message 5 of 6

Anonymous
Not applicable
Frank,

Thanks for the tips, a lot of the code was cut too minimize the post but they
are helpful and I will keep them in mind.

Frank Oquendo wrote:

> Tony Tanzillo once pointed out to me that as long as you're going through
> the process of collecting information in a loop, you might as well perform
> your actions within the same loop. This of course assumes that your purpose
> is more than just retrieving a list of layer names. It looks like you're
> using an external text file to first create an array containing layer info
> then using another function to actually create the layers. Combine the
> routines and create the layers as you read the information. It's a lot more
> efficient.
>
> As for your query on passing arrays, I usually use a variant as the input
> parameter of a function that expects an array. This allows you to pass any
> type of array and by avoiding the call for a specific data type, you won't
> have to rewrite your function just because you changed data types. One other
> note: do not assume that arrays always start at 0. If you ever use Option
> Base 1 or give your code to someone who does, your routine will bomb.
> Instead, construct your For Next loop using LBound and UBound.
>
> --
> "I have noted that persons with bad judgment are most insistent that we do
> what they think best."
> Visit AcadXtreme for a chance to win a copy of AutoCAD LT 2000
> Contest ends 5/26/00
> http://www.acadx.com
>
0 Likes
Message 6 of 6

Anonymous
Not applicable
Trevor-

One more **very important** tip. Always include "Option Explicit" at the top
of every module. Allowing undeclared variables makes misspelled variables
very hard to identify.

Mark Holder

Trevor Willmer wrote in message
news:[email protected]...
> Mark,
>
> Declaring the argument in the second function as you suggested was just
what I
> needed. Thanks for the FreeFile tip. I haven't made any attempts at
creating a
> Class... yet.
>
> Mark Holder wrote:
>
> > Trevor-
> >
> > Here are some tips.
> >
> > 1) You need to declare lyrData as an array like this:
> >
> > Dim lyrData() As LayerData
> >
> > 2) You need to use ReDim Preserve lyrData(0 to i) INSIDE the loop where
you
> > read the file.
> >
> > 3) I'd declare the argument in the second function as:
> >
> > Function Function_2 (aryData() As LayerData)
> >
> > I'd also declare a local variable in it as LayerData
> >
> > 4) I'd use the FreeFile function instead of hardcoding the file as #1.
> >
> > 5) Instead of using a Type, I'd create a Class. Once you have done a
few,
> > they are no harder to build, and are much more robust.
> >
> > Mark Holder
>
0 Likes