Results 1 to 5 of 5

Thread: Need help with my recrusive function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Need help with my recrusive function

    I am trying to write a recursive function to return a list of files within folders and subfolders of a given directory.

    I get the error, Object reference not set to an instance of an object, when I do FileList.Add.

    Code:
      Function thisfunction(ByVal path As String, ByVal FileList As ArrayList)
            Dim di As New DirectoryInfo(path)
            Dim folder As DirectoryInfo
            Dim folders() As DirectoryInfo
            Dim file As FileInfo
            Dim files() As FileInfo
    
    
            folders = di.GetDirectories()
    
            For Each folder In folders
                thisfunction(path & "\" & folder.Name, FileList) 
            Next
    
            files = di.GetFiles
    
            For Each file In files
                FileList.Add(path & "\" & file.Name) '-capture currentworking path & filename
            Next
    
        End Function
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim Files As ArrayList
    
            thisfunction("c:\test", Files)
    
            MessageBox.Show(Files.Count)
        End Sub
    Last edited by nkad; Jan 14th, 2005 at 01:14 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Need help with my recrusive function

    Where have you declared FileList?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Re: Need help with my recrusive function

    You must have missed it. It's a parameter of thisfunction(). I bet this doesnt work like it did in VB6. But I would like a way to do something similar w/o having to reference some other variable defined in a more global scope.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Need help with my recrusive function

    My bad.

    I'll go over this again.

  5. #5
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Need help with my recrusive function

    Hi
    You forgot to use the arraylist constructor call.
    See modified code
    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim Files As new ArrayList
    
            thisfunction("c:\test", Files)
    
            MessageBox.Show(Files.Count)
        End Sub
    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width