johnnie
johnnie

Reputation: 2057

Custom list in c#

I need to create a list that hold something like

public List<int,bool> PostionAndChecked
{ get; set; }

and then a list

public List<int> PageNumber
        { get; set; }

So what should happen is the each page number in the PageNumber list should be linked to the PostionAndChecked list

How do i do this?

Upvotes: 0

Views: 646

Answers (3)

Mr. Mr.
Mr. Mr.

Reputation: 4285

The following is suitable only as an illustration especially if the int is unique in your list of pages to check. Hopefully, you should be able to see how this might be changed to suit your purposes.

It is not obvious to me why you need another list storing page numbers, maybe you could explain why you think you need it?

public class PageChecker
{
    public IDictionary<int, bool> PositionAndChecked { get; set; }

    public PageChecker()
    {
        SetUpPages();
    }

    private void SetUpPages()
    {
        PositionAndChecked = new Dictionary<int, bool>();
        var pageCount = 10;
        for (int i = 0; i < pageCount; i++)
        {
            PositionAndChecked.Add(i, false);
        }
    }

    public void CheckPage(int pageNo)
    {
        PositionAndChecked[pageNo] = true;
    }
}

Upvotes: 2

MoonKnight
MoonKnight

Reputation: 23833

Use a Dictionary<int, bool>, this will store the Key (int) and the corresponding Value (bool), without the need to do your own indexing. So for the above you would have

public Dictionary<int, bool> lookup { get; set; } 
...
lookup = new Dictionary<int, bool>();

add new entries to this like

lookup.Add(0, true);
lookup.Add(1, false);
...

You can then reference the Boolean value based upon the relevent index as follows

bool b = lookup[someIndex];

For more information on this calss see here.

I hope this helps.

Upvotes: 2

zmbq
zmbq

Reputation: 39013

A List is a list of one type, not two. You can use a Tuple<int, bool>, or you can use a Dictionary<int, bool>.

Although, since you just need to hold a bit for each number, you might be satisfied with using a Set<int>, adding only the true numbers.

Upvotes: 3

Related Questions