Some help designing a class for a C# beginner!

M

Mark

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.
 
W

William Stacey [MVP]

Use a private array or arraylist to hold ref to each child object. Did you
mean something else?
 
D

David McClarnon

Mark said:
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.
 
J

Jon Skeet [C# MVP]

David McClarnon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top