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 912777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:26:36+00:00 2026-05-15T17:26:36+00:00

I’m trying to find a tidy solution to a questionnaire problem. Let us say

  • 0

I’m trying to find a tidy solution to a questionnaire problem. Let us say that I have a Questionnaire class which has a collection of Answers, e.g.

public class Questionnaire
{
    public virtual ISet<Answer> Answers {get;set;}
}

Answers need to be of different types depending on the question, e.g. date of birth, marks out of ten, why do you think etc.

My first thought was something like this:

public class Question
{
    public virtual QuestionType TypeOfQuestion {get;set;}
    public virtual string PromptText {get;set;}
}

public class Answer
{
    public virtual Question Question {get;set;}
}

public class DateTimeAnswer : Answer
{
    public virtual DateTime Response {get;set;}
}        

public class IntegerAnswer : Answer
{
    public virtual int Response {get;set;}
}        
// etc.

The obvious problem would be that from the questionnaire, there is no access to the Response property:

questionnaire.Answers[0].Response; // compile error

The same would apply to an interface. It would be nicer to use a generic interface, such as:

public interface IAnswer<T> 
{
    public virtual Question Question {get;set;}
    public virtual T Response {get;set;}
}

public class DateTimeAnswer : IAnswer<DateTime> {}

The problem then comes in the Questionnaire class, as the type of IAnswer must be supplied:

public class Questionnaire
{
    public virtual ISet<IAnswer<???>> Answers {get;set;}
}

Clearly I don’t want to have many collections of IAnswer each with different types. I could use

ISet<IAnswer<dynamic>> 

but then NHibernate wouldn’t like it.

I realise a compromise is needed somewhere, but I’m not sure which is the prettiest. What would you do?

  • 1 1 Answer
  • 1 View
  • 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. Editorial Team
    Editorial Team
    2026-05-15T17:26:37+00:00Added an answer on May 15, 2026 at 5:26 pm

    I think subclassing Question as well as answer is a good plan – get rid of that QuestionType enum.

    You can then have a MakeAnswer(string) abstract method on Question which encapsulates a lot of logic for you, and can do type conversion etc if necessary.

    My solution to the generic problem on Rob’s answer would be something like this:

    public interface IAnswer
    {
       Question Question { get; }
       void Respond(IAnswerFormatter formatter);
    }
    

    Where IAnswerFormatter looks something like this:

    public interface IAnswerFormatter
    {
       void Format(string value);
       void Format(DateTime value);
       ...
    }
    

    And DateTimeAnswer would look like this:

    public class DateTimeAnswer : IAnswer
    {
        public DateTimeAnswer(Question question, DateTime value)
        {
            Question = question;
        }
        public Question Question { get; protected set; }
    
        protected DateTime Response {get; set;}
    
        public virtual void Respond(IAnswerFormatter formatter)
        {
            formatter.Write(Response);
        }
    }
    

    And DateTimeQuestion might look like:

    public class DateTimeQuestion : Question
    {
        public IAnswer MakeAnswer(string value)
        {
            //  Ignoring error handling here
            return new DateTimeAnswer(this, DateTime.Parse(value));
        }
    }
    

    This way your answer can hold the value in their own way, and you NH mappings can specify how it looks in the database (I’d recommend mapping using Discriminators) your Question can be responsible for creating answers from generic responses.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from

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.