Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties
Saving Data from Class Properties Saving Data from Class Properties
Saving Data from Class Properties
Go Back  Xtreme Visual Basic Talk > > > Saving Data from Class Properties


Reply
 
Thread Tools Display Modes
  #1  
Old 06-14-2011, 06:56 PM
TRSDOSBasic79's Avatar
TRSDOSBasic79 TRSDOSBasic79 is offline
Contributor
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
Default Saving Data from Class Properties


I currently have a class
Code:
Public Class Customer
    Public Property AccountNumber() As String
    Public Property PMDue() As String
    Public Property FirstName() As String
    Public Property LastName() As String
    Public Property CompanyName() As String
    Public Property CompanyContactName() As String
    Public Property Address() As String
    Public Property City() As String
    Public Property State() As String
    Public Property ZipCode() As String
    Public Property PhoneNumber() As String
    Public Property FaxNumber() As String
    Public Property CellPhoneNumber() As String
    Public Property EmailAddress() As String
End Class
Account Class is initialized as new customer. To save the data from each property in the Account Class, I'm currently looping thru the properties each time Account obtains new data. I tried to simplify the process by the following line...
Code:
CustomerStreamToWrite.WriteLine(Account)
This does not work as written. I figured it would be much faster. It did not write out the property data. It saved "WindowsApplication1.Customer" Not exactly what I wanted. Is there a method to get it to save the properties in a written out style by just using the class name?
Reply With Quote
  #2  
Old 06-14-2011, 09:15 PM
AtmaWeapon's Avatar
AtmaWeaponSaving Data from Class Properties AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I'm sleepy so this will be short on details, but I think I can hit the high points.

What happened when you called WriteLine()? If you understand that it's not so surprising what happened.

Let's start by asking, "What did you expect, and is that a reasonable expectation?" It's a game I like to play when I get confused by how an API works.

What you probably expected was for the value of each property to be written to a file. But what would it look like? Would it be comma-separated? How would that work for strings, which can contain commas and quotes? What about DateTime values, which have complicated formatting? What if your object references itself directly or indirectly, what should happen for that property? Should read-only properties be written? What about private variables set by the constructor? If you change the name of a property, should that cause an error? What if you rename many properties but maintain the same types, if they're written out in alphabetical order how can they be read back in properly? That's a lot of questions, and the answers are hard. That's usually a sign that you're expecting the wrong thing.

So ask the API if it's the wrong thing. Have a peek at StreamWriter.WriteLine(). It's got a lot of overloads, but none that take your Customer class. It'd be kind of creepy if it had one; how would MS know you were going to write it? The overload that takes an Object is the only one that fits. Its documentation says it does the same thing as the similar overload of Write(). Here's what that has to say:
Quote:
If the specified object is Nothing, no action is taken and no exception is thrown. Otherwise, the object's ToString method is called to produce the string representation, and the resulting string is then written to the output stream.
So .NET assumes if you want it to write a text representation of an object it knows nothing about, it has to convert that to a string. The most obvious way to do so is to call the object's ToString() method. Since you haven't overloaded it, you get the default implementation that prints the class's name.

If you want to save/load an object using StreamWriter, your only real option is to write out each property in some format you can read back. Using ToString() for this isn't that useful for a complex class because there's no analagous FromString() for StreamReader to lean on; you have to write property-by-property code for that one. Same with if you want to use XML: you have to do the writing and parsing yourself.

There is a feature called Serialization in .NET that's good if you just want a simple solution. You can serialize objects to an XML file or a binary file. For something to be serialized, it has to be marked with the Serializable attribute and any properties it has have to be of types that are serializable. When you serialize an object to a stream, .NET saves each property in whatever format it feels like; there's a deserialization operation that does the same thing.

My wrists are wrecked and I'm practically passing out right now, so I'll leave you with some good examples written by MS. BinaryFormatter is the class for binary serialization/deserialization, and its example is pretty clear. XmlSerializer serializes to XML and has an example that's a little better. If you were confused when I said a class has to be marked with the Serializable attribute, its documentation has an example.

I don't give serialization enough credit. It has some limitations, but they're not limitations I encounter frequently. I don't know why I waste so much time writing my own serialization code. I think I just like doing it.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 06-18-2011, 07:49 PM
TRSDOSBasic79's Avatar
TRSDOSBasic79 TRSDOSBasic79 is offline
Contributor
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
Default

I dumped the serialization idea in favor of a text format for my data. The data is stored in the file separated by the "~". That symbol will never be used in any of my apps and I am sure of it. I've disabled that key's events so there will be no errors of typing it. I used an example from a thread here on the string.split function to parse my data when read from the file. Works very nice and shortened my code by 65 lines.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties Saving Data from Class Properties
Saving Data from Class Properties
Saving Data from Class Properties
 
Saving Data from Class Properties
Saving Data from Class Properties
 
-->