简体   繁体   中英

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);
                }

            }
        }

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.

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);
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM