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

new operator with deep class structure

Hi,

I generated C# classes from some complex XMLSchemas usind xsd.exe. The
result is that I get a class hierarchy that is quite deep (well for me
8 levels are deep). What I'm curiuos about is, that if I create an
instance of my top level element I still need to create instances of
all sub-elements. What would be the best way to do some sort of "deep
new" operator, that recursively creates instances for all sub-classes
(the complete structure consists of 89 different classes).

Lets say my class structure is like
Top
+-->Sub1
+-->Sub1_1
+-->Sub1_2
+-->Sub2
+-->Sub2_1
+-->Sub2_2

and instead of:
Top top = new Top();
top.sub1 = new Sub1();
top.sub1.sub1_1 = new Sub1_1();
top.sub1.sub1_2 = new Sub1_2();
....

just do:
Top top = new Top(true);

which will create instances for all sub-elements.

Of couse I could code a creator method that just does this. But maybe
this is not the best approach. So what would a experienced C#-guy do?
Any idea?

Thanks,
Stefan

P.S.: You probably noticed that I must be new to C#/FW2.0...

Jul 28 '07 #1
6 2547
Yes - you are right, I did not make this point very clear. The top
level class contains members which are of the sub class types. And the
sub-level members in turn contain members which are of typ sub-sub-
level class and so on. Sorry about the confusion.

Stefan

Jul 28 '07 #2
Pete and "PvdG42",

just to make it clear I provide a sample of what XSD.EXE created from
the XMLSchemas:

My top class is "Balance":

public partial class Balance
{
private BalanceGeneral generalField;
private BalanceAssets assetsField;
private BalanceLiabilities liabilitiesField;

// followed by correspondig get/set property methods
public BalanceGeneral general
{
get
{
return this.generalField;
}
set
{
this.generalField = value;
}
}
public BalanceAssets assets
{
// get/set for assetsField
}
}

public partial class BalanceGeneral
{
private System.DatTime effectiveDateField;
private int numberOfEmployeesField;
...
// followed by correspondig get/set property methods
public DateTime effectiveDate
{
// get/set for effectiveDateField
}
...
}

public partial class BalanceAssets
{
...
private BalanceAssetsStocks stocksField;
...
}

Again: there is no inheritance involved. Each class consist of member
fields that are either simple types (as DateTime, int or decimal) or
class types like BalanceGeneral or BalanceAssets. I have 89 classes
and about 160 simple type member fields (mostly of type decimal, the
"leaves" of my "balance tree") from which a balance is "constructed".
This tree like structure is at most 8 levels deep.

Now to fill in the effective date of a balance I need to create the
appropriate class instances for all members in the path to the "leaf"
effectiveDate:

Balance balance = new Balance();
balance.general = new BalanceGeneral();
balance.general.effectiveDate = DateTime.Today();
In the case of some decimal value in the path down to balance assets
this would be:

balance.assets = new BalanceAssets();
balance.assests.stocks = new BalanceAssetsStocks();
balance.assests.stocks.subMember1 = new
BalanceAssetsStocksSubMember1();
....
balance.assests.stocks.subMember1.subMember11.subM ember111.subMember1111.someValue
= 1000;

What I want to know If there is some "mechanism" so that I can avoid
all those "new" operators. I just do a "deep new" on the top level
class and all other instances for the sub-member fields are created,
too. Now I could code a constructor for my top level class Balance
that would do just this or a constructor for each of the 89 classes.
But as I said, this "bunch of classes" is generated from XMLSchemas.
If there is a future change in one of the schemas I will have to
carefully adapt my contructors. And more worse there are three other
data trees besides "Balance" for which I have to generate classes from
the corresponding XML schemas. So that's why I'm asking if there is
some way to automate this process, so that I can just code:

Balance balance = new Balance(true); // this is the constructor that
will create all member instances
balance.general.effectiveDate = DateTime.Today();
balance.assests.stocks.subMember1.subMember11.subM ember111.subMember1111.someValue
= 1000;

So before I start to code this constructor I wanted to ask some C#
expert if there is another approach that I didn't think of.
Stefan

Jul 29 '07 #3
ol**********@gmx.de wrote:
[...]
What I want to know If there is some "mechanism" so that I can avoid
all those "new" operators. I just do a "deep new" on the top level
class and all other instances for the sub-member fields are created,
too.
Well, what's your intent with respect to populating the contained classes?

For contained class instances, you must do _some_ initialization
explicitly. There's no automatic way for that to happen. By default
the reference to the contained instance is null, and it doesn't get to
be non-null unless you write some code that does that explicitly.

However, if you have some reasonable natural way to instantiate the
top-level node, then surely you also have some natural way to
instantiate the contained nodes, all the way down your containment tree.
For example, presumably you aren't instantiating the root node without
data to use to initialize it. In the same way, shouldn't you have data
before you go instantiating the contained nodes? And if so, wouldn't
the natural method be to simply go through the data as it exists,
initializing the relevant data structures as necessary?

