If I have a class like this:
class myClass
{
string foo;
string bar;
public myClass(string foo, string bar)
{
//does some stuff here
(myClass)deserializer.Deserialize(reader); //assign the return value to the instance
}
}
Something that would achieve this:
public myClass(string foo, string bar)
{
//does some stuff here
myClass tempobject = (myClass)deserializer.Deserialize(reader); //this method has a return value of type myClass
this.foo = tempObject.foo;
this.bar = tempObject.bar;
}
SomeMethod returns a value of type myClass. Can I assign this returned value to the instance of myClass that invoked the constructor?
No, that isn’t possible.
What you could do is make
SomeMethodstatic and the constructor private. This is called the Factory Pattern, as you have a method used to create instances of your class. The factory pattern is often used to create instances of various different concrete classes at runtime.As you’ve updated your question to say that the method isn’t in your class, then i think the best option is to make a method in your class that takes another instance of the class, then in that method, copy all properties. You’d call that in your constructor.