-2

Sorry I use this title on purpose. I m quite new on c# and i don't manage to call my class on the usual way.

I have a winform and I want to insert a class (copy and paste from an API), and call it from my winform.

This class is a program which open the console, ask me parameters and then lunch an EMG acquisition.

the class is like that:

  class Program1     
        {
            static void Main(string[] args)
            { 
// some code here, with  a lot of "Console.Writeline"
            }
        }

It works fine.

I change it to:

  public class Program1     
            {
               public void method1(string[] args)
                { 
//some code here, , with  a lot of "Console.Writeline"
                }
            }

And i tried to call it on my form

 private void button1_Click(object sender, EventArgs e)
        {
            Program1 program = new Program1();
            program.method1();
        }

it says "No overload for method 'method1' takes 0 arguments" . The thing i don"t understand is when i run the program of the API alone it doesn't ask me anything it just runs.

I'm pretty sure the error is from the Console.WriteLine and Console.Readline but i don't know how to resolve my problem. I want a button on my winform which run my class, open the console and ask me on the console which paramaters i want.

Thank you !

jsls
  • 251
  • 3
  • 16
  • 3
    dont forget "string[] args", your input is not void. "program.method1(new string[] {"a","b","c"});" –  Jul 11 '16 at 08:49
  • 1
    This is a problem because, even if you call the method, you don't have a console because you're running a winforms application. – Alessandro D'Andria Jul 11 '16 at 08:53
  • Ok the program runs now (thank you) ! But nothing happens, it just freezes!! – jsls Jul 11 '16 at 08:53
  • You can't call a console on a winform?! Why not?! – jsls Jul 11 '16 at 08:54
  • Why don't you build the code into your WinForms application? Does it have to run in a console? – Dominik B Jul 11 '16 at 08:55
  • 3
    You can, check [this](http://stackoverflow.com/questions/2593313/how-to-execute-console-application-from-windows-form) and [this](http://stackoverflow.com/questions/1275403/run-a-console-application-from-a-windows-form) – Mong Zhu Jul 11 '16 at 08:55
  • `"No overload for method 'method1' takes 0 arguments"` -- the error tells you exactly the problem. You're calling the method as if it has an overload that takes 0 arguments, but there is no such overload. It works when you run the program because the runtime _passes an argument_! I.e. it doesn't try to do the invalid thing you are trying to do. It sounds like you just want to run the program as a separate process (i.e. with a console window, per the links from Mong's comment), but your question isn't very clear. – Peter Duniho Jul 11 '16 at 08:59
  • @DominikB I wanted to do it like that first, but the API is for experienced programmer and modify it is quite complicated... – jsls Jul 11 '16 at 09:00

1 Answers1

2

Method overloading is a feature in OOP. which allow you to write multiple methods of the same name but with the different parameters, during invocation it pick the correct one by the parameters.

You can try

passing the specified parameters which is string[] args as eg.

public class Program1     
{
    public void method1(string[] args){ 
        //some code here, , with  a lot of "Console.Writeline"
    }
}

private void button1_Click(object sender, EventArgs e){
    Program1 program = new Program1();
    program.method1(new string[]{"one","two"});
}

Or Try with optional parameters as eg.

public class Program1     
{
    public void method1(string[] args=null){ 
        //some code here, , with  a lot of "Console.Writeline"
    }
}

private void button1_Click(object sender, EventArgs e){
    Program1 program = new Program1();
    program.method1();
}
Nitesh Shaw
  • 216
  • 2
  • 17