473,791 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class within class, design stuff, need expert opinion

Hi to all!

I am writing an implentation of the a-star algorithm in c#. My message is
going to be a little bit long, but this is in order to be as specific as
possible. My question has nothing to do with the algorithm. It is related to
OOP in c#.

I have created a class named astar.cs. An instance of the class among other,
contains a instance of a class named Input to store the input parameters of
the algorithm and an instance of a class named Result to store the results
of the algorithm. Here is an example of the final result:

------------------------------------------
CAstar astar = new CAstar();
astar.Input.Sta rtPoint = new Point(0,0);
astar.Input.Goa lPoint = new Point(500,500);
astar.Space.Add polygon(100,100 ,100,200,200,20 0,200,100);
astar.Space.Add polygon(... ... ...);
astar.Solve();
if (astar.Result.S uccessful) { Console.writeln (astar.Result.E xpandedNodes) .
....};
------------------------------------------

I like the fact that input parameters are not messed with results. Results
as accessed as astar.Results.* and input parameters of the algorithm are
accessed as astar.Input.*

In the example above: Input, Space, Result are classes within the class
CAstar, eg:

------------------------------------------
public class CAStar {
.....public class clsResults {...}
.....public clsResults Results = new clsResults();
------------------------------------------

The CAstar uses events to notify controls (textboxes and panels) when data
are changed. I use handlers to capture those events and update the controls
so that I can view the progress of the algorithm. Here is an example:

------------------------------------------ (in a win form):
astar.ProgressM essageHandlers += new CAStar.StringEv entHandler(LogH andler);
(...)
private void LogHandler(Obje ct sender, string e) {txtData.Text += "\r\n" +
e;}
------------------------------------------

In the example above, whenever astar has something to report, it calls the
ProgressMessage Handlers (declared as an event). The LogHandler get the text
sent, and presents it to the screen.

So here is my problem.

If an input parameter is changed after astar.solve() is called ( like:
astar.Input.Sta rtPoint = new Point(0,0); ) I want the data in the textboxes
and graphic panels to be updated. BUT, StartPoint is a member of the class
Input, which does not have access to the events of the astar class (since
astar contains Input and not the other way arround). So I cannot call
ProgressMessage Handlers from within the class Input.

BUT, if I add a property in astar class to set the astar startpoint (a
property thru which I could call ProgressMessage Handlers in set{} section),
I lose the versatility of the syntax: astar.Input.* . I would have to type
astar.SetGoalPo int, astar.SetStartP oint, astarGetExpande dNodes and so on
which leads to a complete chaos of properties.

Is there a way to avoid this? I want like to keep access of input parameters
and results separated but I want CAstar to automatically notify controls to
update their data.

Thanks to all who read all of this. I didn't know how to write all of this
stuff in a more compact way.(!)
Dec 10 '06 #1
3 2279
How about Input and Results having a reference to the creating CAstar?
If CAstar creates them then this could be as simple as (incomplete
code):

class CAstar {
public CAstar() {
input = new clsInput(this);
results = new clsResults(this );
}
// ...
public class clsInput {
private readonly CAstar star;
public clsInput(CAstar star) {
this.star = star;
}
}
}

If the caller acutally creates the input instance, then you could do
this via a method on CAStar:
class CAstar {
// ...
public clsInput CreateInput() {
return new clsInput(this);
}
}

Either way, this then allows CAstar to have a *private* method e.g.
OnProgressMessa geHandlers, which clsInput can then call (for instance
in the property setter) star.OnProgress MessageHandlers ({any args
necessary}), since inner classes can call private methods of outer
classes:

public class clsInput {
private string someField;
public string SomeProperty {
get {return someField;}
set {
if(value!=SomeP roperty) {
someField = value;
star.OnProgress MessageHandlers ();
}
}
}
}

Does this help?

On an unrelated note: it doesn't really matter to the compiler, but it
would probably help your code to read the MS naming conventions
(conventions, not hard rules). For instance, the cls prefix would be
unnecessary. But we know what you mean, so trivial really...

Marc

Dec 10 '06 #2
For reference (also on conventions), the "On{blah}" method is just the
method that fires the event:

public event SomeEventHandle r SomeEvent;
private void OnSomeEvent({ar gs}) {
SomeEventHandle r handler = SomeEvent;
if(handler!=nul l) {
handler(this, {args - perhaps creating a SomeEventArgs}) ;
}
}

Also - note that to meet normal convention the StringEventHand ler would
have an StringEventArgs : EventArgs which has a string value as a
property. But it should work your way too.

Marc

Dec 10 '06 #3
Thanks Mark for your quick answer.

I followed your advice and of course it works ok. The code didn't become any
uglier ;-)
Regarding the naming conventions, I tend to add silly prefixes like cls, C
etc, when the classes/variables etc are not to be used anywhere outside one
of my classes. In that way I can group local variables, controls etc in
intellisense. clsResults is a bad example of that logic. This is because
even though new instances of clsResults are only to be created from within
the class astar (clsResults is not to be used anywhere else), clsResults
instance properties are actually accessible outside that class. As the
program becomes bigger and bigger and classes tend to be more independent, I
see that prefixes become more of a problem than an convenience..

Anyway, by googling for MS conventions, I see that many people would agree
or disagree with this.

So ... you have been helpful, thanks again!


Dec 10 '06 #4

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

Similar topics

36
6406
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
13
2389
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
3
8119
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to run the class it gives an error that "System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level". This error comes because XmlNode has not any public constructor. I found XmlNode has two constructor but both are private or friend...
21
1984
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new ("Pear"); Basket.Items.Add(item); //------
17
3926
by: wackyphill | last post by:
If you have a function that basically creates an object/struct that holds data only in a function and wish to return the object/struct from the function which is faster a class object or struct? I would have thought class object since it can be passed back as an object reference instead of copying an entire struct, but I've read things that make me question if structs are in fact faster. What's the truth?
3
1642
by: eBob.com | last post by:
I have several applications which mine web sites for personal information which they publish. They publish the info in one form, I transform the info into Excel spreadsheets. So all these programs pick up name, telephone number, age, sex, etc.. And as they pick up the information they display it in text boxes. The text boxes are display only, and the info is displayed only for debugging purposes. Right now I have a lot of duplicate...
9
2360
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice = CDec(txbInv.Text) Wage = CDec(txbTotWage.Text)
29
2231
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a "new" contract. I have a method in my contract class called "Save" which is called like this... dim oContract as new Contract
29
1687
by: RageARC | last post by:
I have the following code: index.php: class main_class { public $database = new DAL; public $html = new HTML; }
0
9515
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
10427
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...
1
10155
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
9029
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...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
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
4110
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
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.