Announcement

Collapse
No announcement yet.

Designer passing parameters to script

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

    Designer passing parameters to script

    Hey all

    I'm really confused by this. Read some threads and it seems more confusion and not sure how to get this working.

    Just want designer to 'run a script' ( and pass my own params) and/or ' run a script with value from elements'

    Latest test was this; which throws an error; this may be not what I want 🙂

    Public Sub Main(ByVal ParmArray() As String)

    hs.CreateVar("current_screen")
    'hs.SetVar("current_screen",ParmArray(0))

    hs.Writelog(ParmArray(0))

    End Sub


    Running script set screen.vb :Exception has been thrown by the target of an invocation.->Does entry point Main exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Scheduler.clsRunVBNetScript.ExecuteScript()

    #2
    There is a big difference between how parameters are passed when called from an HS event versus from HS Touch. The first passes the parameters as a single string (and you have to manually separate the parameters if there is more than one), where HS touch passes the parameters as an array of strings. This is why it is recommended to define the subroutine parameter to be of type "object", as an object will handle either case.

    The following is the code I use (and include in any source file that needs it) that will parse either case into something consistant.

    Code:
    Private Function TouchParseStr(ByVal parms As Object, ByVal sSep As String, Optional sDef As String = "") As String()
    ' Takes the input parameter, and returns an array of strings.
    ' If called from event, parms is a single string, so does a split on the specified delimiter.
    ' If called from HS Touch, just returns the string array.
    ' Usage: Sub Main(parms As Obect)
    '          Dim p() As String = TouchParse(parms, ",") ' p(0) now contains first, p(1) second, etc
      Dim buff() As String
      If parms Is Nothing Then parms = ""
      If parms.GetType().ToString = "System.String" Then
        If parms = "" Then parms = sDef
        parms = Trim(parms.ToString())
        buff = parms.Split(sSep)
      Else
        buff = parms
      End If
      Return buff
    End Function
    Then an example of how this is used:

    Code:
    Public Sub DoSomething(parms As Object)
      Dim p() As String = TouchParseStr(parms, ".")
      If p(0) <> "" Then
      ...

    Comment


      #3
      Originally posted by aa6vh View Post
      There is a big difference between how parameters are passed when called from an HS event versus from HS Touch. The first passes the parameters as a single string (and you have to manually separate the parameters if there is more than one), where HS touch passes the parameters as an array of strings. This is why it is recommended to define the subroutine parameter to be of type "object", as an object will handle either case.

      The following is the code I use (and include in any source file that needs it) that will parse either case into something consistant.

      Code:
      Private Function TouchParseStr(ByVal parms As Object, ByVal sSep As String, Optional sDef As String = "") As String()
      ' Takes the input parameter, and returns an array of strings.
      ' If called from event, parms is a single string, so does a split on the specified delimiter.
      ' If called from HS Touch, just returns the string array.
      ' Usage: Sub Main(parms As Obect)
      ' Dim p() As String = TouchParse(parms, ",") ' p(0) now contains first, p(1) second, etc
      Dim buff() As String
      If parms Is Nothing Then parms = ""
      If parms.GetType().ToString = "System.String" Then
      If parms = "" Then parms = sDef
      parms = Trim(parms.ToString())
      buff = parms.Split(sSep)
      Else
      buff = parms
      End If
      Return buff
      End Function
      Then an example of how this is used:

      Code:
      Public Sub DoSomething(parms As Object)
      Dim p() As String = TouchParseStr(parms, ".")
      If p(0) <> "" Then
      ...
      Thanks. I did read that things were dealt with differently between HS event and HS touch ( did originally have object in my main ).
      I'll give it a go tomorrow and report back.

      Much appreciated!

      Comment


        #4
        Originally posted by aa6vh View Post
        There is a big difference between how parameters are passed when called from an HS event versus from HS Touch. The first passes the parameters as a single string (and you have to manually separate the parameters if there is more than one), where HS touch passes the parameters as an array of strings. This is why it is recommended to define the subroutine parameter to be of type "object", as an object will handle either case.

        The following is the code I use (and include in any source file that needs it) that will parse either case into something consistant.

        Code:
        Private Function TouchParseStr(ByVal parms As Object, ByVal sSep As String, Optional sDef As String = "") As String()
        ' Takes the input parameter, and returns an array of strings.
        ' If called from event, parms is a single string, so does a split on the specified delimiter.
        ' If called from HS Touch, just returns the string array.
        ' Usage: Sub Main(parms As Obect)
        ' Dim p() As String = TouchParse(parms, ",") ' p(0) now contains first, p(1) second, etc
        Dim buff() As String
        If parms Is Nothing Then parms = ""
        If parms.GetType().ToString = "System.String" Then
        If parms = "" Then parms = sDef
        parms = Trim(parms.ToString())
        buff = parms.Split(sSep)
        Else
        buff = parms
        End If
        Return buff
        End Function
        Then an example of how this is used:

        Code:
        Public Sub DoSomething(parms As Object)
        Dim p() As String = TouchParseStr(parms, ".")
        If p(0) <> "" Then
        ...
        Seems to work! Thank-you so much!!!. Always learning !😁

        Comment

        Working...
        X