473,222 Members | 1,723 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,222 software developers and data experts.

Dynamic casting to object base class

Zac
Alright anyone who has 2c throw it in...

I am working through a custom xml serializer and have come upon a
conundrum, given our class design.

The interface implemented on the base class (base for all business
entities) dictates that the implementing class expose a Dirty property
(for state).

The base class (that actually manages state, once for all inheritors)
maintains an array of Dirty (bools) and each class down the chain
knows (internally) the index in the array based on its level in the
heirarchy.

StateMgr (bool[] dirty)
- BaseEntity
- Person (dirty[0] = false)
- Employee (dirty[1] = false)
- Manager (dirty[2] = true)

Given the above, each entity overrides the Dirty property from the
base and via the override hides the indexing of the Dirty property (so
I could call manager.Dirty = true instead of manager.Dirty[2] = true).

Now in the serializer, I want to be able to serialize all of the
exposed properties PLUS the Dirty value for each of the entities in
the chain. In the resulting xml I would have something like <Manager
Dirty="true false false" ... indicating that only the properties in my
StateMgr need be updated.

Getting long - but stick with me. The Dirty property is exposed
through the interface so if I cast my Manager to an Employee the Dirty
property through that interface is false (given the above), and in
turn if I cast down to a Person then I also get the dirty property at
the Person index (0) which is false.

The problem is, I will only have "some object" when I serialize. I can
use the GetType().BaseType to see the base type - but I know of no way
to actually cast to that type (i.e. Employee tempObject = manager as
Employee) but using dyanmic properties like (Employee tempObject =
manager as Manager.GetType().BaseType)...

....therein lies the problem, that I'm dealing with...

....thoughts? questions? snide remarks? dirty jokes?
Nov 15 '05 #1
2 2530
Zac,

Just curious, what is the reason that you are not using the
SoapFormatter to serialize the contents of your class? If you used that,
this would be a moot point.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zac" <zn*****@hotmail.com> wrote in message
news:7e************************@posting.google.com ...
Alright anyone who has 2c throw it in...

I am working through a custom xml serializer and have come upon a
conundrum, given our class design.

The interface implemented on the base class (base for all business
entities) dictates that the implementing class expose a Dirty property
(for state).

The base class (that actually manages state, once for all inheritors)
maintains an array of Dirty (bools) and each class down the chain
knows (internally) the index in the array based on its level in the
heirarchy.

StateMgr (bool[] dirty)
- BaseEntity
- Person (dirty[0] = false)
- Employee (dirty[1] = false)
- Manager (dirty[2] = true)

Given the above, each entity overrides the Dirty property from the
base and via the override hides the indexing of the Dirty property (so
I could call manager.Dirty = true instead of manager.Dirty[2] = true).

Now in the serializer, I want to be able to serialize all of the
exposed properties PLUS the Dirty value for each of the entities in
the chain. In the resulting xml I would have something like <Manager
Dirty="true false false" ... indicating that only the properties in my
StateMgr need be updated.

Getting long - but stick with me. The Dirty property is exposed
through the interface so if I cast my Manager to an Employee the Dirty
property through that interface is false (given the above), and in
turn if I cast down to a Person then I also get the dirty property at
the Person index (0) which is false.

The problem is, I will only have "some object" when I serialize. I can
use the GetType().BaseType to see the base type - but I know of no way
to actually cast to that type (i.e. Employee tempObject = manager as
Employee) but using dyanmic properties like (Employee tempObject =
manager as Manager.GetType().BaseType)...

...therein lies the problem, that I'm dealing with...

...thoughts? questions? snide remarks? dirty jokes?

Nov 15 '05 #2

Hi,

Thanks for posting in this group.
Why you can not cast your child class intance to your parent class?(i.e.
Employee tempObject = manager as Employee)
What is the problem about this cast?

Btw: I think your design is somewhat strange. If your base class managed
all the state of dirty variable in the array. If someone inherit a new
child class from your base class. How does your base class know this, and
add this inherited class's "dirty" value in the array?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: zn*****@hotmail.com (Zac)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Dynamic casting to object base class
| Date: 18 Nov 2003 08:12:48 -0800
| Organization: http://groups.google.com
| Lines: 45
| Message-ID: <7e************************@posting.google.com>
| NNTP-Posting-Host: 160.79.200.162
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1069171968 5020 127.0.0.1 (18 Nov 2003
16:12:48 GMT)
| X-Complaints-To: gr**********@google.com
| NNTP-Posting-Date: Tue, 18 Nov 2003 16:12:48 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnew s1.google.com!no
t-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:200192
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Alright anyone who has 2c throw it in...
|
| I am working through a custom xml serializer and have come upon a
| conundrum, given our class design.
|
| The interface implemented on the base class (base for all business
| entities) dictates that the implementing class expose a Dirty property
| (for state).
|
| The base class (that actually manages state, once for all inheritors)
| maintains an array of Dirty (bools) and each class down the chain
| knows (internally) the index in the array based on its level in the
| heirarchy.
|
| StateMgr (bool[] dirty)
| - BaseEntity
| - Person (dirty[0] = false)
| - Employee (dirty[1] = false)
| - Manager (dirty[2] = true)
|
| Given the above, each entity overrides the Dirty property from the
| base and via the override hides the indexing of the Dirty property (so
| I could call manager.Dirty = true instead of manager.Dirty[2] = true).
|
| Now in the serializer, I want to be able to serialize all of the
| exposed properties PLUS the Dirty value for each of the entities in
| the chain. In the resulting xml I would have something like <Manager
| Dirty="true false false" ... indicating that only the properties in my
| StateMgr need be updated.
|
| Getting long - but stick with me. The Dirty property is exposed
| through the interface so if I cast my Manager to an Employee the Dirty
| property through that interface is false (given the above), and in
| turn if I cast down to a Person then I also get the dirty property at
| the Person index (0) which is false.
|
| The problem is, I will only have "some object" when I serialize. I can
| use the GetType().BaseType to see the base type - but I know of no way
| to actually cast to that type (i.e. Employee tempObject = manager as
| Employee) but using dyanmic properties like (Employee tempObject =
| manager as Manager.GetType().BaseType)...
|
| ...therein lies the problem, that I'm dealing with...
|
| ...thoughts? questions? snide remarks? dirty jokes?
|

Nov 15 '05 #3

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

Similar topics

5
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
9
by: maynard | last post by:
How legit/acceptable is this bit of code? class Base{ Base(){...} Base(const Base& b){...} }; class D1: public Base { ....
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
2
by: Mike Stevenson | last post by:
Hi. I'm in the process of re-learning all the C++ I forgot from college, and I'm starting to get into some virgin (or at least only a couple times) territory. I have some questions about casting...
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
6
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.