Solved

VB.Net Build a custom collection class

Posted on 2007-03-24
6
1,297 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

14 Experts available now in Live!

Get 1:1 Help Now