please explain this!

  • Thread starter Thread starter Geoff Cox
  • Start date Start date
G

Geoff Cox

Hello,

Could someone please talk me through following code? I understand the
overall picture but not the details. Forexample, I'm not clear on the
use of ReDim (why Dim and then ReDim?), Dir$, UBound and ReDim
Preserve.

Thanks

Geoff



ReDim rayFileList(1 To 1) As String
strTemp = Dir$(FolderPath & FileSpec)
While strTemp <> ""
rayFileList(UBound(rayFileList)) = FolderPath & strTemp
ReDim Preserve rayFileList(1 To UBound(rayFileList) + 1) As_
String
strTemp = Dir
Wend
 
Dir returns the first file in the folder subsequent calls returns the next
one and so on. ReDim is used to resize the array according to the
requirements. Each time you execute the ReDim statement, all the values
currently stored in the array are lost. VB resets the values. To resize the
array without losing the data in the array use ReDim with the Preserve
keyword. Ubound is used to determine the upper bound of the array.

For example, you can enlarge an array by one element without losing the
values of the existing elements :

ReDim Preserve MyArray(UBound(MyArray) + 1)

Your code seems to be creating an array of files contained in the folder.


--
Regards,
Shyam Pillai

Animation Carbon
http://www.animationcarbon.com
 
Hello,

Could someone please talk me through following code? I understand the
overall picture but not the details. Forexample, I'm not clear on the
use of ReDim (why Dim and then ReDim?),

There are two kinds of arrays, static and dynamic. The size of static arrays
is set and fixed when they're declared; you can't add any more elements to
them than that.

Dim SomeArray(1 to 50) as String

Dynamic arrays such as used here are declared w/o size.

Dim rayFileList() as String

They must be Redimensioned to a specific size before any elements can be added:

ReDim rayFileList(1 to 42) as String

Ubound(SomeArray) tells us the current number of elements in the array, so to
add one more element to a dyamic array and still preserve any existing
elements:

Redim Preserve rayFileList(1 to Ubound(rayFileList))
 
Dir returns the first file in the folder subsequent calls returns the next
one and so on. ReDim is used to resize the array according to the
requirements. Each time you execute the ReDim statement, all the values
currently stored in the array are lost. VB resets the values. To resize the
array without losing the data in the array use ReDim with the Preserve
keyword. Ubound is used to determine the upper bound of the array.

For example, you can enlarge an array by one element without losing the
values of the existing elements :

ReDim Preserve MyArray(UBound(MyArray) + 1)

Your code seems to be creating an array of files contained in the folder.

Thanks Shyam,

What's the significance of the $ in Dir$?

Geoff
 
There are two kinds of arrays, static and dynamic. The size of static arrays
is set and fixed when they're declared; you can't add any more elements to
them than that.

Dim SomeArray(1 to 50) as String

Dynamic arrays such as used here are declared w/o size.

Dim rayFileList() as String

They must be Redimensioned to a specific size before any elements can be added:

ReDim rayFileList(1 to 42) as String

Ubound(SomeArray) tells us the current number of elements in the array, so to
add one more element to a dyamic array and still preserve any existing
elements:

Redim Preserve rayFileList(1 to Ubound(rayFileList))

Thanks Steve.

Cheers

Geoff
 
What's the significance of the $ in Dir$?

I was going to suggest that you have a look in VBA help for this but realized
that'd be cruel. You're probably using PPT 2002 or higher and have the New!
Improved! Useless! help system.

From PPT 2000:
============================================================================
Some functions have two versions: one that returns a Variant data type and one
that returns a String data type. The Variant versions are more convenient
because variants handle conversions between different types of data
automatically. They also allow Null to be propagated through an expression. The
String versions are more efficient because they use less memory.

=============================================================================

Dir$ returns a String, Dir returns a Variant

Unless you have good reason to use a variant and know exactly why you need to
use one instead of a standard data type like String, Long, etc. don't.
 
I was going to suggest that you have a look in VBA help for this but realized
that'd be cruel. You're probably using PPT 2002 or higher and have the New!
Improved! Useless! help system.

From PPT 2000:
============================================================================
Some functions have two versions: one that returns a Variant data type and one
that returns a String data type. The Variant versions are more convenient
because variants handle conversions between different types of data
automatically. They also allow Null to be propagated through an expression. The
String versions are more efficient because they use less memory.

=============================================================================

Dir$ returns a String, Dir returns a Variant

Unless you have good reason to use a variant and know exactly why you need to
use one instead of a standard data type like String, Long, etc. don't.

Thanks Steve - I do indeed have an "improved" version (PPT 2003)!

Cheers

Geoff
 
Back
Top