Thread: Newbie Question

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    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!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

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