Anthony Don
Anthony Don

Reputation: 157

Retrieving item from List collection when each index have a set of items

I am trying to create a form application with asp.net C# that display question and hint. I was going to put the two in a separate list collection but there could be a possibility that questions and hints can become disjointed so I decided to create a class where it takes ID, Q, and Hint and then put them in a list collection as a set.

Here is the code in QHint.cs file:

public QHint (int ID, string Q, string Hint)
{
      this.ID = ID;
      this.Q = Q;
      this.Hint = Hint;
}

public int ID { get; set; }
public string Q { get; set; }
public string Hint { get; set; }

Here is the code in the form1.cs file:

List<QHint> QHintList = new List<QHint>;
QHintList.add(new QHint(1, "quesiton1 blah blah?", "hint1 blah blah"));
QHintList.add(new QHint(2, "quesiton2 blah blah?", "hint2 blah blah"));
.... and so on....

My question is how can I specify what item to retrieve from the list such as just the hint1? I tried to retrieve a set (ID, Q, and Hint) with QHintList[0] but was not even able to do that. However, ultimately I want to be able to display question1 and then when the user hit a hint button I can display the corresponding hint1. Also, is using class and list the best way logically to accomplish what I want?

This might be some basic knowledge and I tried looking it up like how to use a list, how to retrieve data from list, and so on but had no luck.

Any help will be much appreciated.

Upvotes: 3

Views: 251

Answers (6)

Gero
Gero

Reputation: 13553

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace QuestHint
{
    class QHint
    {
        public QHint() { }
        public QHint(int ID, string Q, string Hint)
        {
            this.ID = ID;
            this.Q = Q;
            this.Hint = Hint;
        }

        public int ID { get; set; }
        public string Q { get; set; }
        public string Hint { get; set; }
        public List<QHint> QHintList = new List<QHint>();
    }

    class Program
    {
        static void Main(string[] args)
        {
            QHint q = new QHint();

            q.QHintList.Add(new QHint(1, "quesiton1 blah blah?", "hint1 blah blah"));
            q.QHintList.Add(new QHint(42, "quesiton2 blah blah?", "hint2 blah blah"));


            int magicNumber = 42;

            Debug.WriteLine(q.QHintList[0].Q); // output quesiton1 blah blah?
            Debug.WriteLine(q.QHintList.Find(obj => obj.ID == magicNumber).Hint); //hint2 blah blah
// you are saying like: find me the obj, where the ID of that obj is equals my magicNumber. And from that found object, give me the field Hint.
        }
    }
}

Upvotes: 0

Hukam
Hukam

Reputation: 1026

Try this with Linq

 var hint = QHintList.First(p=>p.ID == inputId).Hint

Upvotes: 0

user240141
user240141

Reputation:

If I am in right direction then , You need to find the text hint1 in Property Hint. In case of multiple

foreach QHint q in QHintList
{
   if(q.Hint.Contains("hint1"))
   {
     // then do something cool;
   }
}

Upvotes: 0

uriDium
uriDium

Reputation: 13420

If you can keep track of which hints are at which positions then you can just use

var qHint = QHintList[i];

If you have no way of keeping track then you can use the find method on the List which takes a predicate. I think this will work (depending on what information you have available at the time)

var qHint = QHintList.Find(q => q.Id == YourId);

Upvotes: 4

saj
saj

Reputation: 4796

Why not create a dictionary for increased performance

Dictionary<int, QHint> QHintList = new Dictionary<int, QHint>;
QHintList.add(1, new QHint(1, "quesiton1 blah blah?", "hint1 blah blah"));
QHintList.add(2, new QHint(2, "quesiton2 blah blah?", "hint2 blah blah"));

Then you can call like this;

int questionId = 1;
QHintList[questionId].Hint

Upvotes: 1

Jude Fisher
Jude Fisher

Reputation: 11294

var hint = QHintList[0].Hint;
Console.WriteLine(hint);

Upvotes: 0

Related Questions