0

Does the SamAccountName property of UserPrincipal not return a string? I'm trying to take the first character of my SamAccountName and convert it .ToUpperCase() but .ToUpperCase() is not available for SamAccountName

private void firstCharToUppercase(Prinicpal principal)
{
    UserPrinicpal user = principal as UserPrincipal;
    user.SamAccountName[0].toUpperCase();
}
BlueBarren
  • 321
  • 7
  • 24

3 Answers3

1

As clearly denoted by the documentation, SamAccountName returns a string.

However, by using an indexer, you are retrieving the first character as type char, not type string.

You need to call ToString() on the result first.

user.SamAccountName[0].ToString().ToUpper();
David L
  • 32,885
  • 8
  • 62
  • 93
1

When you use the indexer on a string, it will return char representing the character at that index. The type char does have a ToUpper method, but it's static. I don't know why the .NET team chose to make string.ToUpper non-static and the char.ToUpper static.

Try this:

private void firstCharToUppercase(Prinicpal principal)
{
    UserPrinicpal user = principal as UserPrincipal;
    char.ToUpper(user.SamAccountName[0]);
}

This method is better for making a single character uppercase than calling ToString() on the character first. ToString() allocates a string which will need to be garbage collected again later, while char.ToUpper(char) does not.

Wazner
  • 2,962
  • 1
  • 18
  • 24
0
    private void firstCharToUppercase(Prinicpal principal)
{
    UserPrinicpal user = principal as UserPrincipal;
    user.SamAccountName[0].ToString().ToUpper();
}

Try like this

M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28