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 have an XSLT script which I would like to number things sequentially each
This may have been asked already, but I can't find it, so here goes.
I have a few things going on that need to be done one after
I have an exam tomorrow in Advanced Development, but I am stuck on the
This is not language specific. What are the best methods to limit how many
I am working on converting a web application over to a WPF desktop application.
One of my web apps has grown over the past few months from a
I know that each programming language has certain guideline and styles. My question is
I am using the Nov version of the AjaxControlToolkit, and I found a logic
I've been considering how you would write a business application in only F# with

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.