Gabor
Gabor

Reputation: 47

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.

Upvotes: 0

Views: 1377

Answers (3)

Wasif Hossain
Wasif Hossain

Reputation: 3940

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);
}

Upvotes: 0

Idov
Idov

Reputation: 5124

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);
    }

Upvotes: 1

user3288049
user3288049

Reputation: 124

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.

Upvotes: 1

Related Questions