.NET Framework Class Library
INameCreationService..::.ValidateName Method

Gets a value indicating whether the specified name is valid.

Namespace:  System.ComponentModel.Design.Serialization
Assembly:  System (in System.dll)
Syntax

Visual Basic (Declaration)
Sub ValidateName ( _
    name As String _
)
Visual Basic (Usage)
Dim instance As INameCreationService
Dim name As String

instance.ValidateName(name)
C#
void ValidateName(
    string name
)
Visual C++
void ValidateName(
    String^ name
)
JScript
function ValidateName(
    name : String
)

Parameters

name
Type: System..::.String
The name to validate.
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.

This method is similar to IsValidName, except that this method throws an exception if the name is invalid. This allows implementers to provide detailed information in the exception message.

Examples

The following code example provides an example INameCreationService..::.IsValidName method implementation that 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 throws an exception if the string is not valid.

Visual Basic
' Throws an exception if the specified name does not contain 
' all valid character types.
Public Sub ValidateName(ByVal name As String) Implements INameCreationService.ValidateName
    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
                Throw New Exception("The name '" + name + "' is not a valid identifier.")
        End Select
    Next i
End Sub
C#
// Throws an exception if the specified name does not contain 
// all valid character types.
public void ValidateName(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:
                throw new Exception("The name '"+name+"' is not a valid identifier.");                
        }
    }
}
Visual C++
// Throws an exception if the specified name does not contain 
// all valid character types.
virtual void ValidateName( 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:
            throw gcnew Exception( String::Format( "The name '{0}' is not a valid identifier.", name ) );
      }
   }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker