G
Gianluca
Hi,
I want to make a class that acts like an "object factory", that
creates objects all inherited from a root class called MyBaseObject.
To create objects I want to use a method in which I pass in the first
parameter the object type I want to create, and in the second
parameter (an output parameter) the reference to the object created.
So, the method is declared like this:
public int GetObject(MyObjectTypes type, out MyBaseObject obj)
Suppose I have a class MyObject1 inherited from MyBaseObject.
In the client code that uses the GetObject method I want to do
something like this:
.... other lines of code ...
MyObject1 newObj;
Factory.GetObject(MyObjcectType.Object1, out newObj);
.... other lines of code ...
Using the code above, I get the following error at compilation time:
'argument 2: cannot convert from out MyObject1 to out MyBaseObject'.
I use this code to avoid the problem:
.... other lines of code ...
MyObject1 newObj;
MyBaseObject temp;
Factory.GetObject(MyObjcectType.Object1, out temp);
newObj = (MyObject1) temp;
.... other lines of code ...
Is there a way to force the c# compiler to accept the first version of
my code?
Thanks in advance for any suggestion
Bye
Gianluca
I want to make a class that acts like an "object factory", that
creates objects all inherited from a root class called MyBaseObject.
To create objects I want to use a method in which I pass in the first
parameter the object type I want to create, and in the second
parameter (an output parameter) the reference to the object created.
So, the method is declared like this:
public int GetObject(MyObjectTypes type, out MyBaseObject obj)
Suppose I have a class MyObject1 inherited from MyBaseObject.
In the client code that uses the GetObject method I want to do
something like this:
.... other lines of code ...
MyObject1 newObj;
Factory.GetObject(MyObjcectType.Object1, out newObj);
.... other lines of code ...
Using the code above, I get the following error at compilation time:
'argument 2: cannot convert from out MyObject1 to out MyBaseObject'.
I use this code to avoid the problem:
.... other lines of code ...
MyObject1 newObj;
MyBaseObject temp;
Factory.GetObject(MyObjcectType.Object1, out temp);
newObj = (MyObject1) temp;
.... other lines of code ...
Is there a way to force the c# compiler to accept the first version of
my code?
Thanks in advance for any suggestion
Bye
Gianluca