The Artima Developer Community
Sponsored Link •

C# Answers Forum
C# arraylist and recursion problems

1 reply on 1 page. Most recent reply: May 15, 2006 11:02 AM by matt davis

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
matt davis

Posts: 2
Nickname: mattda
Registered: May, 2006

C# arraylist and recursion problems Posted: May 15, 2006 7:29 AM
Reply to this message Reply
Advertisement
Ok I am new to this c# stuff. I have used c++ for a while and have to say that this new c# thing is pretty nice. Ok here is my problem. C# doesn't have vectors so I am using an arrylist instead. I don't know if you can set a type or not but I haven't. I store a list of files that I am recursively compiling as it runs through each directory. I was stupid and created a new instance of the array list each time I recursed. So I figured I need to make the arraylist global, but I don't know how to do this. Here is my code for the recursive directory listing. Let me know if you can spot something else I can do instead or if you can spot another error that I have not seen yet.

Thanks,

Matt

public ArrayList getDirectoryTreeFiles(string folderName)
{
//MessageBox.Show(folderName);
DirectoryInfo di = new directoryInfo(folderName);
DirectoryInfo [] dirNames = di.GetDirectories();

if (dirNames.Length != 0)
{
foreach (DirectoryInfo tmp in dirNames)
{
directoriesFilesString.Add(getDirectoryTreeFiles(folderName + "\\" + tmp.ToString()));
//return directoriesFilesString;
}
}
else
{
DirectoryInfo tmp = new DirectoryInfo(folderName);
FileInfo[] rgFiles = tmp.GetFiles();
foreach(FileInfo fi in rgFiles)
{
directoriesFilesString.Add(folderName + "\\" + fi.Name);
}
return directoriesFilesString;
}
return directoriesFilesString;
}


matt davis

Posts: 2
Nickname: mattda
Registered: May, 2006

Re: C# arraylist and recursion problems Posted: May 15, 2006 11:02 AM
Reply to this message Reply
Ok I have figured this one out. At First I was creating a new instance of the array list every time I recursed. Also on line 9 I was saving the blank directory which was inserting a new listing of the count = function. Removing the .add function and just letting it recurse solved the problem.

Flat View: This topic has 1 reply on 1 page
Topic: C# Code Converter Previous Topic   Next Topic Topic: C# beginner

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use