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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:42:47+00:00 2026-05-15T12:42:47+00:00

I have a number of section items (Lesson, Info) which inherit from the common

  • 0

I have a number of “section items” (Lesson, Info) which inherit from the common type SectionItem. The various types of SectionItems share some but not all properties.

I have found the best way to pass parameters to each kind of object is to pack them all in a Dictionary<string, object> and then let the base class SectionItem unpack the common ones, and each inheriting class unpack the specific ones.

This works well enough, but this is all very C#2 since I will only catch errors at runtime and not during compilation. Is there a way to do this more elegantly perhaps with generics?

alt text
(source: deviantsart.com)

using System;
using System.Collections.Generic;
using System.Text;

namespace TestPass234
{
    class Program
    {
        static void Main(string[] args)
        {
            List<SectionItem> sectionItems = new List<SectionItem>();

            {
                Dictionary<string, object> vars = new Dictionary<string, object>();
                vars.Add("sectionNumber", 1);
                vars.Add("title", "Lesson #1");
                vars.Add("startDate", new DateTime(2008, 12, 25));
                List<Flashcard> flascards = new List<Flashcard>();
                flascards.Add(new Flashcard { Question = "What color is the sky?", Answer = "blue" });
                flascards.Add(new Flashcard { Question = "What color is the sun?", Answer = "yellow" });
                vars.Add("flashcards", flascards);
                SectionItem sectionItem = SectionItem.Instantiate("lesson", vars);
                sectionItems.Add(sectionItem);
            }

            {
                Dictionary<string, object> vars = new Dictionary<string, object>();
                vars.Add("title", "Info #1");
                vars.Add("content", "This is info number one.");
                SectionItem sectionItem = SectionItem.Instantiate("info", vars);
                sectionItems.Add(sectionItem);
            }

            foreach (var sectionItem in sectionItems)
            {
                Console.WriteLine(sectionItem.Render());
            }
            Console.ReadLine();
        }
    }

    public class SectionItem
    {
        protected string _title;

        public SectionItem()
        { }

        public SectionItem(Dictionary<string, object> vars)
        {
            _title = Convert.ToString(vars["title"]);
        }

        public static SectionItem Instantiate(string idCode, Dictionary<string, object> vars)
        {
            switch (idCode)
            {
                case "lesson":
                    return new SectionItemLesson(vars);
                case "info":
                    return new SectionItemInfo(vars);
                default:
                    return new SectionItem();
            }
        }

        public virtual string Render()
        {
            return "undefined section item";
        }

    }


    public class SectionItemLesson : SectionItem
    {
        private int _sectionNumber;
        private DateTime _startDate;
        private List<Flashcard> _flashcards = new List<Flashcard>();

        public SectionItemLesson(Dictionary<string, object> vars) : base(vars)
        {
            _sectionNumber = Convert.ToInt32(vars["sectionNumber"]);
            _startDate = Convert.ToDateTime(vars["startDate"]);
            _flashcards = vars["flashcards"] as List<Flashcard>;
        }

        public override string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(String.Format(">>> {0}. {1} (Starts {2:ddd, MMM d, yyyy})", _sectionNumber, _title, _startDate));
            foreach (var flashcard in _flashcards)
                sb.AppendLine("    - " + flashcard.Render());
            return sb.ToString();
        }
    }

    public class SectionItemInfo : SectionItem
    {
        private string _content;

        public SectionItemInfo(Dictionary<string, object> vars)
            : base(vars)
        {
            _content = Convert.ToString(vars["content"]);
        }

        public override string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(String.Format(">>> {0}", _title));
            sb.AppendLine(String.Format("    {0}", _content));
            return sb.ToString();
        }
    }

    public class Flashcard
    {
        public string Question { get; set; }
        public string Answer { get; set; }

        public string Render()
        {
            return "Q: " + Question + " A: " + Answer;
        }
    }
}
  • 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. Editorial Team
    Editorial Team
    2026-05-15T12:42:47+00:00Added an answer on May 15, 2026 at 12:42 pm

    Can you just create a ‘parameters’ class for each SectionItem class?

    public class SectionItemParameters
    {
        public string Title { get; set; }
    }
    
    public class SectionItemLessonParameters
        : SectionItemParameters
    {
        public int SectionNumber { get; set; }
        public DateTime StartDate { get; set; }
        public List<Flashcard> Flashcards { get; set; }
    }
    
    public class SectionItemInfoParameters
        : SectionItemParameters
    {
        public string Content { get; set; }
    }
    

    Then, every class in the hierarchy can receive its parameters in a strongly typed object. Your factory method Instantiate would take in a SectionItemParameters and cast to the appropriate type for the constructor being called.

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

Sidebar

Related Questions

I have a section of code which should be executed by a maximum number
I have SEF urls like /sitename/section/12-news-title and number 12 is id of the news.
I have this in the head section of my page <script type=text/javascript> function makeCookie(name)
I have this form which has a section where a user can specify an
I have a UITableView with several sections, and the number of sections is dynamic,
I have number of line breaks in a string. But I only want it
I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number
I have a number input that should trigger jQuery on every change. To do
I have a number of classes and they are quite close to each other
We have a number of data objects that realize INotifyPropertyChanged to allow for WPF

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.