C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-17-2006, 11:32 AM   #1
Registered User
 
Join Date: Aug 2006
Posts: 32
Newbie Question

Hi,

I am trying to write some code in which one class, when it reaches a certain state, tells its parent class to do something:

Code:
class classA
{
    classB B;
    public classA()
    {
        B = new classB();
    }
    public functionA(){}
}

class classB
{
    int x;
    public functionB()
    {
        // Modifies the value of x in a random way
    }
}

class Main
{
    A = new classA();
    A.B.functionB() /* This is called lots and lots, until x happens to  equal 50. When x = 50, I want to dispose of B, and call A.functionA().*/
}
I don't want to have to keep asking if (A.B.x == 50) after every call to functionB, because x is only equal to 50 after, on average, hundreds of calls to functionB(), so this would be very inefficient. Function B changes x randomly so there is no way of telling when exactly x will equal 50.

So, how can I tell A to do something once B reaches a certain state....could I use some sort of event handler, such that x=50 raises and event, thus telling A do dispose of B and so on?

I am very new to C# and would like to know what the code for such an event handler would be, or any other suggestions on how I might implement this.

Thank you!
ejohns85 is offline   Reply With Quote
Old 12-17-2006, 04:06 PM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
You have no choice. To react to x being 50, you have to test whether x is 50. Where you do it is irrelevant.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid Newbie question TimL C++ Programming 4 07-22-2008 04:43 AM
C prog newbie question Draginzuzu C Programming 1 02-03-2003 06:45 PM
a stupid question from a newbie newcomer C++ Programming 4 01-11-2003 04:38 PM
confusion with integers (newbie question) imortal C Programming 7 12-06-2002 04:09 PM
newbie class templates question daysleeper C++ Programming 2 09-18-2001 09:50 AM


All times are GMT -6. The time now is 12:10 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22