Return Windows Forms value

0

I have an application that must return a value to the method that invokes it, I have been researching but most examples are of console applications and mine is of Forms.

I have tried this way:

Program.cs

 [STAThread]
    static string Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Formulario());

        response = Formulario.resp;

        return response;
    }

and in Formulario.cs:

internal static string resp;

protected void Page_Load(object sender, EventArgs e)
{
   resp = "valor devuelto";
}

I even tried:

protected string Page_Load(object sender, EventArgs e)
{
   return resp = "valor devuelto";
}

But when trying to execute it, it marks me:

"El programa no contiene ningún método Main estático adecuado para un punto de entrada"

Any idea how I could send the value to the method that my application invokes? Thanks

    
asked by EriK 20.04.2018 в 17:29
source

1 answer

0

I have had the same question, and at first I did it as suggested by Bercklyn, but my colleagues recommended me not to work with public properties in that way, but declaring them private, but with a public property method to access their value. The result is very similar to what has already been seen in this question and answer, but it serves as an alternative, a little bit more secure, in the case, for example, that you do not want this property to be modified from outside the form, etc. .

This applied to your code, it would be something like:

Program.cs

 [STAThread]
    static string Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Formulario());

        response = Formulario.Valor;

        MessageBox.Show(response);
    }

Form.cs:

private string resp;
public string Valor { get { return resp; } }

// Puedes asignarle el valor incluso ya en el constructor
// O puedes asignarlo en cualquier otra parte del Form
public Formulario()
{
   resp = "valor devuelto";
}
private void Form_Load(object sender, EventArgs e)
{
   resp = "valor devuelto";
}
    
answered by 20.04.2018 в 18:46