Generic's - Collections

Hi all,

I aint too cluded up on Generics yet, but let me give you a run down on my problem so that you can advise me if Generics is the way for me to go.

OK - I have a a couple of collection classes that inherit from CollectionBase. These collection classes all have the same property members that I have overriden,, such as Add, Remove (Overloaded), this, etc etc.

I may be going down the wrong route with Generic's here, but could I have one Collection Class with these Property Methods that holds generic Types, and then do some running conversion to the appropriate types

Am I going down the right path with this

Tryst



Answer this question

Generic's - Collections

  • pranav kulkarni

    Tryst –

    Your original question was “Am I going down the right path with this ” in re using generics.

    It seems to me that you might want to think again about what it is you’re storing. Clearly, if the questions require essays for answers, you’ll want to store the answers. On the other hand, if the questions are of the multiple choice type, you need only store the numbers of the answers.

    It’s not clear to me what it is you’re trying to do, so I have to recommend against generics (and all other technologies, as well) until your goal is defined.



  • Pravin Pagare

    First, I would go to the help file and look at the collections that already exist in System.Collections.Generic;

    Dictionary looks like a reasonable choice, although ymmv.

    Then I would write a QuestionAndAnswer class because it’s easier to keep track of an answer that is in close proximity to a question, assuming there is only one answer to a question. That is probably an oversimplification

    It seems to me that perhaps keeping the Questions and Answers in a database is appropriate You’re going to have to get them from somewhere.

    Anyway, I would simply add the Q&A instances to the dictionary using the already built code, a la

    const int QUESTIONCOUNT = 10;

    Dictionary<int, InspectionQuestion> Questions = new Dictionary<int, InspectionQuestion>();

    Dictionary<int,InspectionAnswer> Answers = new Dictionary<int,InspectionAnswer>();

    for ( int i = 0; i < QUESTIONCOUNT; i++ ) {

    // somehow make a question

    InspectionQuestion q = new InspectionQuestion();

    Questions.Add( i, q );

    // somehow make an answer

    InspectionAnswer a = new InspectionAnswer();

    Answers.Add( i, a );

    }



  • Ashish757

    Hi,

    I am using ADO.NET to get the data from the database, but I am referring to the objects that need to hold this data during the life of my application. For example, all answered questions (answers) will need to be stored in the Answers collection object, and once they have finished (hit the Finished button) this data then gets saved to the database (through using ADO.NET).

    Thanks

    Tryst


  • mrehpine

    The idea behind generic collections is that you don’t (usually) have to write your own methods to replace the generic ones. E.g.,

    List<int> li = new List<int>();

    li.Add( 1 );

    li.Add( 2 );

    works



  • Prashanti

    Hi Peter, and thanks for your reply.

    A quick rundown of what I am doing. I have an application that allows users to create a set of questions that are assoicated with an inspection, and against each question, I have an answer. Now an answer can be of varying types (i.e., they can come from a look up table in a database, they can be free text (int, string), or the user can pre-define the answers that will be displayed in a combo-box). So for the user its a two-stage process, create a question, and define the answers for that question. In my application, all the questions get displayed on a form, and then when a user clicks on a question, the applications' logic determines what type of answer is associated with this, and displays the relevant controls assoicated with this questions - and performs processing if necessary (i.e. get the data from the database to populate the control). In my database I have a answers table that contains a host of information about the answer (i.e additional comments field, further action by date field etc).

    The reason I need to store these answers, along with questions, in a collection, is because when on the questions summary form, the answers get displayed.

    The tricky part is that questions have a parent of group, so I need a groups collections, where each group has a questions collection that is associated with it.

    (I guess I need to get back to the drawing board with this... :-s)

    Tryst


  • Shawn Ramey

    Since the questions and answers are in a database, why not use ADO.NET

  • Trombino

    Tryst – probably more than one tricky part, eh

    I am thinking now that you would have several classes:

    QuestionBase that is the root of the question hierarchy, and from that derive

    QuestionMultiChoice, QuestionTrueFalse, etc.

    Similarly, AnswerBase, from which the concrete answer types are derived.

    For each question there is some kind of answer. They’re really tied together, so I might group each question and answer in a Conversation or Interrogation class.

    The Inspection class might then be a collection of Interrogations.

    But all of these classes that you write are easily contained in one of the generic containers without writing any of your own “collection” code.



  • Shah Yogesh

    So how would I make use of Generics (if possible for my solution) if I have the following collection classes...

    public class InspectionQuestions : CollectionBase
    {
    public InspectionQuestions() { }

    public bool Add(TblNspInspectionQuestion.RecNspInspectionQuestion QuestionToAdd)
    {
    return List.Add(QuestionToAdd) >= 0;
    }

    public bool Remove(TblNspInspectionQuestion.RecNspInspectionQuestion QuestionToRemove)
    {
    bool Success = true;
    try
    {
    List.Remove(QuestionToRemove);
    }
    catch
    {
    Success = false;
    }

    return Success;
    }

    public bool Remove(int QuestionUID)
    {
    bool Success = false;
    try
    {
    bool Found = false;
    int Idx = 0;
    while (Idx < Count)
    {
    if (this[Idx].m_nQuestionTreeUID == QuestionUID)
    {
    Found = true;
    RemoveAt(Idx);
    }

    if (!Found)
    {
    Idx++;
    }
    }
    Success = Found;
    }
    catch
    {
    Success = false;
    }

    return Success;
    }

    public TblNspInspectionQuestion.RecNspInspectionQuestion this[int Index]
    {
    get { return (TblNspInspectionQuestion.RecNspInspectionQuestion)List[Index]; }
    }
    }


    public class InspectionAnswers : CollectionBase
    {
    public InspectionAnswers() { }

    public bool Add(TblNspInspectionAnswer.RecNspInspectionAnswer AnswerToAdd)
    {
    return List.Add(AnswerToAdd) >= 0;
    }

    public bool Remove(TblNspInspectionAnswer.RecNspInspectionAnswer AnswerToRemove)
    {
    bool Success = true;
    try
    {
    List.Remove(AnswerToRemove);
    }
    catch
    {
    Success = false;
    }

    return Success;
    }

    public bool Remove(string AnswerUID)
    {
    bool Success = false;
    try
    {
    bool Found = false;
    int Idx = 0;
    while (Idx < Count)
    {
    if (this[Idx].m_strAnswerUID.Equals(AnswerUID))
    {
    Found = true;
    RemoveAt(Idx);
    }

    if (!Found)
    {
    Idx++;
    }
    }
    Success = Found;
    }
    catch
    {
    Success = false;
    }

    return Success;
    }

    public TblNspInspectionAnswer.RecNspInspectionAnswer this[int Index]
    {
    get { return (TblNspInspectionAnswer.RecNspInspectionAnswer)List[Index]; }
    }
    }

    Would I just have one Collection Class

    Thanks

    Tryst


  • rainielle73

    Hi Peter, and thanks for your reply.

    A few notes: The questions and answers are already stored in the datasbase, and the Answer Class has a Question ID as one of its properties, so attaching an answer to a question isn't to diffucult. Also, it is possible for a question to have multiple answers - so I am not sure how using a dictionary (like in your example) will help/resolve this issue/scenario.

    The example you provide is very nice approach for one answer to a question, but can't see how it would work for scenarios where multiple answers belong to one question.

    Thanks

    Tryst


  • Generic's - Collections