Shalomi90
Shalomi90

Reputation: 744

Adding value from a dictionary c#

i have a method where i'm adding Tabpages and i created a dictionary for the tabpages because they should be shown in their each Mode(Key)

I want to iterate im my SwitchMode method over the forech and and add the tabpages.

My Code:

    [Flags]
    public enum Mode
    {
        Start = 1,
        Test = 1 << 1,
        Config = 1 << 2,
    }

Iterating over Dictionary - > Here I need help - how can I add all the tabpages and not only the first and is this kind of iterating right?

public void SwitchMode (Mode mode)
        {
            foreach (var m in _tabDict)
            {
                if (m.Value == Mode.Start)
                {
                    AddTabPage (_tabDict.First ().Key);
                }
                if (m.Value == Mode.Test)
                {
                    AddTabPage (_tabDict.First ().Key);
                    // AddTabPage (_tabDict.All ()); // doesn't work
                }
                if (m.Value == Mode.Config)
                {
                    AddTabPage (_tabDict.First ().Key);
                }

            }
        }

Upvotes: 0

Views: 94

Answers (2)

mnieto
mnieto

Reputation: 3874

you can iterate through the Keys collection

foreach (Type key in _tabDict.Keys) {
    AddTabPage(key);
}

What I don't understand is what is the function of Mode flag in your scenario. It's not used in the AddTabPage. So there is no diferences between one tab and another.

Upvotes: 4

Zein Makki
Zein Makki

Reputation: 30022

You're almost there, use m.Key in your loop:

public void SwitchMode(Mode mode)
{
    foreach (var m in _tabDict)
    {
        if (m.Value == Mode.Start)
        {
            AddTabPage(m.Key);
        }
        if (m.Value == Mode.Test)
        {
            AddTabPage(m.Key);
        }
        if (m.Value == Mode.Config)
        {
            AddTabPage(m.Key);
        }

    }
}

Upvotes: 2

Related Questions