Redmancometh
Redmancometh

Reputation: 76

Get Value in Parameter for Object Listener in GTK#

Hey guys I'm fairly new to programming in general, and I've run into an issue when creating a GUI with Xamarin Studio.

So I have a button with an event handler:

        Button gbutton = new Button("Generate");
        gbutton.Clicked += new EventHandler (Generate);

And it calls the method Generate: //test method
static void Generate (object obj, EventArgs args, int x) { Console.WriteLine ("Generate"); }

I want to modify an object of my GUI called table1. I want to use table1.attach. So to access table1 I would need to an a Table to the method parameters. What would be the best way to access the table object from outside the main method where it's declared? I tried to do add int x to the above method parameter, and putting the value both in the parenthesis containing generate, and creating then and adding it ( gbutton.Clicked += new EventHandler (Generate, x); ).

Upvotes: 1

Views: 97

Answers (1)

Merta
Merta

Reputation: 964

You should define Table1 in your class instead of defining it in a method, in this way you can access your Table1 in every method you want.

Table table1;
static void Main(string[] args)
{
    //Table table1;  //instead of defining variable in a method, define it in class level.
    table1 = new Table();
}

private static void Generate(/*required arguments*/)
{
    //Here you can access table1:
    Console.WriteLine(table1.ToString());
}

Upvotes: 0

Related Questions