473,802 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2583
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 BalanceLiabilit ies liabilitiesFiel d;

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

public partial class BalanceGeneral
{
private System.DatTime effectiveDateFi eld;
private int numberOfEmploye esField;
...
// followed by correspondig get/set property methods
public DateTime effectiveDate
{
// get/set for effectiveDateFi eld
}
...
}

public partial class BalanceAssets
{
...
private BalanceAssetsSt ocks 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 "constructe d".
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 BalanceAssetsSt ocks();
balance.assests .stocks.subMemb er1 = new
BalanceAssetsSt ocksSubMember1( );
....
balance.assests .stocks.subMemb er1.subMember11 .subMember111.s ubMember1111.so meValue
= 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.subMemb er1.subMember11 .subMember111.s ubMember1111.so meValue
= 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**********@gm x.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 overcomplicatin g 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.subMemb er1.subMember11 .subMember111.s ubMember1111.so meValue
= 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...@gm x.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
2247
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
3069
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, Gary Moore LP, LP Faded DC, Jeff Beck Strat, Morgan OM Acoustic,
15
1911
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
3411
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 was disastrous and hard to debug bug, as pointer was copied). Adding explicit operator = helped. See code snip below. I am not sure if this compiler behaviour is standard conformant or not. Any thoughts?
6
3555
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 base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
5
2295
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
8
2010
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 ---------------- Original Copy-----------------------
6
1326
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, 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...
9
3829
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 object? reference or const reference? deep copy or shallow copy is used in default assignment operator?)? I have the C++ Programming Book at hand, but can not find it from Index page.
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.