0

I am tring to use a method or function to create the directory tree that I need, and return the new path to the calling procedure.

But I cannot get it to compile

I get an error cannot convert method group 'localAttachmentsPath' to non delegate type string ..

here is my code - what have I done wrong? And is there a better way to accomplish this?

        private string localAttachmentsPath(ref string emailAttachmentsPath)
        {

            string sYear = DateTime.Today.Year.ToString();
            string sMonth = DateTime.Now.ToString("MMMM");
            string sDirectoryDate = DateTime.Today.ToString("dd.MM.yyyy");

            if (!Directory.Exists(emailAttachmentsPath))
            {
                Directory.CreateDirectory(emailAttachmentsPath);

            }

            emailAttachmentsPath = emailAttachmentsPath + "\\" + sYear;
            if (!Directory.Exists(emailAttachmentsPath))
            {
                Directory.CreateDirectory(emailAttachmentsPath);
            }

            emailAttachmentsPath = emailAttachmentsPath + "\\" + sMonth;
            if (!Directory.Exists(emailAttachmentsPath))
            {
                Directory.CreateDirectory(emailAttachmentsPath);
            }

            emailAttachmentsPath = emailAttachmentsPath + "\\" + sDirectoryDate;
            if (!Directory.Exists(emailAttachmentsPath))
            {
                Directory.CreateDirectory(emailAttachmentsPath);
            }

            //localAttachmentsPath = emailAttachmentsPath;
            return localAttachmentsPath;

        }
Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
  • 1
    What are you trying to return? All you're doing at the minute is returning a reference to your own function, not a value. If you want to return emailAttachementsPath just put "return emailAttachmentsPath", you don't need to set the name of the function to the return value (is that VB syntax?) – DoctorMick Apr 03 '14 at 15:55
  • And just so you know, `CreateDirectory` already creates the whole tree if you pass a full path, you dont need to create it subfolder by subfolder... – Laurent S. Apr 03 '14 at 15:58
  • @Bartdude: thanks, now I don't need a function :) – Our Man in Bananas Apr 03 '14 at 16:59
  • 1
    I almost wrote an answer out of this actually. Indeed, you can replace the whole function by a single line of code for the same effect. Here's another piece of advice : when you're not sure, read the doc and/or try it... it would have saved you some time I guess ;-) – Laurent S. Apr 03 '14 at 17:01

4 Answers4

0

You need to simply specify the value you want to return, like this:

return emailAttachmentsPath;

I would also recommend you drop the ref keywordfrom the parameter.

Further Reading

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

It looks like the problem is this:

return localAttachmentsPath;

That is a function, I think you want to declare a local variable with some other name than the function name.

midfield99
  • 313
  • 1
  • 4
  • 13
0

If you are using ref string emailAttachmentsPath than you do not need to return any string

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
0

You can make your return type as void and remove return statement as you are passing variable emailAttachmentsPath as reference variable so it wouldbe updated from the caller without return statement.

Your Method should look like:

private void localAttachmentsPath(ref string emailAttachmentsPath)
{

//your code

//no return statement

}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67