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
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
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?
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{ }
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?
Yeah.. basically.. or would i be better off having functions in the Parent class to handle the communication between objects
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:
Note INotifyPropertyChanged is defined in System.ComponentModelCode: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 } }
Woop?
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..
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?