Freigeben über


INameCreationService.CreateName-Methode

Erstellt einen neuen Namen, der für sämtliche Komponenten im angegebenen Container eindeutig ist.

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

Syntax

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

returnValue = instance.CreateName(container, dataType)
string CreateName (
    IContainer container,
    Type dataType
)
String^ CreateName (
    IContainer^ container, 
    Type^ dataType
)
String CreateName (
    IContainer container, 
    Type dataType
)
function CreateName (
    container : IContainer, 
    dataType : Type
) : String

Parameter

  • container
    Der Container, in dem das neue Objekt hinzugefügt wird.
  • dataType
    Der Datentyp des Objekts, das den Namen erhält.

Rückgabewert

Ein eindeutiger Name für den Datentyp.

Hinweise

Diese Methode gibt einen im angegebenen Container eindeutigen Namen für das neue Objekt zurück.

Hinweise für Implementierer Dieser Diensttyp wird häufig implementiert, um einen eindeutigen Objektnamen aus dem Namen des Datentyps zu erstellen, an den meist eine Nummer angefügt wird, sodass der Name ein eindeutiger Bezeichner ist. Beispielsweise ListBox1 für ein ListBox-Objekt.

Beispiel

Im folgenden Codebeispiel wird eine Beispielimplementierung der INameCreationService.CreateName-Methode bereitgestellt. Die Methode kann auf Grundlage des Namens eines angegebenen Typs, der für die Namen der Komponenten im angegebenen Container eindeutig ist, einen Namen erstellen.

' 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
// 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();
}
// 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 );
}
// 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.get_Name();
    int uniqueID = 1;
    boolean 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.get_Components().get_Count(); i++) {
            // Check component name for match with unique ID string.
            if (container.get_Components().get_Item(i).get_Site().
                get_Name().StartsWith((baseName + ((Int32)uniqueID).
                ToString()))) {
                // If a match is encountered, set flag to recycle 
                // collection, increment ID numeral, and restart.
                unique = false;
                uniqueID++;
                break;
            }
        }
    }
    return (baseName + ((Int32)uniqueID).ToString());
} //CreateName

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

INameCreationService-Schnittstelle
INameCreationService-Member
System.ComponentModel.Design.Serialization-Namespace