Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 79177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:06:18+00:00 2026-05-10T21:06:18+00:00

I may be going about this backwards… I have a class which is like

  • 0

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;

Document doc = mytemplate.GetNewDoc(); 

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

Document doc = New Document(TemplateID) 

Because the template class already has the ability to return a document, I’d like the constructor to do something like

Template temp = new Template(TemplateID); this = temp.GetNewDoc(); 

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 🙂

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.

A more elaborate code example:

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;     } } } 

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:06:18+00:00Added an answer on May 10, 2026 at 9:06 pm

    If you want to be able to do anything other than create a new object just from the code in the constructor, don’t use a constructor in the first place.

    Do you really need an Instance constructor taking an int? Why not turn it into a static factory method:

    public static Instance CreateInstance(int id) {     MyTemplate def = new MyTemplate();     return def.GetInstance(id); } 

    Static methods have various advantages over constructors – although some disadvantages too. (There’s a separate SO question on that – worth a look.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I may be going about this the wrong way. I have a container of
I'm wondering if I may be going about this the wrong way and am
I'm new to Python so I may be going about this completely wrong, but
I have a number of elements with the same class like this: <html> <head><title>example</title>
I may be going about this the wrong way, so I'll set out the
This really is an architectural question. I feel like I'm going about this the
Alright, I am going to state up front that this question may be too
I'm probably going about this all wrong, but hey. I am rendering a large
What I'd like to accomplish is something similar to this: class Foo( val bar:
Let's say I have some code like this in AS3 for(...) thing = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.