-1

I wonder if anyone could help me with this. I'm getting an error saying "The name 'client' does not exist in the current context". This is in reference to the line var tradePileResponse = await client.GetTradePileAsync(); now I know why it's occuring but I'm not sure how to fix it.

I'm initializing client with var client = new FutClient(); but I can't seem to use it across all of my app.

How can I make this instance available throughout my app? Do I need to create the new instance somewhere else? I tried calling it just after the class but it complained that "The contextual keyword 'var' may only appear within a local variable declaration"

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

            public async void button1_Click(object sender, EventArgs e)
            {
                var client = new FutClient();
                var loginDetails = new LoginDetails(email, password, secret, platform);
                try
                {
                    var loginResponse = await client.LoginAsync(loginDetails);
                    var creditsResponse = await client.GetCreditsAsync();
                    label1.Text = creditsResponse.Credits.ToString();
                }
                catch (Exception ex)
                {
                    this.textBox4.Text = ex.Message;
                    //throw;
                }
            }

            private async void butGetTradepile_Click(object sender, EventArgs e)
            {
                //var client = new FutClient();
                var tradePileResponse = await client.GetTradePileAsync();
                Console.WriteLine(tradePileResponse);
            }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ravvles
  • 1
  • 2
  • Do you want a form level variable or a program wide variable? For a global you want to use the word static. http://stackoverflow.com/questions/2445436/global-variables-in-c-net http://stackoverflow.com/questions/14368129/c-sharp-global-variables – Jerry Jeremiah Oct 12 '14 at 22:45

2 Answers2

0

Your issue is scope. Your declaring the variable in the button1_Click(...) method, at the end of the method the variable no longer exist, because of garbage collection. So when the other method gets called the variable does not exist. Try the following but instead of using "var" use the actual type the method returns such as string, int, ..., whatever. If FutClient is a class, I believe it is, use "FutClient client". Then set it to a new instance of the class wherever you like.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    FutClient client;//<-- declared it here

    public async void button1_Click(object sender, EventArgs e)
    {
        client = new FutClient();// <--I changed this
        var loginDetails = new LoginDetails(email, password, secret, platform);
        try
        {
            var loginResponse = await client.LoginAsync(loginDetails);
            var creditsResponse = await client.GetCreditsAsync();
            label1.Text = creditsResponse.Credits.ToString();
        }
        catch (Exception ex)
        {
            this.textBox4.Text = ex.Message;
            //throw;
        }
    }

    private async void butGetTradepile_Click(object sender, EventArgs e)
    {
        //var client = new FutClient();
        var tradePileResponse = await client.GetTradePileAsync();
        Console.WriteLine(tradePileResponse);
    }
}
Steve
  • 21
  • 4
0

You need to move the declaration of client out of the button1_Click() method and make it a class-level property.

public partial class Form1 : Form
{
    private FutClient _client;

    public Form1()
    {
        InitializeComponent();
        _client = new FutClient();
    }
}

Now you can use _client from both button1_Click() and butGetTradepile_Click()

Brendan Green
  • 11,676
  • 5
  • 44
  • 76