c# - Combine check boxes without using if -


i'm making password generator , have 3 check boxes: lowercase, uppercase, , number. right have if statement says:

    if (check1.checked == true & check2.checked == true & check3.checked == true)        {            length = convert.toint32(lengthtextbox.text);                 string password = "";                 int choice;                 generate gen = new generate();                 (int = 1; <= length; i++)                 {                     choice = rnd.next(1, 4);                     if (choice == 1)                     {                         password = password + gen.lower();                     }                     else if (choice == 2)                     {                         password = password + gen.upper();                     }                     else if (choice == 3)                     {                         password = password + gen.number();                     }                 }                  passwordtextbox.text = password;        }     else if (check1.checked == true & check2.checked == true)     {             length = convert.toint32(lengthtextbox.text);                 string password = "";                 int choice;                 generate gen = new generate();                 (int = 1; <= length; i++)                 {                     choice = rnd.next(1, 3);                     if (choice == 1)                     {                         password = password + gen.lower();                     }                     else if (choice == 2)                     {                         password = password + gen.upper();                     }                 }                  passwordtextbox.text = password;     } 

and on. method terribly inefficient. ran problem while making cash register program (10 choices of toppings, check ones want, , program adds prices together. if uncheck topping, price subtracted amount.), gave on it. now, determined find way metaphorically go shopping.

so need here find way of making selection of choice dynamic. 1 way of doing create list of functions in each function capable of generating value. @ start populate list relevant functions based on what's checked, , can have single loop chooses 1 of functions list:

list<func<char>> generators = new list<func<char>>(); if (lowercasecheckbox.checked)     generators.add(() => gen.lowercase());  if (uppercasecheckbox.checked)     generators.add(() => gen.uppercase());  if (numbercheckbox.checked)     generators.add(() => gen.number());  int length = convert.toint32(lengthtextbox.text); stringbuilder password = new stringbuilder(); random random = new random(); (int = 0; < length; i++) {     int choice = random.next(generators.count);     password.append(generators[choice]()); } string result = password.tostring(); 

note rather appending characters string repeatedly in loop should using stringbuilder avoid constant re-allocation , copying of values.

i suggest using more meaningful names checkbox variables; makes code more readable.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -