Thread: Quick question about classes

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    Quick question about classes

    I'm wondering what is the best method to go about this. So say i have class A and class B and an instance of each within the same scope.. How would I access one class from another. I hope you understand my question. Thanks

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can set it via a property or pass it to function calls. Just depends on what you are trying to accomplish.

    Code:
    A ains = new A();
    B bins = new B();
    
    ains.BIns = bins;
    bins.AIns = ains;
    
    //OR
    
    ains.NeedsB(bins);
    bins.NeedsA(ains);
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    Okay.. So is it better to have something within the class they are contained in messaging between them. then to have functions built in the classes communicating to each other. so say pass the class that contains them into the class function so...

    I hope you can understand the follow code.. this is an example of what not to use right?

    Code:
    class MainClass{
           A a;
           B b;
           MainClass(){
                 a.TakeOtherClass(this);
           }
    }
    
    class A{
           public void TakeOtherClass(MainClass class){
                    b.DoSomething();
           }
    }
    class B{
    }

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I guess I need a better understanding of what you are trying to do.

    You want child classes to have a reference to the parent class they are members of?
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    Yeah.. basically.. or would i be better off having functions in the Parent class to handle the communication between objects

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well. I am still waiting on what you are trying to accomplish..so if you could elaborate on your goal that could help me out..but I am thinking you are looking for some sort of event model.

    Here is a brief example:
    Code:
        public class BaseNotifyPropertyChange : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void NotifyPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }//if
            }
        }
    
        public class SimpleClassA : BaseNotifyPropertyChange
        {
            private string _simpleProperty;
            public string SimpleProperty
            {
                get
                {
                    return this._simpleProperty;
                }//get
                set
                {
                    this._simpleProperty = value;
                    this.NotifyPropertyChanged("SimpleProperty");
                }//set
            }
        }
    
        public class SimpleClassB : BaseNotifyPropertyChange
        {
            private int _simpleProperty;
            public int SimpleProperty
            {
                get
                {
                    return this._simpleProperty;
                }//get
                set
                {
                    this._simpleProperty = value;
                    this.NotifyPropertyChanged("SimpleProperty");
                }//set
            }
        }
    
        public class HolderClass
        {
            private SimpleClassA _instanceA;
            private SimpleClassB _instanceB;
    
            public HolderClass()
            {
                _instanceA = new SimpleClassA();            
                _instanceA.PropertyChanged += new PropertyChangedEventHandler(_instanceA_PropertyChanged);
    
                _instanceB = new SimpleClassB();
                _instanceB.PropertyChanged += new PropertyChangedEventHandler(_instanceB_PropertyChanged);
            }
    
            void _instanceB_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                //Do something here
            }
    
            void _instanceA_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                //Do something here
            }
        }
    Note INotifyPropertyChanged is defined in System.ComponentModel
    Woop?

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    I'm trying to do interaction between objects in a game.. and its somewhat confusing.. So I want to be able to trigger when something happens.. ex. collision etc..

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Right that is perfect for events. What I did was for a property changing usually done for UI updates and what not. But you could do the same concept in a game.

    Another example
    Code:
        public delegate void CollisionOccuredEventHandler(Sprite s1, Sprite s2);
    
        public class Sprite
        {
            public event CollisionOccuredEventHandler OnCollision;
    
            public virtual void CheckCollision(Sprite s)
            {
                bool isCollision = true;
    
                if (isCollision)
                {
                    if (OnCollision != null)
                    {
                        OnCollision(this, s);
                    }//if
                }//if
            }
        }
    
        public class GameStateLogic
        {
            private Sprite _player = null;
            private Sprite _enemy = null;
    
            public GameStateLogic()
            {
                _player = new Sprite();
                _player.OnCollision += new CollisionOccuredEventHandler(this.HandleCollision);
    
                _enemy = new Sprite();            
                _enemy.OnCollision += new CollisionOccuredEventHandler(this.HandleCollision);
            }
    
            private void HandleCollision(Sprite s1, Sprite s2)
            {
                //Do what you need to here.
            }
        }
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2006, 05:56 PM
  2. quick question
    By hyrule in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2005, 09:05 AM
  3. Quick Question on Classes
    By gguy85 in forum C++ Programming
    Replies: 6
    Last Post: 02-14-2005, 02:28 AM
  4. Quick Question!
    By dizz in forum C++ Programming
    Replies: 14
    Last Post: 11-21-2002, 07:02 AM
  5. Hey i got a quick Question
    By cis in forum C Programming
    Replies: 1
    Last Post: 03-15-2002, 10:12 PM