6

I am trying count total number of files in all the sub folder of a given path. I am using recursive function call. What could be the reason?

Code:

int iCount =0; 
getFileCount(_dirPath, out iCount);

private void getFileCount(string _path, out int iCount )
{          
    try
    {
        // gives error :Use of unassigned out parameter 'iCount' RED Underline
        iCount += Directory.GetFiles(_path).Length;

        foreach (string _dirPath in Directory.GetDirectories(_path))
            getFileCount(_dirPath, out iCount);
    }
    catch { }
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
  • have declared iCount in main/global ? – H. Mahida Jul 15 '14 at 11:41
  • 1
    Why can't you make method return value instead of passing it as out? What's the rror messae that you get? What's more, you need to assing zero on the top of recursion (f.e. with 0). –  Jul 15 '14 at 11:42
  • updated , I forgot it to write in question, i have assign zero to count variable –  Jul 15 '14 at 11:44
  • @user3732729 since you declared iCount as `out` you need to set that before returning from the method Reference(http://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx) – Deeptechtons Jul 15 '14 at 11:44
  • @NikhilAgrawal that question i have checked but did not helped me. Thanks all for quick response. –  Jul 15 '14 at 11:52
  • Please remove Duplicate... –  Jul 15 '14 at 12:15

2 Answers2

11

You want a ref parameter not an out parameter since you're both accepting the value and setting a new value.

int iCount = 0;
getFileCount(_dirPath, ref iCount);

private void getFileCount(string _path, ref int iCount )
{          
    try
    {
        // gives error :Use of unassigned out parameter 'iCount' RED Underline
        iCount += Directory.GetFiles(_path).Length;

        foreach (string _dirPath in Directory.GetDirectories(_path))
            getFileCount(_dirPath, ref iCount);
    }
    catch { }
}

even better, don't use out parameters at all.

private int getFileCount(string _path) {
    int count = Directory.GetFiles(_path).Length;
    foreach (string subdir in Directory.GetDirectories(_path))
        count += getFileCount(subdir);

    return count;
}       

and even better than that, don't create a function to do something the framework already does built in..

int count = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length

and we're not done getting better... don't waste space and cycles creating an array of files when all you need is a length. Enumerate them instead.

int count = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Count();
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • Thanks for answer, I tried Ref function , but gives me error at function call, also I do not have to use any built in function.. –  Jul 15 '14 at 11:54
  • When you change the function to `ref`, you also need to change the call to `ref` and need to initialize the variable before the call. – Samuel Neff Jul 15 '14 at 12:00
  • says "Method must have return type" at getFileCount(_dirPath, ref iCount); –  Jul 15 '14 at 12:02
  • I did that but no luck, please remove duplicate if you can.. –  Jul 15 '14 at 12:14
0

Parameters which are passed as out need to be initialized within the function. As iCount is not initialized yet the value is not known and it does not where to start with even though it is an integer which default values is 0.

I would recommend not to couple the out parameter together with a recursive function. Instead it would be possible to use a regular return parameter. Microsoft itself suggests via some static analysis rules to avoid out parameters if possible.

Alex
  • 5,240
  • 1
  • 31
  • 38