Collection.Add 方法
將項目加入至 Collection 物件。
命名空間: Microsoft.VisualBasic
組件: Microsoft.VisualBasic (在 microsoft.visualbasic.dll 中)
語法
'宣告
Public Sub Add ( _
Item As Object, _
<OptionalAttribute> Optional Key As String = Nothing, _
<OptionalAttribute> Optional Before As Object = Nothing, _
<OptionalAttribute> Optional After As Object = Nothing _
)
'用途
Dim instance As Collection
Dim Item As Object
Dim Key As String
Dim Before As Object
Dim After As Object
instance.Add(Item, Key, Before, After)
public void Add (
Object Item,
[OptionalAttribute] string Key,
[OptionalAttribute] Object Before,
[OptionalAttribute] Object After
)
public:
void Add (
Object^ Item,
[OptionalAttribute] String^ Key,
[OptionalAttribute] Object^ Before,
[OptionalAttribute] Object^ After
)
public void Add (
Object Item,
/** @attribute OptionalAttribute() */ String Key,
/** @attribute OptionalAttribute() */ Object Before,
/** @attribute OptionalAttribute() */ Object After
)
public function Add (
Item : Object,
Key : String,
Before : Object,
After : Object
)
參數
- Item
必要項。任何型別的物件,可指定要加入到集合中的項目。
- Key
選擇項。唯一的 String 運算式,指定用來存取集合中這個新項目的索引鍵字串,藉以取代位置索引。
- Before
選擇項。運算式,可指定集合中的相對位置。要加入的項目會放置在集合中由 Before 引數識別的項目之前。如果 Before 為數值運算式,它必須是從 1 到集合的 Count 屬性 (Collection 物件) 值的一個數字。如果 Before 為 String 運算式,當所參考的項目加入到集合中時,它必須對應到指定的索引鍵字串。不能同時指定 Before 和 After。
- After
選擇項。運算式,可指定集合中的相對位置。要加入的項目會放置在集合中由 After 引數識別的項目之後。如果 After 為數值運算式,它必須是從 1 到集合的 Count 屬性值的一個數字。如果 After 為 String 運算式,當所參考的項目加入到集合中時,它必須對應到指定的索引鍵字串。不能同時指定 Before 和 After。
備註
如需詳細資訊,請參閱 Visual Basic 的主題 Add 方法 (集合物件)。
Before 或 After 引數必須參考集合中的現有項目,否則將會發生錯誤。
如果指定的 Key 值符合現有集合項目的索引鍵,也會發生錯誤。
範例
下列範例使用 Add 方法,將 child
物件 (稱為 child
的類別之執行個體,包含 Public 屬性 name
) 加入到稱為 family
的集合中。若要瞭解這項處理運作的方式,請建立一個使用兩個 Button 控制項的 Form,並將控制項的 Text 屬性設定為 Add 和 List。將 child
類別定義和 family
宣告加入表單程式碼中。為 [加入] 和 [清單] 按鈕修改 _Click
事件處理常式,如下所示。[加入] 按鈕可讓您加入子系。[清單] 按鈕可顯示所有子系的名稱。
Public Class child
Public name As String
Sub New(ByVal newName As String)
name = newName
End Sub
End Class
' Create a Collection object.
Private family As New Collection()
Private Sub addChild_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim newName As String
newName = InputBox("Name of new family member: ")
If newName <> "" Then
family.Add(New child(newName), newName)
End If
End Sub
Private Sub listChild_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
For Each aChild As child In family
MsgBox(aChild.name)
Next
End Sub
平台
Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
版本資訊
.NET Framework
支援版本:2.0、1.1、1.0
.NET Compact Framework
支援版本:2.0、1.0
請參閱
參考
Collection 類別
Collection 成員
Microsoft.VisualBasic 命名空間
其他資源
Add 方法 (集合物件)
Collection 物件 (Visual Basic)
Item 屬性 (Collection 物件)
Remove 方法 (Collection 物件)