0

I am trying to capitalize the first letter of the first name, middle name and the last name.
I am trying to use System.Globalization.TextInfo.ToTitleCase(newUser.Firstname) but i am getting an error in visual studio saying "An object reference is required for the non-static field, method or property". Help me with the solution for this:

public static UserAccount CreateUser(string firstName, string middleName, string lastName, string nameSuffix, int yearOfBirth, int? monthOfBirth, int? dayOfBirth, string email, string password, UserRole roles, bool tosAccepted = false)
{
    var newUser = new UserAccount
    {
        CreationDate = DateTime.Now,
        ActivationCode = Guid.NewGuid(),
        FirstName = firstName,
        MiddleName = middleName,
        LastName = lastName,
        NameSuffix = nameSuffix,
        YearOfBirth = yearOfBirth,
        MonthOfBirth = monthOfBirth,
        DayOfBirth = dayOfBirth,
        Email = email,
        UserRoles = roles,
        ToSAccepted = tosAccepted
    };
    string newUsers= System.Globalization.TextInfo.ToTitleCase(newUser.FirstName);
    newUser.SetPassword(password);
    return newUser;
}
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
  • 3
    RTFM, please. The documentation for ToTitleCase shows you how to use that method properly. – tnw Jun 30 '14 at 15:16
  • Well, I'm guessing OP got this from the accepted (and highly upvoted) answer to [How do I capitalize first letter of first name and last name in C#?](http://stackoverflow.com/questions/72831/how-do-i-capitalize-first-letter-of-first-name-and-last-name-in-c?rq=1). – p.s.w.g Jun 30 '14 at 15:25

2 Answers2

5

ToTitleCase is an instance method so you have to call it with a reference to an instance of TextInfo. You can get an instance of TextInfo from the current thread's culture like this:

var textInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;

Or from a specific culture like this:

var textInfo = new CultureInfo("en-US",false).TextInfo;

Also, it returns a new string rather than modifying the string that you pass in. Try something like this instead:

public static UserAccount CreateUser(string firstName, string middleName, string lastName, string nameSuffix, int yearOfBirth, int? monthOfBirth, int? dayOfBirth, string email, string password, UserRole roles, bool tosAccepted = false)
{
    var textInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;

    var newUser = new UserAccount
    {
        CreationDate = DateTime.Now,
        ActivationCode = Guid.NewGuid(),
        FirstName = textInfo.ToTitleCase(firstName),
        MiddleName = middleName,
        LastName = textInfo.ToTitleCase(lastName),
        NameSuffix = nameSuffix,
        YearOfBirth = yearOfBirth,
        MonthOfBirth = monthOfBirth,
        DayOfBirth = dayOfBirth,
        Email = email,
        UserRoles = roles,
        ToSAccepted = tosAccepted
    };
    newUser.SetPassword(password);
    return newUser;
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • what if i need to add a period after the middle initial if only one letter is entered in the field? Can i still use something similar – Nitin Jaitely Jun 30 '14 at 16:20
  • @NitinJaitely In that case, you can use something along the lines of `MiddleName = middleName + (middleName.Length == 1 ? "." : "")`—`?…:` is the [conditional operator](http://msdn.microsoft.com/en-us/library/ty67wk28.aspx) – p.s.w.g Jun 30 '14 at 16:25
2

You can call the CultureInfo.CurrentCulture.TextInfo.ToTitleCase method to do this for you:

CultureInfo.CurrentCulture.TextInfo.ToTitleCase(lastName)
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325