Announcement

Collapse
No announcement yet.

C Sharp script example with parameters

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C Sharp script example with parameters

    Could someone please post a C# script snippit that will take multiple parameters with with comma separated values?
    I am learning to write my HS3 apps in this language and have had pretty good sucess with a self contained script, I just am not sure on receiving parameters.

  • #2
    HS passes in only one "string", so you have to manually separate your parameters by using a "Split" statement. Note however, passing parameters in from HS Touch will pass in an Object that consists of an array of strings, which is different from HS.

    Parsing a string in VB .net:

    Code:
    Sub Main(parms As Object)
      Dim parm() As String = Split(parms,",")
    The C# version would be:

    Code:
    public void Main(object parms) {
      string[] parm = parms.Split(",")
    Hope this helps.

    Comment


    • #3
      Thank you, that starts to help me:

      I'm not able to get this to execute, starting with the parms.Split(","); but also I can't get the variable to set windowDoorOpen

      Here's what I'm trying to create

      Code:
      using System;
      using System.Collections.Generic;
      public void Main(object line) {
      String[] parm = line.Split(",");
      var windowDoorOpenVar = hs.GetVar("WindowDoorOpen");
      if ( windowDoorOpenVar.Size == 0 || windowDoorOpenVar == null ) {
      hs.CreateVar("WindowDoorOpen");
      List<string> windowDoorOpen = new List<string>();
      } else {
      List windowDoorOpen = windowDoorOpenVar;
      }
      
      switch ( parm[0] ) {
      case "Open":
      windowDoorOpen.Add(parm[1]);
      break;
      case "Closed":
      windowDoorOpen.Remove(parm[1]);
      break;
      }
      hs.SaveVar("WindowDoorOpen",windowDoorOpen);
      }
      It takes the parameter Open,F2LivingRoomLeftWindow and the result is the global variable WindowDoorOpen should be saved as a list of all values, or have the specified falued if the parameter is Closed,F2LivingRoomLeftWindow

      And here is the error:

      Code:
      Compiling script /usr/local/HomeSeer/scripts/WindowDoor_Open.cs: {interactive}(21,26): error CS1061: Type `object' does not contain a definition for `Split' and no extension method `Split' of type `object' could be found (are you missing a using directive or an assembly reference?) {interactive}(25,28): error CS1061: Type `object' does not contain a definition for `Size' and no extension method `Size' of type `object' could be found (are you missing a using directive or an assembly reference?) {interactive}(29,9): error CS0246: The type or namespace name `List' could not be found. Are you missing a using directive or an assembly reference? {interactive}(33,9): error CS0246: The type or namespace name `List' could not be found. Are you missing a using directive or an assembly reference? {interactive}(45,13): error CS0103: The name `windowDoorOpen' does not exist in the current context {interactive}(45,28): error CS0023: The `.' operator cannot be applied to operand of type `' {interactive}(53,13): error CS0103: The name `windowDoorOpen' does not exist in the current context {interactive}(53,28): error CS0023: The `.' operator cannot be applied to operand of type `' {interactive}(61,33): error CS0103: The name `windowDoorOpen' does not exist in the current context {interactive}(61,8): error CS1502: The best overloaded method match for `Scheduler.hsapplication.SaveVar(string, object)' has some invalid arguments {interactive}(11,17): warning CS0414: The private field `Script.hs' is assigned but its value is never used

      Comment


      • #4
        I think I forgot to have you convert the incoming object into a string. Sorry.

        string[] parm = line.ToString().Split(",");

        My C# is a little rusty, but I think that will work. If not, you could do:

        Code:
        public void Main(object parms) {
        string line = parms.ToString();
        string[] parm = line.Split(",");
        Also remember that C# is case sensitive. "String" is not the same thing as "string".

        Comment

        Working...
        X