public void ChangeObjectType(System.Type objectType, Object selectedObject)
{
selectedObject = Activator.CreateInstance(objectType);
}
internal void BuoyToMouse()
{
ChangeObjectType(typeof(Buoy), selectedObject);
selectedObject.setPosition(mouseCurrentState.X - mouseStart.X, mouseCurrentState.Y - mouseStart.Y);
}
I’m creating a game and I’m trying to handle the mouse events right now. I’m trying to use an Object of type Object and then redefine it’s type depending on the type of Object that is clicked.
When I try this I get this error message :
Error 2 ‘object’ does not contain a definition for ‘setPosition’ and no extension method ‘setPosition’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)
How do I make it understand that the selectedObject has changed type?
You can’t “change the type” of an object, but that doesn’t sound like what you want to do. It sounds like you have a value of a certain type that’s held in a variable of type
object.In that case, you can just tell the compiler what the actual type of your value is using a “cast”: