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

Creates a new name that is unique to all components in the specified container.

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

Visual Basic (Declaration)
Function CreateName ( _
    container As IContainer, _
    dataType As Type _
) As String
Visual Basic (Usage)
Dim instance As INameCreationService
Dim container As IContainer
Dim dataType As Type
Dim returnValue As String

returnValue = instance.CreateName(container, _
    dataType)
C#
string CreateName(
    IContainer container,
    Type dataType
)
Visual C++
String^ CreateName(
    IContainer^ container, 
    Type^ dataType
)
JScript
function CreateName(
    container : IContainer, 
    dataType : Type
) : String

Parameters

container
Type: System.ComponentModel..::.IContainer
The container where the new object is added.
dataType
Type: System..::.Type
The data type of the object that receives the name.

Return Value

Type: System..::.String
A unique name for the data type.
Remarks

This method returns a name for the new object that is unique within the specified container.

Notes to Implementers:

This type of service is often implemented to create a unique object name from the name of the data type, often appended with a number that allows the name to be a unique identifier. For example, ListBox1 for a ListBox object.

Examples

The following code example provides an example INameCreationService..::.CreateName method implementation. The method can create a name based on the name of a specified type that is unique to the names of the components within the specified container.

Visual Basic
' 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
C#
// Creates an identifier for a particular data type that does not conflict 
// with the identifiers of any components in the specified collection.
public string CreateName(System.ComponentModel.IContainer container, System.Type dataType)
{
    // Create a basic type name string.
    string baseName = dataType.Name;
    int uniqueID = 1;

    bool unique = false;            
    // Continue to increment uniqueID numeral until a 
    // unique ID is located.
    while( !unique )
    {
        unique = true;
        // Check each component in the container for a matching 
        // base type name and unique ID.
        for(int i=0; i<container.Components.Count; i++)
        {
            // Check component name for match with unique ID string.
            if( container.Components[i].Site.Name.StartsWith(baseName+uniqueID.ToString()) )
            {
                // If a match is encountered, set flag to recycle 
                // collection, increment ID numeral, and restart.
                unique = false;
                uniqueID++;
                break;
            }
        }
    }

    return baseName+uniqueID.ToString();
}
Visual C++
// Creates an identifier for a particular data type that does not conflict 
// with the identifiers of any components in the specified collection.
virtual String^ CreateName( System::ComponentModel::IContainer^ container, System::Type^ dataType )
{
   // Create a basic type name string.
   String^ baseName = dataType->Name;
   int uniqueID = 1;
   bool unique = false;

   // Continue to increment uniqueID numeral until a 
   // unique ID is located.
   while (  !unique )
   {
      unique = true;

      // Check each component in the container for a matching 
      // base type name and unique ID.
      for ( int i = 0; i < container->Components->Count; i++ )
      {
         // Check component name for match with unique ID string.
         if ( container->Components[ i ]->Site->Name->StartsWith( String::Concat( baseName, uniqueID ) ) )
         {
            // If a match is encountered, set flag to recycle 
            // collection, increment ID numeral, and restart.
            unique = false;
            uniqueID++;
            break;
         }
      }
   }

   return String::Concat( baseName, uniqueID );
}
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