0

I am either looking for an existing solution or to create a solution to solve a problem for looking up items based on an Enum bitmask. Essentially, I have an Enum with the flag property set and so I can combine these values into a bitmask. What I would like to do is to have a bucket for each of the individual values and put an entry into each of the buckets where the flag is enabled in the mask.

The value portion of the dictionary would function as a sort of list to contain the entry. Ideally, the code usage would look something similar to this:

Enum Fields { Unknown = 0, Username = 1 << 0, Password = 1 << 1, Email 1 << 2...etc }
Dictionary<Fields, List<string>> errors = new Dictionary<Fields, List<string>>();
Fields mask = Fields.UserName | Fields.Password;
errors[mask] = "Input is empty";
List<string> errMsgs = errors[Fields.UserName]; 
// to get a list of all error messages associated with Fields.UserName
// including the one just entered.  A lookup on Fields.Password would 
// return the same entry

I already have an idea of how this can be done using Dictionarys with the value element as a list. My concern is the efficiency when I create thousands of objects each with their own instance of this error lookup mechanism. I don't expect each instance of this Error dictionary to have hundreds of entries, because each object only has N number of fields, and there are only a limited number of bitmasks, because of this I have considered using just lists, or a keyed set, converted to a list and run LINQ against it to find the different Enum buckets of entries.

Edit: I would expect Errors to look something like this in memory

UserName: [001]=[{011,"Input is empty"},{001,"Username must be 8 letters long"]}
Password: [010]=[{011,"Input is empty",{010,"Password must be 8 letters long, and contain UCase, and digits"}]
Email:    [100]=[{100,"Email address malformed."}]

So that when the UI asks Errors if Fields.Password has any errors, it reports two and the UI can show an indicator on that field with the corresponding errors reported. A dictionary with a list value can solve this for the most part, but I am concerned about how efficient that would be in memory and am wondering if a single list for the whole object may not be better. IE List() where Errors contains a Bitmask and an ErrMsg string and let Linq sort it out.

Wadiv
  • 1
  • 1
  • 1
    If you add `FlagsAttribute` to your enum (add `[Flags]` on the line before the `enum` keyword), a `.ToString()` call will produce a comma delimited string with all enabled flags. e.g. `(Fields.Username | Fields.Password).ToString()` produces `"Username, Password"`. – cbr Apr 04 '16 at 18:06
  • Perhaps you can make use of this to produce an error message like "The following fields are missing: Username, Password". – cbr Apr 04 '16 at 18:08
  • 2
    It's a bit unclear to me what you want `errors` to contain, when, and why. Can you edit your question to show some example input-output combinations? – CodeCaster Apr 04 '16 at 18:08
  • Modt of your code should work if you add the key/value pair to the Dictionary first. Example: `errors.Add(mask, new List);` - then you can continue with: `errors[mask].Add("Input is empty");` to add an error to the list. – Visual Vincent Apr 04 '16 at 18:26
  • I meant to say "Most of your code...". – Visual Vincent Apr 04 '16 at 19:03

0 Answers0