Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I implement this complex object

How can I implement this complex object

Scheduled Pinned Locked Moved C#
questioncsharphelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbudak
    wrote on last edited by
    #1

    I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.

    ExamFile.Title = "Test Exam File";
    ExamFile.FileName = "my First Exam.mfe";

    ExamFile.Exam[0].ExamName = "This is my First Exam";
    ExamFile.Exam[1].ExamName = "This is my Second Exam";

    ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
    ExamFile.Exam[0].Question[0].Type = "Multiple";
    ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
    ExamFile.Exam[0].Question[1].Type = "Single";

    ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
    ExamFile.Exam[1].Question[0].Type = "Multiple";

    Thanks

    C D D 3 Replies Last reply
    0
    • M mbudak

      I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.

      ExamFile.Title = "Test Exam File";
      ExamFile.FileName = "my First Exam.mfe";

      ExamFile.Exam[0].ExamName = "This is my First Exam";
      ExamFile.Exam[1].ExamName = "This is my Second Exam";

      ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
      ExamFile.Exam[0].Question[0].Type = "Multiple";
      ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
      ExamFile.Exam[0].Question[1].Type = "Single";

      ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
      ExamFile.Exam[1].Question[0].Type = "Multiple";

      Thanks

      C Offline
      C Offline
      Chris Copeland
      wrote on last edited by
      #2

      Something like..

      public class ExamFileBase
      {
      public string Title { get; set; }
      public string FileName { get; set; }
      public List Exam { get; set; };

      public ExamFileBase()
      {
          Title = "Default title";
          FileName = "default\_filename.mfe";
          Exam = new List();
      }
      

      }

      public class ExamBase
      {
      public string ExamName { get; set; }
      public List Question { get; set; }

      public ExamBase()
      {
          ExamName = "Default exam";
          Question = new List();
      }
      

      }

      public class QuestionBase
      {
      public string Remark { get; set; }
      public string Type { get; set; }
      }

      Only problem is, for the List properties, you'd have to initiate a class before accessing it. IE:

      ExamFileBase ExamFile = new ExamFileBase();
      ExamFile.Exam.Add(new ExamBase());

      Unless you made them static so each was initialized auto-matically.

      1 Reply Last reply
      0
      • M mbudak

        I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.

        ExamFile.Title = "Test Exam File";
        ExamFile.FileName = "my First Exam.mfe";

        ExamFile.Exam[0].ExamName = "This is my First Exam";
        ExamFile.Exam[1].ExamName = "This is my Second Exam";

        ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
        ExamFile.Exam[0].Question[0].Type = "Multiple";
        ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
        ExamFile.Exam[0].Question[1].Type = "Single";

        ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
        ExamFile.Exam[1].Question[0].Type = "Multiple";

        Thanks

        D Offline
        D Offline
        Dag Oystein Johansen
        wrote on last edited by
        #3

        From your code it's evident that you want ExamFile to be a collection of Exam objects. Exams have names and a collection of Question(s), and Question has two string attributes, Remark and Type. This object model is easily expressed in C#:

        public class ExamFile
        {
        public string Title { get; set; }
        public string Name { get; set; }
        readonly public List Exams = new List();
        }

        public class Exam : List
        {
        public string Name { get; set; }
        readonly public List Questions = new List();
        }

        public class Question
        {
        public string Remark { get; set; }
        public string Type { get; set; }
        }

        You could construct the object graph and then set properties as in your posted code, like this:

        ExamFile file = new ExamFile();
        file.Add(new Exam());
        file.Add(new Exam());
        file.Exams[0].Questions.Add(new Question());
        file.Exams[0].Questions.Add(new Question());
        file.Exams[1].Questions.Add(new Question());
        file.Exams[1].Questions.Add(new Question());

        and then do as in your posted code

        file.Title = "Test";
        ...
        file.Exams[1].Questions[1].Remark = "tough question";

        However, in most cases you'd create a question object and set it's properties before adding it to an exam. Also note that the types themselves should usually have constructors that take some parameters. This has two side-effects: Less code to write to use the type, which is not very important but a marginal benefit, and a guarantee that an instance of a type cannot be created without certain minimum information. Types should typically be designed to be flexible and general. Coding convenience can be achieved by writing little "helper methods" separately from the type itself. In this case, you could do something like this:

        void addQuestion(Exam e, string remark, string type)
        {
        Question q = new Question();
        q.Remark = remark;
        q.Type = type;
        e.Questions.Add(q);
        }

        Exam addExam(ExamFile f, string name)
        {
        Exam e = new Exam();
        e.Name = name;
        f.Exams.Add(e);
        return e;
        }

        ExamFile file = new ExamFile();
        f.Title = "test";
        f.Name = "my first exam.mfe";

        Exam e = addExam(file, "First Exam");
        addQuestion(e, "remark #1", "Mutiple");
        addQuestion(e, "remark #2", "I don't know");

        and so on.

        1 Reply Last reply
        0
        • M mbudak

          I am the new about class and objects in c#. I just want to define an object for this issue after than I will save and restore from a file this ExamFile.

          ExamFile.Title = "Test Exam File";
          ExamFile.FileName = "my First Exam.mfe";

          ExamFile.Exam[0].ExamName = "This is my First Exam";
          ExamFile.Exam[1].ExamName = "This is my Second Exam";

          ExamFile.Exam[0].Question[0].Remark = "First Exam's first Q";
          ExamFile.Exam[0].Question[0].Type = "Multiple";
          ExamFile.Exam[0].Question[1].Remark = "First Exam's Second Q"
          ExamFile.Exam[0].Question[1].Type = "Single";

          ExamFile.Exam[1].Question[0].Remark = "Second Exam's First Q";
          ExamFile.Exam[1].Question[0].Type = "Multiple";

          Thanks

          D Offline
          D Offline
          Dominic Goulet
          wrote on last edited by
          #4

          Hello, You question is very large. I will try to answer you with a simple and short answer, and if you feel you need more explanations, feel free to post back here. You will need more than a single object to hold your information. For instance, you could use a ExamFile, Exam and Question objects. In the ExamFile object, you could have a List, and in Exam object you could have a List. Here is a concrete example of a data structure to hold your information :

          using System;
          using System.Collections.Generic;

          public class ExamFile
          {
          public String Title { get; set; }
          public String FileName { get; set; }
          public List Exams { get; set; }
          }

          public class Exam
          {
          public String ExamName { get; set; }
          public List Questions { get; set; }
          }

          public class Question
          {
          public String Remark { get; set; }
          public String Type { get; set; }
          }

          ______________________ Dominic Goulet FroggedSoft

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups