Here is an easy question (C# noob here) and I can’t find how to do this (I have C# 4.0 reference from Schildt but not sure what I am looking for yet).
Say I have two objects created by two different classes, but have fields and/or methods with the same name. For example:
public class Object1 {
bool enable;
public void Reset(){
enable = false;
}
}
public class Object2 {
bool enable;
public void Reset(){
enable = false;
}
}
Objects are very dissimilar, justifying the creation of two different objects. However, I have many objects with the common ‘enable’ field and ‘Reset()’ method.
So here is what I would like to do (in pseudocode or whatever this is):
void Manipulate(ref object pObj){
(pObj).enable = true;
(pObj).Reset();
}
void Main(){
Manipulate(obj1);
Manipulate(obj2);
}
How can I do this? I am not sure how to create an object that can point to objects from different classes.
Thanks!
Thanks to all the answers. I went through interfaces and understood them as an ‘alias’ and couldn’t figure out what was the usefulness of them.
This is what interfaces are for:
You then define your
Manipulatemethod to accept a parameter of any type that implements this interface: