Note that there are some explanatory texts on larger screens.

plurals
  1. POCall a factory from Constructor to get a new version of "this"
    text
    copied!<p>I may be going about this backwards... I have a class which is like a document and another class which is like a template. They both inherit from the same base class and I have a method to create a new document from a template (or from another document, the method it is in the base class). So, if I want to create a new document from a template, I just instantiate the template and call GetNewDoc() on it;</p> <pre><code>Document doc = mytemplate.GetNewDoc(); </code></pre> <p>In the Document class I have a blank constructor creating a new, blank document as well as another constructor that takes a document ID so I can load the document from the database. However, I would also like a constructor that takes a Template ID. This way I can do</p> <pre><code>Document doc = New Document(TemplateID) </code></pre> <p>Because the template class already has the ability to return a document, I'd like the constructor to do something like</p> <pre><code>Template temp = new Template(TemplateID); this = temp.GetNewDoc(); </code></pre> <p>Of course, I can't do this as "this" is read-only - and it feels odd anyway. I have a feeling I am being very stupid here so feel free to shout :)</p> <p>The thing is that the object in question is pretty complex with several collections of child objects and database persistence over multiple tables so i don't want to duplicate too much code. Though, I guess I could just get the new document from the template and then copy the fields/properties across as the collections should follow easily enough - it just seems like duplication.</p> <p>A more elaborate code example:</p> <p><code></p> <pre><code>using System; using System.Collections.Generic; using System.Text; namespace Test { class Program { static void Main(string[] args) { // This just creates the object and assigns a value Instance inst = new Instance(); inst.name = "Manually created"; Console.WriteLine("Direct: {0}", inst.name); //This creates a new instance directly from a template MyTemplate def = new MyTemplate(); Instance inst2 = def.GetInstance(100); Console.WriteLine("Direct from template: {0}", inst2.name); Instance inst3 = new Instance(101); Console.WriteLine("Constructor called the template: {0}", inst3.name); Console.ReadKey(); } } public class Instance { public string name; public Instance(int TemplateID) { MyTemplate def = new MyTemplate(); //If I uncomment this line the build will fail //this = def.GetInstance(TemplateID); } public Instance() { } } class MyTemplate { public Instance GetInstance(int TemplateID) { Instance inst = new Instance(); //Find the template in the DB and get some values inst.name = String.Format("From template: {0}", TemplateID.ToString()); return inst; } } } </code></pre> <p></code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload