Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.Net Build a custom collection class

Posted on 2007-03-24
6
Medium Priority
?
1,324 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
  • 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 1000 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

810 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