Type mismatch error when setting array size to value returned by function

I thought teh below method would be a more efficient way of sizing an array with a variable number of items than the usual method of having to redim it for each new entry. However, I get the error 'Type mismatch: 'g_TXTList''.
Dim g_objFSO
Dim g_TXTPath

Set g_objFSO = CreateObject("Scripting.FileSystemObject")
g_TXTPath = "C:\testdir"
g_TXTList(TXTCount(g_TXTPath))									'Sets arrray size to no of TXTs in g_TXTPath

'
' Returns a count of the text files in the passed in directory
'
Function TXTCount(g_TXTPath)
    TXTCount = 0
    
    Set objFolder = g_objFSO.GetFolder(g_TXTPath)
    Set colFiles = objFolder.Files
    
    wscript.echo "Counting TXTs in " & g_TXTPath
    
    For Each objFile in colFiles
        If UCase(g_objFSO.GetExtensionName(objFile.name)) = "TXT" Then
            TXTCount = TXTCount + 1
        End If
    Next
    
    wscript.echo "TXT count: " & TXTCount
    
End Function

Open in new window

LVL 1
mark_667Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

paololabeCommented:
I can't see  othe method like this:
Dim g_TXTList()
redim g_TXTList(TXTCount(g_TXTPath))      

the error you encontered is because you have to declare an array with Dim before using it and you need a constant as size

P.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raiERBCommented:
THe problem is your function!

You are using the same name for the function as well as a variable.  When the tript hits the line "TXTCount = TXTCount + 1" it tries to return a value as well as calling itseld again.

Although calling the function from within itself cal be valid programming (e.g. recursion through subdirectories) in this case it causes the script to crash.

Use the following:

Dim g_objFSO
Dim g_TXTPath
Dim TXTList

Set g_objFSO = CreateObject("Scripting.FileSystemObject")
g_TXTPath = "C:\testdir"
g_TXTList(TXTCount(g_TXTPath))									'Sets arrray size to no of TXTs in g_TXTPath

'
' Returns a count of the text files in the passed in directory
'
Function TXTCount(g_TXTPath)
    Dim intCounter : intCounter = 0
    
    TXTCount = 0  ' Ensures something is returned, at this point a valid statement
    
    Set objFolder = g_objFSO.GetFolder(g_TXTPath)
    Set colFiles = objFolder.Files
    
    wscript.echo "Counting TXTs in " & g_TXTPath
    
    For Each objFile in colFiles
        If UCase(g_objFSO.GetExtensionName(objFile.name)) = "TXT" Then
            intCounter = intCounter + 1
        End If
    Next
    
    wscript.echo "TXT count: " & intCounter
    TXTCount = intCounter

End Function 

Open in new window


As a final note, be aware if you code like this, there is a chance that the script will crash at a later point.  Example is if between the assigning of the array size and the assigning of values a text file is added.

Dependiong on what you need to do, consider using dictionaries.  These bring special functionalities that an array does not have and do not require redimming.
0
raiERBCommented:
notices a slight error in my code:

change:

Dim TXTList

To:

Dim TXTList()


0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

APoPhySptCommented:
hi, I've taken a look into your code and I've made the following corrections.. try it like this..
Don't really know if it is exactly what you are looking for, but just thought I'd try and help



Dim g_objFSO As Object
Dim g_TXTPath As String
Dim g_TXTList() As Variant

Set g_objFSO = CreateObject("Scripting.FileSystemObject")
g_TXTPath = "C:\testdir"
ReDim g_TXTList(TXTCount(g_TXTPath))

'
' Returns a count of the text files in the passed in directory
'
Function TXTCount(g_TXTPath) As Integer
    TXTCount = 0
   
    Set objFolder = g_objFSO.GetFolder(g_TXTPath)
    Set colFiles = objFolder.Files
   
    wscript.echo "Counting TXTs in " & g_TXTPath
   
    For Each objFile In colFiles
        If UCase(g_objFSO.GetExtensionName(objFile.Name)) = "TXT" Then
            TXTCount = TXTCount + 1
        End If
    Next
       
    wscript.echo "TXT count: " & TXTCount
   
End Function



Cheers
0
mark_667Author Commented:
paololabe I'm pretty sure you can size an array without using a constant, as I understand it any positive int will do.
APoPhySpt you can declare variables as specific types in VBA but not VBScript, I get a  Expected end of statement error on the first line when trying to run your code. raiERB, I get a type mismatch on the line
g_TXTList(TXTCount(g_TXTPath))
with your code.
0
paololabeCommented:
very strange it works for me.
anyway the only way I found is this in the code snippet.
using a variable as size is not possible.
try this simple script:

size=5
Dim myArray(size)

and let me know.


Dim g_objFSO
Dim g_TXTPath

Set g_objFSO = CreateObject("Scripting.FileSystemObject")
g_TXTPath = "C:\Users\Utente\Desktop\Test"

size=TXTCount(g_TXTPath)
ReDim g_TXTList(size)									'Sets arrray size to no of TXTs in g_TXTPath
'
' Returns a count of the text files in the passed in directory
'
Function TXTCount(g_TXTPath)
    TXTCount = 0
    
    Set objFolder = g_objFSO.GetFolder(g_TXTPath)
    Set colFiles = objFolder.Files
    
    wscript.echo "Counting TXTs in " & g_TXTPath
    
    For Each objFile in colFiles
        If UCase(g_objFSO.GetExtensionName(objFile.name)) = "TXT" Then
            TXTCount = TXTCount + 1
        End If
    Next
    
    wscript.echo "TXT count: " & TXTCount
    
End Function

Open in new window

0
James ElliottManaging DirectorCommented:
Dim size


0
Bill PrewCommented:
First post by paololabe: in this question is the right approach.  You can't use a variable in DIM statement as the scalar value (size).  VBS doesn't like that because it doesn't know the value of the variable until runtime, but it wants to allocate the array before that with the DIM.

To be able to size the array at runtime you need to use the REDIM statement as in the first post to this question.  That works fine, and requires the DIM before it with no size for the array.

~bp
0
mark_667Author Commented:
Thanks for the quick answer paololabe and thanks to billprew for explaining it. I've increased the points, paololabe gets the original 250, billprew gets 125 for his help.
0
Bill PrewCommented:
Glad that helped, and thanks for the consideration.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.