Solved

VB.Net Build a custom collection class

Posted on 2007-03-24
6
1,309 Views
Last Modified: 2013-11-26
I'm using VB.net 2005.

I need to build a class to hold a collection of a custom class I built.

I have class Profile which holds profile information.
I need to create a class called profileCollection that holds a group of Profiles.

The reason for this is to allow me to use a serializer to spit out the profileCollection to an xml file and the de-serializer to pull in an entire profileCollection from an xml file.

It is my understanding that a generic type would be the way to go.
0
Comment
Question by:Brothernod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18787262
The Generic List(Of T) can be serialized...

Imports System.IO
Imports System.Xml.Serialization

Public Class Form1

    Public Class Profile
        Public ID As String
        Public Stamp As DateTime
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim p As Profile
        Dim myList As New List(Of Profile)

        p = New Profile
        p.ID = "a"
        p.Stamp = DateTime.Now
        myList.Add(p)

        p = New Profile
        p.ID = "b"
        p.Stamp = DateTime.Now
        myList.Add(p)

        p = New Profile
        p.ID = "c"
        p.Stamp = DateTime.Now
        myList.Add(p)

        Dim mySerializer As XmlSerializer = New XmlSerializer(myList.GetType)
        Dim myWriter As StreamWriter = New StreamWriter("c:\mydata.xml")
        mySerializer.Serialize(myWriter, myList)
        myWriter.Close()
    End Sub

End Class

This generated the following XML file:
 <?xml version="1.0" encoding="utf-8" ?>
- <ArrayOfProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Profile>
  <ID>a</ID>
  <Stamp>2007-03-24T21:06:01.5483472-07:00</Stamp>
  </Profile>
- <Profile>
  <ID>b</ID>
  <Stamp>2007-03-24T21:06:01.5483472-07:00</Stamp>
  </Profile>
- <Profile>
  <ID>c</ID>
  <Stamp>2007-03-24T21:06:01.5483472-07:00</Stamp>
  </Profile>
  </ArrayOfProfile>
0
 
LVL 1

Author Comment

by:Brothernod
ID: 18787388
That worked well, but deserialize isnt working quite as I thought and I think it's due to the profile class declaration. (Why doesn't it have a new sub?)

Dim inputList As Generic.List(Of profile)
Dim dehydrate As FileStream = New FileStream("mydata.xml", FileMode.Open)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(Generic.List(Of profile)))

inputList = serialize.Deserialize(dehydrate)
For Each profile In inputList

Next


profile in the for each line gets underlined and it says " 'profile' is a type and cannot be used as an expression"

A little assistance please? I want to step through each profile in the array and put it's name in a listbox (in your example, the ID).
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 18787518
Try this...

Imports System.IO
Imports System.Xml.Serialization

Public Class Form1

    Public Class Profile
        Public ID As String
        Public Stamp As DateTime
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim p As Profile
        Dim myList As New List(Of Profile)

        p = New Profile
        p.ID = "a"
        p.Stamp = DateTime.Now
        myList.Add(p)

        p = New Profile
        p.ID = "b"
        p.Stamp = DateTime.Now
        myList.Add(p)

        p = New Profile
        p.ID = "c"
        p.Stamp = DateTime.Now
        myList.Add(p)

        Dim mySerializer As New XmlSerializer(myList.GetType)

        Dim myWriter As New StreamWriter("c:\mydata.xml")
        mySerializer.Serialize(myWriter, myList)
        myWriter.Close()

        myList.Clear()

        Dim myReader As New StreamReader("c:\myData.xml", True)
        myList = mySerializer.Deserialize(myReader)
        myReader.Close()

        For Each prf As Profile In myList
            Debug.Print(prf.ID & " " & prf.Stamp)
        Next
    End Sub

End Class
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:Brothernod
ID: 18788365
That worked thanks :)

Just one last thing in regards to the classes.  How come you don't have a New or some get/set type stuff in the class?  Is that just for simplicity in the example?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18788497
That's correct...just to keep it simple for illustrative purposes.  It would be much nicer to have constructors that receive parameters and properties if you need to validate the data and ensure integrity of the unit as a whole.
0
 
LVL 1

Author Comment

by:Brothernod
ID: 18788555
Ok.
Thanks again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question