0

I've been working on a console application for over two years, and although the code is always being improved/extended, I thought it would be helpful to add a GUI to it. After some initial stumbling blocks, I now have a basic GUI (windows forms using CLI). Initially, I tried to get input from the GUI from within my "main" function (It was my main function from the console app). Then I read that this is not a good idea, and I realised also that I don't want the input to my function potentially change after I click my "Run" button.

So basically, I'm wondering what the best programming practice is for this. The only way I really know how to do this is to open my "main" function from within my button_click event, and pass the values obtained from textboxes and checkboxes to it.

I'm not even aware of another way, but this seems way too sloppy. There could potentially be over 100 values passed to the main function, and I've never seen a function with this many arguments. In the past, I hardcoded them into my function, or used cin to alter them at runtime.

Any advice on this would be much appreciated. I don't want to continue doing what I'm doing if it's poor programming practice. I should also mention that my programming style is usually procedural, as I don't have much knowledge of OOP.

Here is an example to illustrate how I'm doing it at the moment:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    if ( this->Input1->Text->Length == 0 ) 
        this->Input1->Text = "Enter number";
    else{
        String^ In1Str = this->Input1->Text;
        double In1 = Convert::ToDouble(In1Str);
        if(this->CBshowConsole->Checked)
            showConsole();
        main(In1);
    }
}

So, to reiterate, the way I would do it from here, would be to basically do this for Input2, Input3, Input4, etc., and then pass In2, In3, In4, etc. to my main function.

4

0 回答 0