By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Some help designing a class for a C# beginner!

P: n/a
I've been writing code in VB and VBScript for several
years and I'm finally caving in to .net! I've decided to
work with C# as I've always understood the basic concepts
of C/C++ but never gained much experience in writing
actual code.

I'm trying to work out the best way to implement a class
to meet the following requirements.

I have three types of object, lets call them Object1,
Object2 and Object3. Each of these has their own unique
values (i.e. Object1.Name, Object2.Name,
Object3.Location) but there is also a set of
relationships where an Object1 can contain one or more
Object2's and each Object2 could contain one or more
Object3's. It's like a treeview hierarchy.

Basically, I want to work out the best way to implement
this in a manner that the relationships between the
objects would be easy to manange. I'd also like to be
able to iterate through all of the objects belonging to
another object. For example, for a particular instance
of Object2, I'd like to list each related Object3 -
something like foreach(Object3 someObject in myObject2).

I hope I've explained that well enough. In short, I need
to model some hierarchical data and I want to be able to
iterate through it quickly and easily.

Thanks in advance,
Mark.
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Use a private array or arraylist to hold ref to each child object. Did you
mean something else?

--
William Stacey, MVP

"Mark" <mt*************@hotmailspammenot.com> wrote in message
news:0c****************************@phx.gbl...
I've been writing code in VB and VBScript for several
years and I'm finally caving in to .net! I've decided to
work with C# as I've always understood the basic concepts
of C/C++ but never gained much experience in writing
actual code.

I'm trying to work out the best way to implement a class
to meet the following requirements.

I have three types of object, lets call them Object1,
Object2 and Object3. Each of these has their own unique
values (i.e. Object1.Name, Object2.Name,
Object3.Location) but there is also a set of
relationships where an Object1 can contain one or more
Object2's and each Object2 could contain one or more
Object3's. It's like a treeview hierarchy.

Basically, I want to work out the best way to implement
this in a manner that the relationships between the
objects would be easy to manange. I'd also like to be
able to iterate through all of the objects belonging to
another object. For example, for a particular instance
of Object2, I'd like to list each related Object3 -
something like foreach(Object3 someObject in myObject2).

I hope I've explained that well enough. In short, I need
to model some hierarchical data and I want to be able to
iterate through it quickly and easily.

Thanks in advance,
Mark.


Nov 16 '05 #2

P: n/a
Mark wrote:
I've been writing code in VB and VBScript for several
years and I'm finally caving in to .net! I've decided to
work with C# as I've always understood the basic concepts
of C/C++ but never gained much experience in writing
actual code.

I'm trying to work out the best way to implement a class
to meet the following requirements.

I have three types of object, lets call them Object1,
Object2 and Object3. Each of these has their own unique
values (i.e. Object1.Name, Object2.Name,
Object3.Location) but there is also a set of
relationships where an Object1 can contain one or more
Object2's and each Object2 could contain one or more
Object3's. It's like a treeview hierarchy.

Basically, I want to work out the best way to implement
this in a manner that the relationships between the
objects would be easy to manange. I'd also like to be
able to iterate through all of the objects belonging to
another object. For example, for a particular instance
of Object2, I'd like to list each related Object3 -
something like foreach(Object3 someObject in myObject2).

I hope I've explained that well enough. In short, I need
to model some hierarchical data and I want to be able to
iterate through it quickly and easily.

Thanks in advance,
Mark.


I would get a good book on object orientation before you start.

Design is everything in .NET. Design your classes to be small (i.e. less
than approximately 300 lines of code) and reuse your code.

By reusing your code (i.e. never write anything twice) you'll actually
be improving the efficiency of your code.

Because everything is JITted (i.e. compiled at runtime) then if you have
routines which are called all the time they need to be in their own
classes.

A typical example of this would be to get the extension from a file name.

If you duplicate code all over the place to get the extension then this
code has to be compiled seperately for each function.

However if your functions call a single function to get the extension
then this only needs to be compiled once. And from that point onwards
your application will be speedy every time it wants to get an extension
of a filename.

Get the idea ? I hope so.

Darwen.
Nov 16 '05 #3

P: n/a
David McClarnon <dm********@ntlworld.com> wrote:
I would get a good book on object orientation before you start.

Design is everything in .NET. Design your classes to be small (i.e. less
than approximately 300 lines of code) and reuse your code.
While small classes are generally good, there's no point in
artificially constraining yourself to about 300 lines of code.
By reusing your code (i.e. never write anything twice) you'll actually
be improving the efficiency of your code.

Because everything is JITted (i.e. compiled at runtime) then if you have
routines which are called all the time they need to be in their own
classes.
What makes you say that?
A typical example of this would be to get the extension from a file name.

If you duplicate code all over the place to get the extension then this
code has to be compiled seperately for each function.
That's an issue of code reuse more than splitting classes artificially
based on size.
However if your functions call a single function to get the extension
then this only needs to be compiled once. And from that point onwards
your application will be speedy every time it wants to get an extension
of a filename.


I don't believe JIT compilation speed is an issue for most programs
beyond the first few seconds of operation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.