Announcement

Collapse
No announcement yet.

Pass Parameters to VB Script

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

    Pass Parameters to VB Script

    Can someone enlighten a "learn by example" programmer on the method to use parameters passed by Homeseer within a VB.Net script? You'd save me a few hours of internet searching and experimenting.
    I'd like to consolodate several scripts which work with similar types of devices into one.

    Thanks
    Real courage is not securing your Wi-Fi network.

    #2
    HS2 event -> run script:
    Package_Tracker.vb("main","youremail@yourisp.com;example title param;123455679")
    VB.net script code:
    Code:
    Sub main(ByVal sFromItemTrackNum As String)
        Dim sFrom As String
        Dim Item As String
        Dim TrackingNum As String
        Dim arrParm() As String
    ...
    
        arrParm = Split(sFromItemTrackNum, ";", -1, 1)
        sFrom = arrParm(0)
        Item = arrParm(1)
        TrackingNum = arrParm(2)
    ...
    end sub
    I did a simple conversion / update to the package tracker script as a text of my less than mad skillz at vb.net [] script conversion. I wanted an email notification if the new tracking results were updated relative to the last tracking status check. Works, but still thinking I'd like a few more features as the delivery time approaches ... []
    huggy_d1

    Automating made easy

    Comment


      #3
      I knew it would be embarrasingly simple
      Thanks.
      Real courage is not securing your Wi-Fi network.

      Comment


        #4
        Originally posted by Wadenut View Post
        I knew it would be embarrasingly simple
        Thanks.
        No need for embarrassment... I had to figure it out with the split command thing after having tried passing multiple parameters and finding no way to do that... It looks simple now but it did not start that way
        huggy_d1

        Automating made easy

        Comment


          #5
          That was about to be my next question... Passing multiple parameters without the parsing. No matter. Whatever works. Thanks again.
          Real courage is not securing your Wi-Fi network.

          Comment


            #6
            Wait a minute... That was a cross between VBScript and VB.NET Script.

            In VB.NET when you create a new script in HomeSeer (in the event editor), note that it puts in the first line like this:

            Sub Main(parm as Object)

            Which is the same as: Sub Main(ByVal Parm As Object)

            So here is the thing - if you have an array of items as parameters, and some are different variable types like true objects, date variables, etc. then the forced conversion to string with the way you have it will not work properly.

            Technically, the parameter passed is an array of objects, but instead of defining it as ByVal Parms() as Object, we probably were assuming most call a single parameter so we made it Parm As Object so you would not have to mess with referencing Parms(0) all the time.

            So, you can avoid improper type conversion and access the array more directly if you define the prototype as this:

            Sub Main(ByVal Parms() As Object)

            Then you can test with UBound(Parms) or Parms.Length to see how many elements (parameters) are in the array, and this will also keep the parameters with their proper type definitions.
            Regards,

            Rick Tinker (a.k.a. "Tink")

            Comment


              #7
              Originally posted by Rick Tinker View Post
              Wait a minute... That was a cross between VBScript and VB.NET Script.

              In VB.NET when you create a new script in HomeSeer (in the event editor), note that it puts in the first line like this:

              Sub Main(parm as Object)

              Which is the same as: Sub Main(ByVal Parm As Object)

              So here is the thing - if you have an array of items as parameters, and some are different variable types like true objects, date variables, etc. then the forced conversion to string with the way you have it will not work properly.

              Technically, the parameter passed is an array of objects, but instead of defining it as ByVal Parms() as Object, we probably were assuming most call a single parameter so we made it Parm As Object so you would not have to mess with referencing Parms(0) all the time.

              So, you can avoid improper type conversion and access the array more directly if you define the prototype as this:

              Sub Main(ByVal Parms() As Object)

              Then you can test with UBound(Parms) or Parms.Length to see how many elements (parameters) are in the array, and this will also keep the parameters with their proper type definitions.
              Ok, so how should we pass multiple parameters between scripts? The package tracker 'creation' script has 3 params to send to the tracker script. I saw the solution somewhere else in another .vb(.net) script, tested, and smiled when it worked.

              Am I to understand having the 3 parameters as I previously posted are actually array elements and instead of doing a string split, I could directly access them and skip the split? How exactly would that look?
              huggy_d1

              Automating made easy

              Comment


                #8
                Is this the way?
                -----------------------
                HS2 event -> run script:

                VB.net script code:
                Code:
                Sub main(aFromItemTrackNum As Object)
                    Dim sFrom As String
                    Dim sItem As String
                    Dim sTrackingNum As String
                
                ...
                
                    If UBound(aFromItemTrackNum) > 2 then
                      sFrom = aFromItemTrackNum(1)
                      sItem = aFromItemTrackNum(2)
                      sTrackingNum = aFromItemTrackNum(3)
                    End If
                
                ...
                
                end sub
                huggy_d1

                Automating made easy

                Comment


                  #9
                  Originally posted by Rick Tinker View Post
                  So, you can avoid improper type conversion and access the array more directly if you define the prototype as this:

                  Sub Main(ByVal Parms() As Object)
                  Rick,
                  So, can we pass multiple parameters in the Run Script action specification of an event? I.e.:

                  my_script("Main","Parm1","Parm2","Parm3")

                  I thought it was like hs.RunEx, in that you could only pass the sub name and one argument. If you are saying we can pass multiple arguments, how do you specify it?

                  Steve

                  Comment


                    #10
                    Stevea,

                    Sorry for the delay - somebody else just told me about this discussion ending with your question - I did not see the post.

                    The thread has an example - you can NOT pass multiple parameters as arguments to the script, but you CAN pass a SINGLE parameter that is an array of objects, and that array can then be your multiple parameters.

                    An object is a variable type that essentially means "any" - it can be a class object, a date variable, an integer variable, a string variable, etc.

                    When you only get ONE parameter to pass, you make that parameter an array, and that gets around this.

                    Note: All of this applies ONLY to VB.NET scripts - there is a way to do it with the older scripts, but it is nowhere as clean and easy.

                    Read through the whole thing - huggy_d1 has it right in his question and example script.

                    If you still have questions, let me know and I'll post another example.
                    Regards,

                    Rick Tinker (a.k.a. "Tink")

                    Comment


                      #11
                      Great information - helped me write one script to set the volume of multiple zones of music using an array as a passed parameter. Saved me alot of head scratching and experimentation.

                      Sonny

                      Comment


                        #12
                        Sorry to revive an old thread but I'm having similar issues. I'd like to call a script and pass some parameters to it, a combination of Objects (some integers and some strings)

                        My event calls a script with like this
                        Code:
                        foo.vb("main","myString,99")
                        My VB file has the following function:

                        Code:
                        Public Sub Main(ByVal parms() as Object)
                        However I get the following error when I call the script:

                        Scripting runtime error: System.ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Object[]'. at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) at ....

                        So it looks like at runtime it is trying to cast my single string "myString,99" to an Object and failing.

                        I know I can change my function to receive parms as String and then use some jiggery pokery to split them out, but that's a little messy. Can anyone tell me what I'm missing please?
                        Author of Highpeak Plugins | SMS-Gateway Plugin | Blue Iris Plugin | Paradox (Beta) Plugin | Modbus Plugin | Yamaha Plugin

                        Comment


                          #13
                          Read my post #10 below - you cannot call it and pass it an array unless you call it from another script. When you run a script and pass it a parameter, you only get ONE.

                          Call it as you have been:
                          Code:
                          foo.vb("main","myString,99")

                          But define the receiving script as:
                          Code:
                          Public Sub Main(ByVal parm as Object)

                          Then in the script, do this:

                          Code:
                          Dim parms() As String
                          If parm Is Nothing Then
                              (error message, exit script...)
                          End If
                          parms = Split(parm.ToString,",")
                          Now parms is an array of string objects that were passed as a single comma separated string.
                          Regards,

                          Rick Tinker (a.k.a. "Tink")

                          Comment


                            #14
                            What about a VBscript? I want to pass multiple parameters to a script.

                            Comment


                              #15
                              Originally posted by thecatsandi View Post
                              What about a VBscript? I want to pass multiple parameters to a script.
                              Do it the same way and use the vb script split command to split the individual pieces of the passed parameters.
                              💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

                              Comment

                              Working...
                              X