简体   繁体   中英

Call a non-static method in another Class/Form

I'd like call a non static method in an another class. The method what i want call change the own Windows Form controls Text properties. It's in code:

private void Valtas_angolra()
{
    //Angol kultúra értékül adása a 'cul' változónak.
    cul = CultureInfo.CreateSpecificCulture("en-US");

    //Egyes elemek 'Text' tulajdonságainak beállítása a 'Res.en.resx' fájlból.
    this.Text = res_man.GetString("Termekek_kezelese_From", cul);
    Termek_adatok_Groupbox.Text = res_man.GetString("Termek_adatok_Groupbox", cul);
    Termekkod_Label.Text = res_man.GetString("Termekkod_MIND_Label", cul);
    Termeknev_Label.Text = res_man.GetString("Termeknev_MIND_Label", cul);
}

How you can see the method get the Texts from a .resx file.

Now i'd like this function call in an another class, for example:

namespace EcoHelp
{
    class Kozos_fuggvenyek
    {
         public static void call_nonstatic()
         {
               //calling here
         }
     }
}

I know that I can with a new instance, but here comes the problem, because the Form where the "Valtas_angolra" method is declared, already opened. So i need use the old instance if it's possible.

If you need more details just ask.

Pass your static method the instance of the form you want to change.

EDIT:

class Kozos_fuggvenyek
    {
         public static void call_nonstatic(Form yourForm)
         {
               //Do what you want to your form.
         }
     }

and then you call it like this:

 private void Valtas_angolra()
    {
          Kozos_fuggvenyek.call_nonstatic(this);
    }

Your Valtas_angolra() function is marked as private, so even if you got an instance of your form, you won't be able to call it in this other class. If you change it to public, it should be able to, once you have an instance of the form.

Try giving your current function access to the form by passing it through an added parameter.

You can try this:

namespace EcoHelp
{
    class Kozos_fuggvenyek
    {
        public static void call_nonstatic(Form form)
        {
           //Angol kultúra értékül adása a 'cul' változónak.
           var cul = CultureInfo.CreateSpecificCulture("en-US");

           //Egyes elemek 'Text' tulajdonságainak beállítása a 'Res.en.resx' fájlból.
           form.Text = form.res_man.GetString("Termekek_kezelese_From", cul);
           form.Termek_adatok_Groupbox.Text = form.res_man.GetString("Termek_adatok_Groupbox", cul);
           form.Termekkod_Label.Text = form.res_man.GetString("Termekkod_MIND_Label", cul);
           form.Termeknev_Label.Text = form.res_man.GetString("Termeknev_MIND_Label", cul);
        }
    }
}

You have to expose these fields as public properties inside the class of Valtas_angolra()

  • Termek_adatok_Groupbox
  • Termekkod_Label
  • res_man

Then call it like this:

private void Valtas_angolra()
{
    Kozos_fuggvenyek.call_nonstatic(this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM