Solved

VB.Net Build a custom collection class

Posted on 2007-03-24
6
1,313 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 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