-1

I am trying to program a Philips HUE bulb but I am not even able to send a command to the light. I am programming in C# with the Q42.HueApi.

This is how I tried to turn on the lights if I press a button in my WinForms Application:

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            IBridgeLocator locator = new HttpBridgeLocator();
            ILocalHueClient client = new LocalHueClient("10.1.1.150");

            string AppKey = "myappkey";
            client.Initialize(AppKey);
        }

        void commandCreation(object sender, EventArgs e)
        {
        var command = new LightCommand();
        command.On = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ILocalHueClient.SendCommandAsync(command);
        }
    }
}

But at the last line I get the Compiler Error CS0103.

tk421
  • 5,775
  • 6
  • 23
  • 34
cxes
  • 9
  • 5
  • 2
    Please make your title more descriptive of the actual problem you face. And include the actual error message, not just the error code. Not all of us have the code memorized by heart. – mason Mar 29 '18 at 14:23
  • 4
    Believe it or not; most of us do not memorize error codes. Please post the *actual* error. – BradleyDotNET Mar 29 '18 at 14:23
  • You need to share the error message too. – Chetan Mar 29 '18 at 14:23
  • 6
    @cxes It looks to me as if command is not declared inside button1_Click – Ryan Wilson Mar 29 '18 at 14:23
  • `command` is not defined in `button1_Click` – Chetan Mar 29 '18 at 14:26
  • Error CS0103 : The name 'xxxx' does not exist in the current context is the definition of this error code, from MSDN (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0103), so it is indeed coming from the fact that command is not declared within button1_Click – Ryan Wilson Mar 29 '18 at 14:26
  • Welcome to SO! When posting a question try and keep your title as specific as possible. Including your runtime isn't necessary if you used the appropriate tag. Code snippets are just for JS that can be run in the browser. You do not have to indicate the language if you have (just) one language tag. Keep your question as concise as possible; you do not have to explain your situation. – Maarten Bodewes Mar 29 '18 at 14:29

1 Answers1

1

See comments in code

void commandCreation(object sender, EventArgs e)
{
    var command = new LightCommand(); //  <== because you declare it HERE
    command.On = true;
}

private void button1_Click(object sender, EventArgs e)
{
        ILocalHueClient.SendCommandAsync(command); // ^^ command is out of scope HERE.
}

Also, it seems you are calling SendCommandAsync like static function. It may be that you need to call this on the 'client' instance, which you should make a class field:

public partial class Form1 : Form
{

     private ILocalHueClient client
     ....

    private void button1_Click(object sender, EventArgs e)
    {
        client.SendCommandAsync(command);
    }

And "SendCommandAsync" hints at it is an Async method. So you may want to await it:

    private async void button1_Click(object sender, EventArgs e)
    {
        // assuming command is a field ...
        await client.SendCommandAsync(command);
    }

EDIT:

it's actually

public Task<HueResults> SendCommandAsync(
              LightCommand command, 
              IEnumerable<string> lightList = null)

So you can even explore the HueResults and for example see if your command has been successful.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • You left out part of it. `ILocalHueClient` is a type, not an instance of a type. Probably need to move call `SendCommandAsync` via the instance of a client. Which means the client needs to be a field. – mason Mar 29 '18 at 14:28
  • As @mason said `ILocalHueClient` is a type not an instance, change it to `client.SendCommandAsync(command);` – phuzi Mar 29 '18 at 14:30
  • Thanks guys for your fast help! – cxes Mar 29 '18 at 14:38
  • @mason I also added my suspicion it could be a TAP-Method ( async Task ), inferred from naming "SendCommand**Async** ". – Fildor Mar 29 '18 at 14:42