简体   繁体   中英

c# error returning value from function

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 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

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.

如果您使用的是ref string emailAttachmentsPath ,则不需要返回任何字符串

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM