INameCreationService.CreateName(IContainer, Type) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立一個新名稱,該名稱對指定容器中所有元件皆有獨特性。
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
參數
- container
- IContainer
新物件被加入的容器。
- dataType
- Type
接收該名稱的物件的資料型態。
傳回
資料型別的名稱。
範例
以下程式碼範例提供了一個方法 INameCreationService.CreateName 實作範例。 該方法可根據指定類型名稱建立一個名稱,且該名稱與該容器內元件名稱相符。
// 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
備註
此方法回傳一個在指定容器內唯一存在的新物件名稱。
給實施者的注意事項
這類服務通常實作為從資料型別名稱建立唯一物件名稱,並常附加一個數字,使該名稱成為唯一識別碼。 舉例來說, ListBox1 一個 ListBox 物件。