Solved

VB.Net Build a custom collection class

Posted on 2007-03-24
6
1,303 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 85

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 85

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 85

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 78
Exit the loop 4 42
EF5: Question about Metadata Artifact Processing 4 15
Sending receiving text messages in vb.net 15 32
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now