BlueBarren
BlueBarren

Reputation: 351

SamAccountName to UpperCase

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

Upvotes: 0

Views: 230

Answers (3)

Wazner
Wazner

Reputation: 3102

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.

Upvotes: 1

M. Wiśnicki
M. Wiśnicki

Reputation: 6203

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

Try like this

Upvotes: 0

David L
David L

Reputation: 33863

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();

Upvotes: 1

Related Questions