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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I came up with the same solution: var xml =… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer It depends a bit on when you were thinking of… May 11, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer UPDATE: For anyone interested in it, here is the implementation… May 11, 2026 at 7:26 pm

Related Questions

I may be going about this backwards... I have a class which is like
I'm rather new to Java. After just reading some info on path finding, I
OK, I know there have already been questions about getting started with TDD ..
I've got this DOM: <li> <img class=ui-selectee src=../img/thumbs/80x80_1280_wallpaper.jpg style=/> <input type=hidden value=3/> <a class=btnImgDel
I'm working on a PyGTK/glade application that currently has 16 windows/dialogs and is about

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.