问题描述:

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;

}

网友答案:

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

}
网友答案:

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

  • ref (C# Reference)
  • return (C# Reference)
网友答案:

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.

网友答案:

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

相关阅读:
Top