-1

I have a click_event and as you can see there are a number of functions with many more to come.

Is there an easier way of doing this rather than having 30 odd lines of functions or is this really the best way?

private void metroButton1_Click(object sender, EventArgs e)
{
    foreach (MetroFramework.Controls.MetroTextBox item in groupBox1.Controls
        .OfType<MetroFramework.Controls.MetroTextBox>())
    {
        item.Enabled = false;
       
    }
    if (searchUsernametxt.Text == "")
    {
        MessageBox.Show("Domain or Username has not been completed, Please try again!",
            "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        //ActiveDirectory.DomainValue = comboBox1.SelectedText.ToString() ;
        if (ActiveDirectory.DoesUserExist(searchUsernametxt.Text) ==true)
        {
            ActiveDirectory.GetsamAccountName(searchUsernametxt.Text, samAccountNametxt);
            ActiveDirectory.isUserEnabled(searchUsernametxt.Text, userenabledtxt);
            ActiveDirectory.GetObjectGuid(searchUsernametxt.Text, GUIDtxt);
            ActiveDirectory.GetObjectSID(searchUsernametxt.Text, SIDtxt);
            ActiveDirectory.PasswordExpirationDate(searchUsernametxt.Text, passlstsettxt);
            ActiveDirectory.LockoutCheck(searchUsernametxt.Text, metroCheckBox1);
            ActiveDirectory.GetEmployeeID(searchUsernametxt.Text, employeeidtxt);
            ActiveDirectory.GetAttributed(searchUsernametxt.Text, cusatt1txt, cusatt2,
                cusatt3, cusatt4);
        }
    }
}
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Tom Dobing
  • 13
  • 2
  • *Is there an easier way of doing this* - not really, but everyone has different definitions of "easy" - what's yours? – Caius Jard Sep 03 '21 at 07:54

2 Answers2

2

I think in this case the better solution is to create a wrapper class

public static class ActiveDirectoryWrapper
{
    public static void DoTheStuff(NeededProperties properties)
    {
       ActiveDirectory.GetsamAccountName(NeededProperties .searchUsernametxt, NeededProperties.samAccountNametxt);
       ...
       ...
       ...
    }
    
    public static boolean DoesUserExist(string searchUsernametxt)
    {
        ...
    }
}

and then call it from your main class

if (ActiveDirectoryWrapper.DoesUserExist(searchUsernametxt.Text) ==true)
{
    ActiveDirectoryWrapper.DoTheStuff({searchUsernametxt.Text,....});
}

it's more reusable and extendable this way

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Ehsan Kiani
  • 3,050
  • 1
  • 17
  • 19
0

Ehsan Kiani's answer is safer in your case


You can use reflection for this. It would be better if you create custom attribute, and give it to functions you want to "bulk call". Then, get all functions with your custom attribute, at last, call them by iterating over them, for example, using foreach loop.

All helpful links are given to research and solve the problem! Good luck!

Alvan Rahimli
  • 348
  • 4
  • 10
  • 1
    Yeah but every one of those method calls requires different arguments, so then you're getting into some complicated way to reflectively call methods and supply a range of different arguments ..which ends up no less complex and much less clear than just straight calling the methods – Caius Jard Sep 03 '21 at 07:53
  • Yes your are right. That is why I noted there that :D – Alvan Rahimli Sep 03 '21 at 12:17