Microsoft.VisualBasic.Collection replacement?

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

Which vb.net object is the best match for the vb6 collection class?
Specifically, I would like to be able to access the Item property with an
index or a key string.

I wrote my own class that inherits from the collectionbase and uses an
arraylist to hold values, but i'm missing the Item(string) property. I
suppose I could iterate thru the collection and look for a string match, but
I thought there might be an easier way.

Thanks,

Craig
 
Craig Buchanan said:
Which vb.net object is the best match for the vb6 collection
class? Specifically, I would like to be able to access the Item
property with an index or a key string.

I wrote my own class that inherits from the collectionbase and uses
an arraylist to hold values, but i'm missing the Item(string)
property. I suppose I could iterate thru the collection and look for
a string match, but I thought there might be an easier way.

Microsoft.VisualBasic.Collection is not a part of VB6, it's a part of VB.NET
and the Framework. You can use it. It is not part of the compatibility
library that is only there for compatibility reasons.
 
Charles,
Unfortunately I do not do a lot of XML serialization, so I currently do not
have an answer for you, other then what I stated in the other thread.

Also are you referring to the XML serializer or the SOAP serializer? They
are different: one is used the same as BinarySerilazer, while the other is
used for Web Services, my understanding is that the SOAP serializer is more
limited as SOAP is more limited. HOwever I have not done a lot with either
SOAP or XML serialization.

Hope this helps
Jay
 
Cor,
A Dataset is used to contain a set or Data Rows.

A Collection is used to contain a set of objects.

Being an Object Oriented programmer, I find containing a set of objects is
much more powerful then containing a set of Data rows.

Unfortunately I do not have a good resource on when you should choose a Data
Centric (DataSet) approach or an Object Oriented approach. Although I find
keeping both options available is far better then saying. We are only using
one or the other!

Hope this helps
Jay
 
Hi Jay B,

Did you see me giving an answer on Charles last question about the XML
serializer, that is because I agree this with you (I am sad you even do
think I am not).

:-)

However in this thread he was asking about a collection and in my simple
mind it was a collection of data.

So I pointed Charles on the dataset, because that is a very easy collection
when it is all about data.

And I was in doubt if I would look for Charles for the XML serializer
because I did give someone an answer yesterday where that was in. However I
decided to do that this time not. I do not know of the complexebility of the
object Charles wants to serialize (which is mostly simple) and deserialize
(which is mostly difficult) . And because Charles always start telling the
question simple and than comes an "however I am using it in that way", I did
it this time not, but decided to wait for that last one.

:-)

Cor
 
Hi Cor

You have found me out ;-) I do tend to keep my questions simple to start
with because I don't want to put people off having a go at answering. I also
cut out all the stuff that I don't think is relevant, and which might set
people thinking of an inappropriate solution.

The serialiser question is a good example. With each post I have added some
extra detail. I generally like to find things out for myself, because I
think I will learn and remember better that way, so I also don't seek for
people to give me the full answer but just a nudge in the right direction.

In this case, though, I have seen many posts on this subject which allude to
the same thing, that there is no direct replacement for the
VisualBasic.Collection. That is a shame, because many people find that it
has exactly what they want from a collection.

As for serialising, I get the impression that not being able to serialise a
class that implements IDictionary (without hand-coding) is a short-coming,
and therefore may be added later by Microsoft. I hope so anyway.

Regards

Charles
 
Cor,
I was neither agreeing nor disagreeing with you! At the same time I was both
Agreeing & Disagreeing with you! ;-)

You made a statement, in the form of a question:
What is against the dataset it has everything you are asking
for build in in my opinion?

I was answering the question, The fact a dataset can only hold data rows is
something significant against it.

If you are a Data centric programmer then yes, using a DataSet is fine. So
in this regard I was agreeing with you.

However I find significantly more value in being an Object Oriented
programmer, so a lot of times a DataSet is too limiting. So in this regard I
was disagreeing with you.

Remember Object Oriented thinking involves organizing your app by
responsibility & behaviors, not simply I have a collection of data...

As I stated, using a Data centric approach is appropriate sometimes and
using an Object Oriented approach is appropriate .

Hope this helps
Jay
 
Hi Jay and Charles,

Thanks however, I did also thinking this over, two points do not forget the
extensibility of a dataset is limited by the very basic concepts of SQL.

(Jay you maybe know that I hate that "scripting" SQL language what you in my
opinion should do too because there is nothing less OOP than that in my
opinion) .

Another point I have thought on is that it is easy to put a serialized
string in a datarow. So my chalenge for the weekend is to fullfill the
question from Charles (I have seen samples so I hope it should go) and than
put them in a datarow, what is of course a piece of cake.

Hi Charles if this beneath is not what you mean, do than tell it to me?
--------------

