Search code examples
c#visual-studiomodel-view-controller

How to export a procedure outside designer in C# / VS Community


I'm a complete newbie with C# and VS Community, while I'm not really a newbie in OOP and programming in general. I'm just trying to dig into my 20-years-old lack of update into best practices... While I'm happy with my mammoth aged skills in C or HDL...

I have done a micro-solution to text the size of a set of files with this code:

    private void button2_Click(object sender, EventArgs e)
    {
        // Create an instance of the open file dialog box.
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Multiselect = true;
        DialogResult userClickedOK = openFileDialog1.ShowDialog();
        if (userClickedOK == DialogResult.OK)
        {
            System.IO.Stream test = openFileDialog1.OpenFile();
            var size = test.Length.ToString();
            toolStripStatusLabel1.Text = string.Format("Size: {0} bytes", size);
        }

I want to transform the portion of code that deals with action to do if userClickedOK == DialogResult.OK with something like this:

if (userClickedOK == DialogResult.OK)
        {
            something.do();
        }

And my question is .... tada ... where (in which file or via which menu) do I declare the class something and the procedure do?

I apologize, but I'm ready with vi and nano, but IDEs are tools made by wizards for wizards ...


Solution

  • To make your code running you have to either add a extra file (Code file) to your project in which you code your class and implement your functions or you take your main.cs file and insert your code after the main class

    To add another file just press Ctr + Shift + A or go to Project -> Add Class -> Class -> Edit Name -> Add

    Then open the file from the solution explorer and make your implementation

    To make your class and the functions available from a DLL, simply do the same but make sure your class Something is public. then you cann add a reference from another project to your *.exe and import the namespace for using and instanciating the class