views:

118

answers:

4

hi all

i'm trying to come up with a very simple way to store a boolean value for each tabpage in a tabcontrol. each page has a textbox, and i would like to store a bool foreach page so if tabpage 1 has been saved, then bool1 is set to true, else false, and so on.

then when they go to close the program it will go over all the tabpages and see if each tabpage has been saved or not. i need to be able to create some sort of a list that i can search and manipulate.

i have previously used tag properties for this but for some reason that doesn't work PROPERLY anymore.

any help advice would be greatl;y appreciated :)

thank you,

bael.

+4  A: 

You can use for example a Dictionary<TabPage,bool>. At the start of your application:

var pageStates=new Dictionary<TabPage,bool>();

foreach(var page in tabControl.TabPages) {
    pageStates.Add(page, false);
}

To change the state of a TabPage:

pageStates[page]=true;

And when your application finishes:

foreach(var page in TabControl.TabPages) {
    if(pageStates[page]) {
        //The page is saved
    }
}
Konamiman
thank you very much for your help and also for taking the time to provide the code sample aswell :)
baeltazor
+4  A: 

List<TabPage>

Store only those tabpage(s) that needs to be saved. If empty, nothing needs to be saved.

o.k.w
+1  A: 

If you are iterating through without removing or adding elements use the array.

If you are iterating through but adding and removing elements use the List.

If you are using strings as keys then use the dictionary.

The dictionary has very fast lookup performance with a high number of elements compared to the List.

Luke101
+4  A: 

Neither array, nor list, nor dictionary are the right data structure for tracking the boolean status of a set of objects. The structure that does this and nothing more is HashSet<T>. Either a T is in the set, or it isn't.

It's superior to a Dictionary<T, bool> because that actually maintains two states: whether the object is in the dictionary or not, and if it is, whether its value is true or false.

It's superior to a List<T> for two reasons: It's faster (though in your case, that will almost certainly be negligible), and it doesn't imply that there's some meaning to the order of the objects it contains, since the order of objects in a HashSet<T> is arbitrary.

Robert Rossney
I agree with you regarding `Dictionary<T, bool>` vs `HashSet<T>`, but I find the answer a bit misleading at first sight -- i.e. I would always prefer `Dictionary<T, SomeCustomInfo>` over `HashSet<T>` because it would give me an option to attach any data **or functionality** to each page in the future.
Groo
Choosing `HashSet<T>` is YAGNI in action.
Robert Rossney