Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1004811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:11:35+00:00 2026-05-16T08:11:35+00:00

Long term lurker, first time poster here. I have written a class to model

  • 0

Long term lurker, first time poster here.

I have written a class to model an object in vb.net, using vs2008 and framework 2.0. I am serializing the class to an XML file for persistent storage. I do this with a method in the class like this:

Public Sub SaveAs(ByVal filename As String)
    Dim writer As New Xml.Serialization.XmlSerializer(GetType(MyNamespace.MyClass))
    Dim file As New System.IO.StreamWriter(filename)
    writer.Serialize(file, Me)
    file.Close()
End Sub

I now want to do a similar thing but reading the class from file to the current instance, like this:

Public Sub New(ByVal filename As String)
    Dim reader = New Xml.Serialization.XmlSerializer(GetType(MyNamespace.MyClass))
    Dim file = New System.IO.StreamReader(FullPath)
    Me = CType(reader.Deserialize(file), MyNamespace.MyClass)
End Sub

However, I cannot assign anything to “Me”. I’ve tried creating a temporary object to hold the file contents then copying each property and field over to the current instance. I iterated over the properties (using Reflection), but this soon gets messy, dealing with ReadOnly collection properties, for example. If I just copy each property manually I will have to remember to modify the procedure whenever I add a property in the future, so that sounds like a recipe for disaster.

I know that I could just use a separate function outside the class but many built-in .NET classes can instantiate themselves from file e.g. Dim bmp As New Bitmap(filename As String) and this seems very intuitive to me.

So can anyone suggest how to load a class into the current instance in the Sub New procedure? Many thanks in advance for any advice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:11:35+00:00Added an answer on May 16, 2026 at 8:11 am

    I’d put a shared load function on the class, that returned the newly de-serialised object.
    e.g.

    Public Class MyClass
    ...
    Public shared Function Load(ByVal filename As String) as MyClass
        Dim reader = New Xml.Serialization.XmlSerializer(GetType(MyNamespace.MyClass)) 
        Dim file = New System.IO.StreamReader(FullPath) 
        Return CType(reader.Deserialize(file), MyNamespace.MyClass) 
    End Sub 
    End Class
    ...
    Dim mine as MyClass = MyClass.Load("MyObject.Xml");
    

    Hope this helps

    Alternatively,
    Encapsulate the data of your class in an inner, private class.
    The properties on your outer visible class delegate to the inner class.
    Then Serialising and De-serialising happens on the inner class, you can then have a ctor that takes the file name, de-serialises the inner hidden object, and assigns it to the classes data store.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been a long-term .NET developer, but dabble in Java and PHP development
Long time ago I made a socket based server application using the principle of
(Long time reader of SO, first time asking a q. I'm quite new to
I have a few long term processes and temporary processes in Python. While shell
I have a python web.py app with long (minutes) start-up time that I'd like
One long term project I have is working through all the exercises of SICP.
I'm writing mostly embedded code at work. We have a big long-term project that's
No tool is perfect, and I'm about to start several long-term projects using Trac,
My client is a long term rental agency. So we have Goods pk modelId
I have a string like this 4741:GREEN,CIRHOSIS,ORANGE,Long-term,GREEN,HIS B CHIC,4642:GREEN,CRHOSIS,GREEN,HSysk B CC, the sting contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.