This documentation is archived and is not being maintained.

INameCreationService Interface

Provides a service that can generate unique names for objects.

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

'Declaration
Public Interface INameCreationService
'Usage
Dim instance As INameCreationService

A DesignerLoader can implement this service to provide a way for a designer to create new, unique names for objects. If this service is not available, the designer uses a default implementation.

The following example code provides an example INameCreationService implementation. The service can create a unique name based on a type that does not match any names in the specified container. It can also validate a specified name string.

Imports System
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization

Public Class NameCreationService
    Implements System.ComponentModel.Design.Serialization.INameCreationService

    Public Sub New()
    End Sub 

    ' Creates an identifier for a particular data type that does not conflict  
    ' with the identifiers of any components in the specified collection 
    Public Function CreateName(ByVal container As System.ComponentModel.IContainer, ByVal dataType As System.Type) As String Implements INameCreationService.CreateName
        ' Create a basic type name string 
        Dim baseName As String = dataType.Name
        Dim uniqueID As Integer = 1

        Dim unique As Boolean = False 
        ' Continue to increment uniqueID numeral until a unique ID is located. 
        While Not unique
            unique = True 
            ' Check each component in the container for a matching  
            ' base type name and unique ID. 
            Dim i As Integer 
            For i = 0 To container.Components.Count - 1
                ' Check component name for match with unique ID string. 
                If container.Components(i).Site.Name.StartsWith((baseName + uniqueID.ToString())) Then 
                    ' If a match is encountered, set flag to recycle  
                    ' collection, increment ID numeral, and restart.
                    unique = False
                    uniqueID += 1
                    Exit For 
                End If 
            Next i
        End While 

        Return baseName + uniqueID.ToString()
    End Function 

    ' 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 

    ' 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 

End Class

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: