English 中文(简体)
list? dictionary? array?
原标题:

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.

最佳回答

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.

问题回答

List<TabPage>

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

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

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签