瞬間了解VB.NET集合存儲(chǔ)操作
大家都知道大多數(shù)程序處理對(duì)象集合而不是單個(gè)的對(duì)象。這里我們就詳細(xì)的介紹一下關(guān)于VB.NET集合存儲(chǔ)的一些問(wèn)題吧。對(duì)于集合數(shù)據(jù),首先創(chuàng)建一個(gè)數(shù)組(或者是其他類型的集合,比如ArrayList或HashTable),用對(duì)象填充,然后一個(gè)Serialize方法就可以序列化真?zhèn)€集合,是不是很簡(jiǎn)單?下面的例子,首先創(chuàng)建一個(gè)有兩個(gè)Person對(duì)象的ArrayList,然后序列化本身:
VB.NET集合存儲(chǔ)代碼:
- DimFSAsNewSystem.IO.FileStream_
- ("c:\test.txt",IO.FileMode.Create)
- DimBinFormatterAsNewBinary.BinaryFormatter()
- DimPAsNewPerson()
- DimPersonsAsNewArrayList
- P=NewPerson()
- P.Name="Person1"
- P.Age=35
- P.Income=32000
- Persons.Add(P)
- P=NewPerson()
- P.Name="Person2"
- P.Age=50
- P.Income=72000
- Persons.Add(P)
- BinFormatter.Serialize(FS,Persons)
以存儲(chǔ)序列化數(shù)據(jù)的文件為參數(shù),調(diào)用一個(gè)BinaryFormatter實(shí)例的Deserialize方法,就會(huì)返回一個(gè)對(duì)象,然后把它轉(zhuǎn)化為合適的類型。下面的代碼反序列化文件中的所有對(duì)象,然后處理所有的Person對(duì)象:
- FS=NewSystem.IO.FileStream_
- ("c:\test.txt",IO.FileMode.OpenOrCreate)
- DimobjAsObject
- DimPAsPerson(),RAsRectangle()
- Do
- obj=BinFormatter.Deserialize(FS)
- Ifobj.GetTypeIsGetType(Person)Then
- P=CType(obj,Person)
- 'ProcessthePobjext
- EndIf
- LoopWhileFS.Position<FS.Length-1
- FS.Close()
下面的例子調(diào)用Deserialize方法反序列化真?zhèn)€集合,然后把返回值轉(zhuǎn)換為合適的類型(Person):
- FS=NewSystem.IO.FileStream("c:\test.txt",IO.FileMode.OpenOrCreate)
- DimobjAsObject
- DimPersonsAsNewArrayList
- obj=CType(BinFormatter.Deserialize(FS),ArrayList)
- FS.Close()
上述就是一個(gè)關(guān)與VB.NET集合存儲(chǔ)的講解,希望大家可以記下來(lái)作為以后的復(fù)習(xí)資料。
【編輯推薦】