繁体   English   中英

C#错误从函数返回值

[英]c# error returning value from function

我正在尝试使用一种方法或函数来创建所需的目录树,并将新路径返回到调用过程。

但是我无法编译它

我收到一个错误cannot convert method group 'localAttachmentsPath' to non delegate type string ..

这是我的代码-我做错了什么? 有没有更好的方法可以做到这一点?

        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;

        }

您只需指定要返回的值,如下所示:

return emailAttachmentsPath;

我还建议您从参数中删除ref关键字。

进一步阅读

看起来问题是这样的:

return localAttachmentsPath;

那是一个函数,我想您想用一个不同于函数名的其他名称来声明一个局部变量。

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

您可以将返回类型设置为void并在将变量emailAttachmentsPath传递为参考变量时删除return语句,这样就可以在没有return语句的情况下从调用方对其进行更新。

您的方法应如下所示:

private void localAttachmentsPath(ref string emailAttachmentsPath)
{

//your code

//no return statement

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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