Redigera

Dela via


INameCreationService.IsValidName(String) Method

Definition

Gets a value indicating whether the specified name is valid.

public:
 bool IsValidName(System::String ^ name);
public bool IsValidName (string name);
abstract member IsValidName : string -> bool
Public Function IsValidName (name As String) As Boolean

Parameters

name
String

The name to validate.

Returns

true if the name is valid; otherwise, false.

Examples

The following code example provides an example INameCreationService.IsValidName method implementation. The method uses a string validation scheme that examines each character of the specified string to determine whether the specified string is a valid name. The method returns true if the string is valid, or false otherwise.

// Returns whether the specified name contains 
// all valid character types.
virtual bool IsValidName( String^ name )
{
   for ( int i = 0; i < name->Length; i++ )
   {
      Char ch = name[ i ];
      UnicodeCategory uc = Char::GetUnicodeCategory( ch );
      switch ( uc )
      {
         case UnicodeCategory::UppercaseLetter:
         case UnicodeCategory::LowercaseLetter:
         case UnicodeCategory::TitlecaseLetter:
         case UnicodeCategory::DecimalDigitNumber:
            break;

         default:
            return false;
      }
   }
   return true;
}
// Returns whether the specified name contains 
// all valid character types.
public bool IsValidName(string name)
{            
    for(int i = 0; i < name.Length; i++)
    {
        char ch = name[i];
        UnicodeCategory uc = Char.GetUnicodeCategory(ch);
        switch (uc) 
        {
            case UnicodeCategory.UppercaseLetter:       
            case UnicodeCategory.LowercaseLetter:     
            case UnicodeCategory.TitlecaseLetter:                                                  
            case UnicodeCategory.DecimalDigitNumber:                         
                break;
            default:
                return false;                
        }
    }
    return true;        
 }
' Returns whether the specified name contains 
' all valid character types.
Public Function IsValidName(ByVal name As String) As Boolean Implements INameCreationService.IsValidName
    Dim i As Integer
    For i = 0 To name.Length - 1
        Dim ch As Char = name.Chars(i)
        Dim uc As UnicodeCategory = [Char].GetUnicodeCategory(ch)
        Select Case uc
            Case UnicodeCategory.UppercaseLetter, UnicodeCategory.LowercaseLetter, UnicodeCategory.TitlecaseLetter, UnicodeCategory.DecimalDigitNumber
            Case Else
                Return False
        End Select
    Next i
    Return True
End Function

Remarks

An implementation of the INameCreationService can have rules that define the parameters for valid names. This method can be implemented to validate a name and enforce those rules.

Applies to