theHussle
theHussle

Reputation: 321

C# Syntax around calling method within itself

I'm fairly new to C# and trying to create a helper method which returns an object based on meeting certain conditions. It should be able to return an object of itself with the correct result strings pulled from the database, but as you can see I get an error on Var Result declaration, I also tried the 'new' way of declaration but this also failed. I have googled c# methods and calling them but the tutorials seem much more basic and what I already know.

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

Upvotes: 0

Views: 148

Answers (3)

windowtint
windowtint

Reputation: 21

You haven't given the method a name, only a type. It should look something like this:

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

Upvotes: 1

Mong Zhu
Mong Zhu

Reputation: 23732

This is not a helper method. This has the structure of a constructor. A method has a return value in the declaration of the signature:

public ReturnType MethodName(int parameter);

A constructor would have only the class name with parameters as in your case. When you call this line:

var result = new EmailTemplateContents();

the compiler expects a parameterless constructor of type : EmailTemplateContents. Meaning that you want to create an object of that type.

If your class is called EmailTemplateContents you need to rename this method and give it a return value:

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

and make sure that you have a parameterless constructor in the class EmailTemplateContents

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

you have not defined either your the name or return type of the method, it looks like you missed Method Name, your method definition should be:

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

so your method finally will be like:

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

Upvotes: 1

Related Questions