I'm a bit confused by what appears to be an intent on your part to
initialize data structures for which you have no data yet. While I
think that I do finally understand the basic scenario you're dealing
with, it seems to me that the design requirement that the entire
containment tree be completely populated all at once is unnecessary and
is overcomplicating the implementation.
[...]
If there is a future change in one of the schemas I will have to
carefully adapt my contructors.
If there is a future change, won't you have to change all of your code?
For example:

And more worse there are three other
data trees besides "Balance" for which I have to generate classes from
the corresponding XML schemas. So that's why I'm asking if there is
some way to automate this process, so that I can just code:

Balance balance = new Balance(true); // this is the constructor that
will create all member instances
balance.general.effectiveDate = DateTime.Today();
balance.assests.stocks.subMember1.subMember11.subM ember111.subMember1111.someValue
= 1000;
The code above depends on the containment tree. If the schema changes,
that will presumably change the fields within the Balance class,
requiring you to change any code that refers to them. Thus, any code
referring to the properties "general" and "assets" needs to be changed
to address that change.

Basically, any change that would require some manual change to the way
contained members are initialized will also require some manual change
to the way they are accessed, and vice a versa. Trying to automatically
instantiate the entire tree with empty instances is not only likely to
be bad design, it's also solving a fairly minimal part of the overall
problem.

Pete
Jul 29 '07 #4
Pete,

thanks for taking time and sharing your thoughts.

The data comes from a database and needs to be filled into the data
structure. Then the data is serialized to XML and send to a web
service. The data structure in the database is "flat, although I have
created a key table that contains information about parent/child
relationship for balance elements. It also contains XMLSchema element
names and types (these correspond to C# class and property names). I
did this just for informational purposes but now I used this to
generate C# code for my "deep" constructor. The whole process is to
some degree automated by SQL queries that result in C# code. So now I
can create my data structure classes using XSD.EXE and add a "deep"
constructor. The amount of manually coding all this is minimized to a
bit of copy & paste. My solution is now ready to be apllied to the
other XMLschema objects. The C# code that loops through the balance
data and fills in the "flat" data into the data tree is also
generated. Thus any change in the XMLSchema can quickly be turned into
new code.

I don't know if this is really the best solution but it works for me.

Stefan

Jul 29 '07 #5
If you have a 8-level hierarchy tree, i think you'd better consider
more on your design. Inheritance is one of the core features of the OO
Language, but not recommended.

olympus_m...@gmx.de wrote:
Hi,

I generated C# classes from some complex XMLSchemas usind xsd.exe. The
result is that I get a class hierarchy that is quite deep (well for me
8 levels are deep). What I'm curiuos about is, that if I create an
instance of my top level element I still need to create instances of
all sub-elements. What would be the best way to do some sort of "deep
new" operator, that recursively creates instances for all sub-classes
(the complete structure consists of 89 different classes).

Lets say my class structure is like
Top
+-->Sub1
+-->Sub1_1
+-->Sub1_2
+-->Sub2
+-->Sub2_1
+-->Sub2_2

and instead of:
Top top = new Top();
top.sub1 = new Sub1();
top.sub1.sub1_1 = new Sub1_1();
top.sub1.sub1_2 = new Sub1_2();
...

just do:
Top top = new Top(true);

which will create instances for all sub-elements.

Of couse I could code a creator method that just does this. But maybe
this is not the best approach. So what would a experienced C#-guy do?
Any idea?

Thanks,
Stefan

P.S.: You probably noticed that I must be new to C#/FW2.0...
Jul 30 '07 #6
Garfilone,

again, this is not a class hierarchy based on inheritance, it's a data
hierarchy based on a bunch of classes. As these classes are generated
from an XMLSchema using XSD.EXE I have no influence on the desgin.
Also I think this quite normal with XMLSchema based data structures.

Stefan

Aug 5 '07 #7

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

Similar topics

2
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
15
by: Heiner | last post by:
#include <stdio.h> class A { public: virtual A & operator= (const A &); virtual void test(const A &); }; class B : public A
3
by: Ondrej Spanel | last post by:
I defined operator = using template member function for a template class. However compiler failed to recognize it is defined and created its own version (which was member-wise copy, and a result...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: john | last post by:
Hey guys, Quick question i have this code and what i want to do is create a deep copy of class B. Now I tried doing this with the new operator and pointers. Here's is the orignal ...
6
by: olympus_mons | last post by:
Hi, I generated C# classes from some complex XMLSchemas usind xsd.exe. The result is that I get a class hierarchy that is quite deep (well for me 8 levels are deep). What I'm curiuos about is,...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.