Share via


INameCreationService.CreateName(IContainer, Type) Método

Definición

Crea un nombre nuevo que es único para todos los componentes del contenedor especificado.

public:
 System::String ^ CreateName(System::ComponentModel::IContainer ^ container, Type ^ dataType);
public string CreateName (System.ComponentModel.IContainer container, Type dataType);
public string CreateName (System.ComponentModel.IContainer? container, Type dataType);
abstract member CreateName : System.ComponentModel.IContainer * Type -> string
Public Function CreateName (container As IContainer, dataType As Type) As String

Parámetros

container
IContainer

Contenedor al que se agrega el objeto nuevo.

dataType
Type

Tipo de datos del objeto que recibe el nombre.

Devoluciones

Nombre único para el tipo de datos.

Ejemplos

En el ejemplo de código siguiente se proporciona una implementación de método de ejemplo INameCreationService.CreateName . El método puede crear un nombre basado en el nombre de un tipo especificado que sea único para los nombres de los componentes dentro del contenedor especificado.

// 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.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
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

Comentarios

Este método devuelve un nombre para el nuevo objeto que es único dentro del contenedor especificado.

Notas a los implementadores

Este tipo de servicio se implementa a menudo para crear un nombre de objeto único a partir del nombre del tipo de datos, a menudo anexado con un número que permite que el nombre sea un identificador único. Por ejemplo, ListBox1 para un ListBox objeto .

Se aplica a