473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Generically Referring to a Complex Class

I have built several kinds of complex classes that I work with in my program.
Storing them to disk is no problem because I just pass the instantiated
object to a SaveData method, accepting it as a generic "object". Then I use
reflection to iterate through the object and save the data.

But loading the data from disk seems to be anything but generic. I have
several challenges with this but here's one of the first:

public void OpenData (string filename, object dataModel)
{
Poll modelPoll = new Poll();
SysInfo modelSys = new SysInfo();

switch(dataModel.GetType().Name)
{
case "Poll":
modelPoll = (Poll) dataModel;
break;

case "SysInfo":
modelSys = (SysInfo) dataModel;
break;
}

// ... much more code here
}
Because I need to get into the details of each complex object, I found that
I needed to cast the generic object to the specific object type first. But
this requires a whole lot of un-generic code.

Two questions:
1. Is there a way around this?
2. Is there an entirely different approach I should be considering?

Note re #2: My storage format is XML. But I endlessly tried to get the
built-in XML reader methods working to no avail. I *think* this is because
my complex objects have several nested collections and nested classes. I
need them to best map out what I'm doing so I found it necessary to write my
own Save & Open XML parsers. They work fine but I just wish I could make
them more reusable.

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
9 1226
By now use serialization?

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
I have built several kinds of complex classes that I work with in my
program.
Storing them to disk is no problem because I just pass the instantiated
object to a SaveData method, accepting it as a generic "object". Then I
use
reflection to iterate through the object and save the data.

But loading the data from disk seems to be anything but generic. I have
several challenges with this but here's one of the first:

public void OpenData (string filename, object dataModel)
{
Poll modelPoll = new Poll();
SysInfo modelSys = new SysInfo();

switch(dataModel.GetType().Name)
{
case "Poll":
modelPoll = (Poll) dataModel;
break;

case "SysInfo":
modelSys = (SysInfo) dataModel;
break;
}

// ... much more code here
}
Because I need to get into the details of each complex object, I found
that
I needed to cast the generic object to the specific object type first.
But
this requires a whole lot of un-generic code.

Two questions:
1. Is there a way around this?
2. Is there an entirely different approach I should be considering?

Note re #2: My storage format is XML. But I endlessly tried to get the
built-in XML reader methods working to no avail. I *think* this is
because
my complex objects have several nested collections and nested classes. I
need them to best map out what I'm doing so I found it necessary to write
my
own Save & Open XML parsers. They work fine but I just wish I could make
them more reusable.

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #2
Peter, I don't understand your response whatsoever.

--
Robert W.
Vancouver, BC
www.mwtech.com

"Peter Rilling" wrote:
By now use serialization?


Nov 17 '05 #3

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
By now use serialization?
Peter, I don't understand your response whatsoever.


I think he's recommending that you have a look at the "serialization" topic
in the help file. The .Net framework already provides the ability to
serialize/deserialize objects (e.g. to/from disk). I've never used it, so I
can't say how easy it is or how well it works, but it's probably worth
looking into.
Nov 17 '05 #4
I meant "why not use serialization".

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
Peter, I don't understand your response whatsoever.

--
Robert W.
Vancouver, BC
www.mwtech.com

"Peter Rilling" wrote:
By now use serialization?

Nov 17 '05 #5
As I mentioned at the end of my original posting, I already extensively tried
using the built-in serialization but it didn't work.

Any thoughts about my two questions?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Peter Rilling" wrote:
I meant "why not use serialization".

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
Peter, I don't understand your response whatsoever.

--
Robert W.
Vancouver, BC
www.mwtech.com

"Peter Rilling" wrote:
By now use serialization?


Nov 17 '05 #6
You didn't mention anything about Serialization in your original message,
just that you were trying to do that manually using Reflection.

http://www.ondotnet.com/pub/a/dotnet...zationpt1.html

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
As I mentioned at the end of my original posting, I already extensively
tried
using the built-in serialization but it didn't work.

Any thoughts about my two questions?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Peter Rilling" wrote:
I meant "why not use serialization".

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
> Peter, I don't understand your response whatsoever.
>
> --
> Robert W.
> Vancouver, BC
> www.mwtech.com
>
>
>
> "Peter Rilling" wrote:
>
>> By now use serialization?
>


Nov 17 '05 #7
FYI,

The XmlSerializer does not handle circular references in an object graph you
would have to use the SoapFormatter class to serialize to Xml but this I
believe is being depricated in the next version of .Net.

HTH

Ollie Riches
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
As I mentioned at the end of my original posting, I already extensively
tried
using the built-in serialization but it didn't work.

Any thoughts about my two questions?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Peter Rilling" wrote:
I meant "why not use serialization".

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
> Peter, I don't understand your response whatsoever.
>
> --
> Robert W.
> Vancouver, BC
> www.mwtech.com
>
>
>
> "Peter Rilling" wrote:
>
>> By now use serialization?
>


Nov 17 '05 #8

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
I have built several kinds of complex classes that I work with in my program. Storing them to disk is no problem because I just pass the instantiated
object to a SaveData method, accepting it as a generic "object". Then I use reflection to iterate through the object and save the data.

But loading the data from disk seems to be anything but generic. Because I need to get into the details of each complex object, I found that I needed to cast the generic object to the specific object type first. But this requires a whole lot of un-generic code.


Do all of your "complex objects" derive from a common ancestor that you
created? If so, is it possible for this common ancestor to add some
"genericness" to the process?
Nov 17 '05 #9
Hi Robert -

When you say Serialization didnt work, you didnt mention why.
When I first tried it, I wrestled with it until I discovered that
one needs two objects to make it work: the formatter, and the
stream.

After declaring my class [Serializable], I save my objects to a
binary file using this code:

MyObject obj = new MyObject( args );

IFormatter formatter = new BinaryFormatter();

Stream stream = new FileStream( "MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None );

formatter.Serialize( stream, obj );

stream.Close();

I would expect the XML formatter to work similarly. The compiler
and the CLR are the things that do all the work; it only took me
the lines of code shown.

Deserialization is done equally simply:
IFormatter formatter = new BinaryFormatter();

Stream stream = new FileStream( "MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read );

MyObject obj = (MyObject) formatter.Deserialize( stream );

stream.Close();

Hope that helps

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
As I mentioned at the end of my original posting, I already extensively
tried
using the built-in serialization but it didn't work.

Any thoughts about my two questions?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Peter Rilling" wrote:
I meant "why not use serialization".

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com...
> Peter, I don't understand your response whatsoever.
>
> --
> Robert W.
> Vancouver, BC
> www.mwtech.com
>
>
>
> "Peter Rilling" wrote:
>
>> By now use serialization?
>


Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary...
21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
3
by: Arthur | last post by:
Spending the morning avoiding responsibilities, and seeing what it would take to color some complex numbers. class color_complex(complex): def __init__(self,*args,**kws):...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
2
by: BBM | last post by:
I have the following base class that uses Generics in its definition. MyList(of T, C) - T is a BindingList(Of C), C is the type in the list I have many implemented classes...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
4
by: call_me_anything | last post by:
I have different kind of data structures in different applications. The data structures are big and complex and I would like to print the members of each struct. Can we write a generic piece of...
1
by: Fred | last post by:
Hi. Can I make the first control (textfield or text area) get focus via onload, without referring to the object's name of id? Thanks.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.