Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Refactor browse file methods to one method

Hi I have several browse methods for parameters which only differs in which setting and which textbox is used. Can i create a lambda or generic method or something to have only one implementation in C#?

    private void buttonBrowseSrcXML_Click(object sender, EventArgs e) {
        OpenFileDialog fileDialog = new OpenFileDialog();

        try {
            fileDialog.InitialDirectory = Path.GetDirectoryName(Settings.Default.XML_Original_File);
        } catch (ArgumentException) { }

        if (fileDialog.ShowDialog() == DialogResult.OK) {
            Settings.Default.XML_Original_File = fileDialog.FileName;
            Settings.Default.Save();
            textBoxSrcXML.Text = fileDialog.FileName;
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

yes, you can move this code into a common method and then pass the setting and textbox to this method and call in each of your methods like the one below.

BrowseFile("XML_Original_File", textBoxSrcXML);

you will need to use these values accordingly in this method.

the method’s signature should be something like private void BrowseFile(string settingName, TextBox textbox)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading