C#本身调用方法的语法

时间:2016-12-18 18:46:20

标签: c#

我对C#很新,并尝试创建一个辅助方法,该方法根据满足某些条件返回一个对象。它应该能够使用从数据库中提取的正确结果字符串返回自身的对象,但正如您所看到的,我在Var结果声明中收到错误,我也尝试了新的'宣言方式,但这也失败了。我用google搜索c#方法并调用它们,但是教程看起来更基本,而且我已经知道了。

private EmailTemplateContents(User user, int companyId, string template, int cultureId)
        {
            var result = new EmailTemplateContents();
            EmailTemplateContents et = new EmailTemplateContents();
            var extendedInfo = _accountService.GetUserExtendedInfoOrDefault(user.Id, companyId);
            var theCulture = _languageService.GetLanguageCodeOrDefault(extendedInfo.LanguageCode).Culture;
            string theClass = "campaign";

            var bodyTextKey = string.Format("{0}-{1}-bodytext", companyId, template);
            result.BodyText = _resourceProviderService.LocalizationResourceValue(theClass, bodyTextKey, theCulture);

            var subjectKey = string.Format("{0}-{1}-subject", companyId, template);
            result.Subject = _resourceProviderService.LocalizationResourceValue(theClass, subjectKey, theCulture);

            var signatureKey = string.Format("{0}-{1}-signature", companyId, template);
            result.Signature = _resourceProviderService.LocalizationResourceValue(theClass, signatureKey, theCulture);

            var buttonLinkKey = string.Format("{0}-{1}-buttontext", companyId, template);
            result.ButtonText = _resourceProviderService.LocalizationResourceValue(theClass, buttonLinkKey, theCulture);

            return result;
        }

enter image description here

3 个答案:

答案 0 :(得分:1)

你还没有定义你的方法的名称或返回类型,看起来你错过了方法名称,你的方法定义应该是:

private EmailTemplateContents GetEmailTemplateContents(User user, int companyId, string template, int cultureId)
{
  .................
  ..................
}

所以你的方法最终会像:

private EmailTemplateContents GetEmailTemplateContents(User user, int companyId, string template, int cultureId)
{
    var result = new EmailTemplateContents();

    var extendedInfo = _accountService.GetUserExtendedInfoOrDefault(user.Id, companyId);
    var theCulture = _languageService.GetLanguageCodeOrDefault(extendedInfo.LanguageCode).Culture;
   string theClass = "campaign";

   var bodyTextKey = string.Format("{0}-{1}-bodytext", companyId, template);
   result.BodyText = _resourceProviderService.LocalizationResourceValue(theClass, bodyTextKey, theCulture);

   var subjectKey = string.Format("{0}-{1}-subject", companyId, template);
   result.Subject = _resourceProviderService.LocalizationResourceValue(theClass, subjectKey, theCulture);

   var signatureKey = string.Format("{0}-{1}-signature", companyId, template);
   result.Signature = _resourceProviderService.LocalizationResourceValue(theClass, signatureKey, theCulture);

   var buttonLinkKey = string.Format("{0}-{1}-buttontext", companyId, template);
   result.ButtonText = _resourceProviderService.LocalizationResourceValue(theClass, buttonLinkKey, theCulture);

   return result;
}

答案 1 :(得分:1)

这不是辅助方法。这具有构造函数的结构。方法在签名声明中具有返回值:

public ReturnType MethodName(int parameter);

构造函数将只包含带有参数的类名,如您所示。当你拨打这一行时:

var result = new EmailTemplateContents();

编译器需要一个类型为EmailTemplateContents的无参数构造函数。这意味着您要创建该类型的对象。

如果您的班级名为EmailTemplateContents,则需要重命名此方法并为其指定返回值:

private EmailTemplateContents A_different_Name(User user, int companyId, string template, int cultureId)

并确保在类EmailTemplateContents

中有一个无参数构造函数

答案 2 :(得分:1)

您还没有给该方法命名,只有一种类型。看起来应该是这样的:

private EmailTemplateContents GetEmailContents(User user, int companyId, string template, int cultureId)
{
}