XML serialization serializes only the public fields and property values of
an object into an XML stream. XML serialization does not include type
information. For example, if you have a Book object that exists in the
Library namespace, there is no guarantee that it will be deserialized into
an object of the same type.

---------------

I hope that I can do it with this and do not need the memstream because than
it is already ready.
\\\
Dim x As XmlSerializer = new XmlSerializer(GetType(MyClass))
Dim sw As New System.IO.StringWriter
x.Serialize(sw, MyObject)
ds.tables(0).rows(0)(0) = sw.tostring
///

I did not do this serialization of an object before so I am not sure I
succeed.

And Charles when it is with this hint ready for tomorrow European Time, do
not forget to tell it to me.

:-))

Cor
 
Hi Charles,

It was not that difficult.

It is a complete sample and I have placed also the dataset, and a combobox
to test.

The persons are from for us Benelux people from one of the famous Belgian
cartonists Vandersteen, that is because EricJ has often in his samples
Itemke, what is a kind of Dutch the Belgians always do and from which
Dutchman know it are Belgians.

I hope this helps you?

Cor

\\\
Private WithEvents combobox1 As New ComboBox
Private strt As Boolean
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim Belgen As New BelgenCollection
Belgen.Add(New Belg(New String() {"Lambiek", "Man", "Jenever"}))
Belgen.Add(New Belg(New String() {"Sidonia", "Vrouw", "Likeur"}))
Belgen.Add(New Belg(New String() {"Suske", "Jongen", "Cola"}))
Belgen.Add(New Belg(New String() {"Wiske", "Meisje", "Limonade"}))
Belgen.Add(New Belg(New String() {"Jerom", "Jongeman", "Bier"}))
Dim Serializer As New
Xml.Serialization.XmlSerializer(GetType(BelgenCollection))
Dim sw As New System.IO.StringWriter
Serializer.Serialize(sw, Belgen)
Dim ds As New DataSet
Dim dt As New DataTable("Jay")
Dim dc As New DataColumn("Charles")
'This part only for Jay and Charles if he wants
dt.Columns.Add(dc)
ds.Tables.Add(dt)
dt.Rows.Add(dt.NewRow)
ds.Tables(0).Rows(0)(0) = sw.ToString
ds.WriteXml("c:\test1\charles.xml")
Dim dsJay As New DataSet
dsJay.ReadXml("c:\test1\Charles.xml")
Dim Charles As String = dsJay.Tables(0).Rows(0)(0).ToString
'End Jay part
Dim Deserializer As New Xml.Serialization.XmlSerializer _
(GetType(BelgenCollection))
Dim sr As New System.IO.StringReader(Charles)
Dim reader As New System.Xml.XmlTextReader(sr)
Dim Vlamingen As BelgenCollection
Vlamingen = CType(Deserializer.Deserialize(reader), _
BelgenCollection)
Me.Controls.Add(combobox1)
combobox1.DataSource = Vlamingen
strt = True
End Sub
Private Sub combobox1_SelectedIndexChanged(ByVal _
sender As Object, ByVal e As System.EventArgs) _
Handles combobox1.SelectedIndexChanged
If strt = True Then
MessageBox.Show(DirectCast(combobox1.SelectedValue, Belg).drank)
End If
End Sub
End Class
///
\\\
Public Class Belg
Public Naam As String
Public sex As String
Public drank As String
Public Sub New()
End Sub
Public Sub New(ByVal fill As String())
Naam = fill(0)
sex = fill(1)
drank = fill(2)
End Sub
Public Overrides Function tostring() As String
Return Naam
End Function
End Class
Public Class BelgenCollection
Inherits System.Collections.CollectionBase
Default Public Overloads ReadOnly _
Property Item(ByVal index As Integer) As Belg
Get
Return DirectCast(list.Item(index), Belg)
End Get
End Property
Public Sub Add(ByVal aBelg As Belg)
List.Add(aBelg)
End Sub
End Class
 
Hi Cor

That looks great. I will have to try it later as I am supposed to be going
to Longleat any moment now, and then we have guests until Monday. But
thanks, and I will get back to you.

Charles
 
Hi Cor

I'm sorry I didn't reply before. When I came to try this I couldn't find
your response. I had forgotten that it was in a thread started by someone
else.

I have tried your solution a piece at a time, and have now achieved what I
set out to do.

The first thing I did was change my collection to inherit from
System.Collections.CollectionBase, as you have.

I tried to read my collection from my existing xml file and it just worked.
I did not need to implement the serialization code that you kindly provided.

The only remaining task was to sort the list.

That was remarkably straight forward as I only needed to add the following:

Public Sub Sort()

MyBase.InnerList.Sort(New MyComparer)

End Sub

and then write a class that implemented IComparer.

Thanks for you help and ideas. I am well on the way now :-)

Charles
 
Back
Top