Test my VB Script please

Author Message
Amida

  • Total Posts : 30
  • Scores: 0
  • Reward points : 0
  • Joined: 5/31/2009
  • Location: Codoria
  • Status: offline
Test my VB Script please Sunday, May 31, 2009 6:13 AM (permalink)
0
I've made a VB Script file that seems to work perfectly on my computer and just to be safe I want to see if it also works on other computers, I personally have no doubts about it but my mom does. The bold face text below is the direct code. It finds the x and y values of a line so it can be graphed on a coordinate plane. Please tell me if you find any bugs or how I could improve, an your general opinion.

runs = 0
x = 0
m = InputBox("Enter Value for M", "Slope")
b = InputBox("Enter Value for B", "Slope")
do
y = m * x + b
msgbox "x value "&x& " y value " &y
x = x + 1
runs = runs + 1
loop until runs = 10

 
#1
    ehvbs

    • Total Posts : 3321
    • Scores: 110
    • Reward points : 0
    • Joined: 6/22/2005
    • Location: Germany
    • Status: offline
    RE: Test my VB Script please Sunday, May 31, 2009 11:23 PM (permalink)
    0
    Hi Amida,

    my mom said about your script:

      (1) While it works in principle, InputBox and MsgBox are the worst possible user interface

      (2) The variable names and the prompts are not so good

      (3) The code layout is lousy (no indentation, missing spaces around operators)

    then she forced me to spend a lazy sunday/holiday afternoon writing:

     Option Explicit
     
     '  A common form of a linear equation in the two variables x and y is
     '
     '    y = mx + b
     '
     '  where m and b designate constants (the variable y is multiplied by
     '  the constant 1, which as usual is not explicitly written). The
     '  origin of the name "linear" comes from the fact that the set of
     '  solutions of such an equation forms a straight line in the plane. In
     '  this particular equation, the constant m determines the slope or
     '  gradient of that line; and the constant term c [sic!; should be b]
     '  determines the point at which the line crosses the y-axis.
     '    http://en.wikipedia.org/wiki/Linear_equation
     
     ' Default values for all parameters
       Dim nXFrom  : nXFrom  = 0
       Dim nXTo    : nXTo    = 5
       Dim nSlope  : nSlope  = 1
       Dim nYCross : nYCross = 0
     
     ' Evaluate command line parameters
       Dim oWAN : Set oWAN = WScript.Arguments.Named
       If oWAN.Exists( "nXFrom" ) Then
          checkIntRange oWAN, "nXFrom", nXFrom, 0, 10
          nXTo = nXFrom + 10
       End If
       If oWAN.Exists( "nXTo"    ) Then checkIntRange oWAN, "nXTo"   , nXTo   , nXFrom, nXFrom + 10
       If oWAN.Exists( "nSlope"  ) Then checkIntRange oWAN, "nSlope" , nSlope ,      1,          10
       If oWAN.Exists( "nYCross" ) Then checkIntRange oWAN, "nYCross", nYCross,      0,          10
     
     ' Display parameters
       WScript.Echo "+-------------+"
       WScript.Echo "| nXFrom:", padL3( nXFrom  ), "|"
       WScript.Echo "|   nXTo:", padL3( nXTo    ), "|"
       WScript.Echo "| nSlope:", padL3( nSlope  ), "|"
       WScript.Echo "|nYCross:", padL3( nYCross ), "|"
       WScript.Echo "+------+------+"
       WScript.Echo "|   X  |    Y |"
       WScript.Echo "+------+------+"
     
     ' Display computed values
       Dim nX, nY
       For nX = nXFrom To nXTo
           nY = nSlope * nX + nYCross
           WScript.Echo "| ", padL3( nX ), "| ", padL3( nY ), "|"
       Next
       WScript.Echo "+-------------+"
       WScript.Echo
     
     ' Display defined Line
       Dim aDisplay : aDisplay = createDisplay( nXTo, nSlope, nYCross )
       For nX = nXFrom To nXTo
           nY = nSlope * nX + nYCross
           aDisplay( nX + 1, nY + 1 ) = padL3( "X" )
       Next
       displayDisplay aDisplay
       WScript.Echo
       WScript.Quit 0
     
     ' Create a canvas (two dimensional array) with legend
     Function createDisplay( nXTo, nSlope, nYCross )
       Dim nRows : nRows = 3 + nSlope * nXTo + nYCross
       Dim nCols : nCols = 3 + nXTo
       Dim nRow, nCol
       ReDim aTmp( nCols, nRows )
       aTmp(     0,     0 ) = padL3( "" )
       aTmp( nCols, nRows ) = aTmp( 0, 0 )
       For nCol = 1 To nCols
           aTmp( nCol,     0 ) = padL3( nCol - 1 )
           aTmp( nCol, nRows ) = aTmp( nCol, 0 )
       Next
       For nRow = 1 To nRows
           aTmp(     0, nRow ) = padL3( nRow - 1 )
           aTmp( nCols, nRow ) = aTmp( 0, nRow )
       Next
     
       nRows = nRows - 1
       nCols = nCols - 1
       For nRow = 1 To nRows
           For nCol = 1 To nCols
               aTmp( nCol, nRow ) = padL3( "." )
           Next
       Next
       createDisplay = aTmp
     End Function
     
     ' Display canvas
     Sub displayDisplay( aDisplay )
       Dim nRow, nCol
       For nRow = 0 To UBound( aDisplay, 2 )
           For nCol = 0 To UBound( aDisplay, 1 )
               WScript.StdOut.Write aDisplay( nCol, nRow )
           Next
           WScript.StdOut.WriteLine
       Next
     End Sub
     
     ' Convenience function: padL with hardcoded width
     Function padL3( nVal )
       padL3 = Right( "   " & nVal, 3 )
     End Function
     
     ' Evaluate and check commandline parameters
     Sub checkIntRange( oWAN, sKey, nVar, nFrom, nTo )
       Dim sTmp : sTmp = oWAN( sKey )
       Dim nTmp, nErr, sErr
      On Error Resume Next
       nTmp = CInt( sTmp )
       nErr = Err.Number
       sErr = Err.Description
      On Error GoTo 0
       If 0 <> nErr Then
          sErr = sErr & " (""" & sTmp & """)"
       Else
          If nTmp >= nFrom And nTmp <= nTo Then
             nVar = nTmp
          Else
             nErr = 1
             sErr = "not in range: " & nFrom & " -| " & nTmp & " |- " & nTo
          End If
       End If
       If 0 <> nErr Then
          WScript.Echo "*** error:", sErr & "!", sKey, "=", nVar
       End If
     End Sub
     


    output:

     xplore forum28 computeLine /nYCross:-5
     === computeLine: compute linear equation ===============
     *** error: not in range: 0 -| -5 |- 10! nYCross = 0
     +-------------+
     | nXFrom:   0 |
     |   nXTo:   5 |
     | nSlope:   1 |
     |nYCross:   0 |
     +------+------+
     |   X  |    Y |
     +------+------+
     |    0 |    0 |
     |    1 |    1 |
     |    2 |    2 |
     |    3 |    3 |
     |    4 |    4 |
     |    5 |    5 |
     +-------------+
     
          0  1  2  3  4  5  6  7
       0  X  .  .  .  .  .  .  0
       1  .  X  .  .  .  .  .  1
       2  .  .  X  .  .  .  .  2
       3  .  .  .  X  .  .  .  3
       4  .  .  .  .  X  .  .  4
       5  .  .  .  .  .  X  .  5
       6  .  .  .  .  .  .  .  6
       7  0  1  2  3  4  5  6  7
     
     === computeLine: 0 done (00:00:00) =====================
     
     ############################################
     
     xplore forum28 computeLine /nYCross:3 /nSlope:2 /nXFrom:3 /nXTo:12
     === computeLine: compute linear equation ============================
     +-------------+
     | nXFrom:   3 |
     |   nXTo:  12 |
     | nSlope:   2 |
     |nYCross:   3 |
     +------+------+
     |   X  |    Y |
     +------+------+
     |    3 |    9 |
     |    4 |   11 |
     |    5 |   13 |
     |    6 |   15 |
     |    7 |   17 |
     |    8 |   19 |
     |    9 |   21 |
     |   10 |   23 |
     |   11 |   25 |
     |   12 |   27 |
     +-------------+
     
          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14
       0  .  .  .  .  .  .  .  .  .  .  .  .  .  .  0
       1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  1
       2  .  .  .  .  .  .  .  .  .  .  .  .  .  .  2
       3  .  .  .  .  .  .  .  .  .  .  .  .  .  .  3
       4  .  .  .  .  .  .  .  .  .  .  .  .  .  .  4
       5  .  .  .  .  .  .  .  .  .  .  .  .  .  .  5
       6  .  .  .  .  .  .  .  .  .  .  .  .  .  .  6
       7  .  .  .  .  .  .  .  .  .  .  .  .  .  .  7
       8  .  .  .  .  .  .  .  .  .  .  .  .  .  .  8
       9  .  .  .  X  .  .  .  .  .  .  .  .  .  .  9
      10  .  .  .  .  .  .  .  .  .  .  .  .  .  . 10
      11  .  .  .  .  X  .  .  .  .  .  .  .  .  . 11
      12  .  .  .  .  .  .  .  .  .  .  .  .  .  . 12
      13  .  .  .  .  .  X  .  .  .  .  .  .  .  . 13
      14  .  .  .  .  .  .  .  .  .  .  .  .  .  . 14
      15  .  .  .  .  .  .  X  .  .  .  .  .  .  . 15
      16  .  .  .  .  .  .  .  .  .  .  .  .  .  . 16
      17  .  .  .  .  .  .  .  X  .  .  .  .  .  . 17
      18  .  .  .  .  .  .  .  .  .  .  .  .  .  . 18
      19  .  .  .  .  .  .  .  .  X  .  .  .  .  . 19
      20  .  .  .  .  .  .  .  .  .  .  .  .  .  . 20
      21  .  .  .  .  .  .  .  .  .  X  .  .  .  . 21
      22  .  .  .  .  .  .  .  .  .  .  .  .  .  . 22
      23  .  .  .  .  .  .  .  .  .  .  X  .  .  . 23
      24  .  .  .  .  .  .  .  .  .  .  .  .  .  . 24
      25  .  .  .  .  .  .  .  .  .  .  .  X  .  . 25
      26  .  .  .  .  .  .  .  .  .  .  .  .  .  . 26
      27  .  .  .  .  .  .  .  .  .  .  .  .  X  . 27
      28  .  .  .  .  .  .  .  .  .  .  .  .  .  . 28
      29  0  1  2  3  4  5  6  7  8  9 10 11 12 13 29
     
     === computeLine: 0 done (00:00:00) ==================================
     


    So how about you checking my code?

    Regards

    ehvbs

     
    #2
      ebgreen

      • Total Posts : 8227
      • Scores: 98
      • Reward points : 0
      • Joined: 7/12/2005
      • Status: offline
      RE: Test my VB Script please Monday, June 01, 2009 2:25 AM (permalink)
      0
      All of ehvbs' comments are completely correct, but my mom said I had to do it in 30 lines or less so my output isn't as pretty as his:

      Option Explicit
       Dim y
       Dim x
       Dim m
       Dim b
       Dim nIteration
       Dim nIterationLimit
       Dim strMessage
       
       y = 0
       x = 0
       m = 0
       b = 0
       nIteration = 1
       nIterationLimit = 5
       
       m = Inputbox("Please enter the slope (m)", "SLOPE")
       b = Inputbox("Please enter the Y-Intercept (b)", "Y-INTERCEPT")
       
       While nIteration <= nIterationLimit
           y = (m*x) + b
           strMessage = strMessage & "For m = " & m & ", x = " & x & ", b = " & b & " then y = " & y & vbCrLf
           x = x + 1
           nIteration = nIteration + 1
       Wend
       MsgBox strMessage
       

      "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
      Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
      http://www.visualbasicscript.com/m_47117/tm.htm
       
      #3
        Amida

        • Total Posts : 30
        • Scores: 0
        • Reward points : 0
        • Joined: 5/31/2009
        • Location: Codoria
        • Status: offline
        RE: Test my VB Script please Monday, June 01, 2009 5:10 PM (permalink)
        0
        Well ehvbs it runs nicely, but after listing a few of the x and y coordinates there is an error on line 93 at character 10
        also what would be a better form than input boxes and message boxes particularity for getting the user to input data like my code has.
         
        #4
          ehvbs

          • Total Posts : 3321
          • Scores: 110
          • Reward points : 0
          • Joined: 6/22/2005
          • Location: Germany
          • Status: offline
          RE: Test my VB Script please Tuesday, June 02, 2009 4:36 AM (permalink)
          0
          Hi Amida,

          I assume, you started my script by double-clicking on the file entry in Explorer. It's
          not meant to be used in such a way (with wscript.exe). Open a DOS box, cd to the
          directory you saved the script, do

             cscript computeLine.vbs <Enter>

          or

           C:\wis\_vbs\0506\dev\forum
           cscript computeLine.vbs
           +-------------+
           | nXFrom:   0 |
           |   nXTo:   5 |
           | nSlope:   1 |
           |nYCross:   0 |
           +------+------+
           |   X  |    Y |
           +------+------+
           |    0 |    0 |
           |    1 |    1 |
           |    2 |    2 |
           |    3 |    3 |
           |    4 |    4 |
           |    5 |    5 |
           +-------------+
           
                0  1  2  3  4  5  6  7
             0  X  .  .  .  .  .  .  0
             1  .  X  .  .  .  .  .  1
             2  .  .  X  .  .  .  .  2
             3  .  .  .  X  .  .  .  3
             4  .  .  .  .  X  .  .  4
             5  .  .  .  .  .  X  .  5
             6  .  .  .  .  .  .  .  6
             7  0  1  2  3  4  5  6  7
           
           
           C:\wis\_vbs\0506\dev\forum
           cscript computeLine.vbs /nxFrom:6 /nxTo:12 /nSlope:3 /nYCross:2
           +-------------+
           | nXFrom:   6 |
           |   nXTo:  12 |
           | nSlope:   3 |
           |nYCross:   2 |
           +------+------+
           |   X  |    Y |
           +------+------+
           |    6 |   20 |
           |    7 |   23 |
           |    8 |   26 |
           |    9 |   29 |
           |   10 |   32 |
           |   11 |   35 |
           |   12 |   38 |
           +-------------+
           
                0  1  2  3  4  5  6  7  8  9 10 11 12 13 14
             0  .  .  .  .  .  .  .  .  .  .  .  .  .  .  0
             1  .  .  .  .  .  .  .  .  .  .  .  .  .  .  1
             2  .  .  .  .  .  .  .  .  .  .  .  .  .  .  2
             3  .  .  .  .  .  .  .  .  .  .  .  .  .  .  3
             4  .  .  .  .  .  .  .  .  .  .  .  .  .  .  4
             5  .  .  .  .  .  .  .  .  .  .  .  .  .  .  5
             6  .  .  .  .  .  .  .  .  .  .  .  .  .  .  6
             7  .  .  .  .  .  .  .  .  .  .  .  .  .  .  7
             8  .  .  .  .  .  .  .  .  .  .  .  .  .  .  8
             9  .  .  .  .  .  .  .  .  .  .  .  .  .  .  9
            10  .  .  .  .  .  .  .  .  .  .  .  .  .  . 10
            11  .  .  .  .  .  .  .  .  .  .  .  .  .  . 11
            12  .  .  .  .  .  .  .  .  .  .  .  .  .  . 12
            13  .  .  .  .  .  .  .  .  .  .  .  .  .  . 13
            14  .  .  .  .  .  .  .  .  .  .  .  .  .  . 14
            15  .  .  .  .  .  .  .  .  .  .  .  .  .  . 15
            16  .  .  .  .  .  .  .  .  .  .  .  .  .  . 16
            17  .  .  .  .  .  .  .  .  .  .  .  .  .  . 17
            18  .  .  .  .  .  .  .  .  .  .  .  .  .  . 18
            19  .  .  .  .  .  .  .  .  .  .  .  .  .  . 19
            20  .  .  .  .  .  .  X  .  .  .  .  .  .  . 20
            21  .  .  .  .  .  .  .  .  .  .  .  .  .  . 21
            22  .  .  .  .  .  .  .  .  .  .  .  .  .  . 22
            23  .  .  .  .  .  .  .  X  .  .  .  .  .  . 23
            24  .  .  .  .  .  .  .  .  .  .  .  .  .  . 24
            25  .  .  .  .  .  .  .  .  .  .  .  .  .  . 25
            26  .  .  .  .  .  .  .  .  X  .  .  .  .  . 26
            27  .  .  .  .  .  .  .  .  .  .  .  .  .  . 27
            28  .  .  .  .  .  .  .  .  .  .  .  .  .  . 28
            29  .  .  .  .  .  .  .  .  .  X  .  .  .  . 29
            30  .  .  .  .  .  .  .  .  .  .  .  .  .  . 30
            31  .  .  .  .  .  .  .  .  .  .  .  .  .  . 31
            32  .  .  .  .  .  .  .  .  .  .  X  .  .  . 32
            33  .  .  .  .  .  .  .  .  .  .  .  .  .  . 33
            34  .  .  .  .  .  .  .  .  .  .  .  .  .  . 34
            35  .  .  .  .  .  .  .  .  .  .  .  X  .  . 35
            36  .  .  .  .  .  .  .  .  .  .  .  .  .  . 36
            37  .  .  .  .  .  .  .  .  .  .  .  .  .  . 37
            38  .  .  .  .  .  .  .  .  .  .  .  .  X  . 38
            39  .  .  .  .  .  .  .  .  .  .  .  .  .  . 39
            40  0  1  2  3  4  5  6  7  8  9 10 11 12 13 40
           


          This samples answers your question concerning better ways to gather input from a
          user: let him/her specify his/her wishes by passing comand line parameters to the script.

          Regards

          ehvbs


           
          #5
            mbouchard

            • Total Posts : 2110
            • Scores: 29
            • Reward points : 0
            • Joined: 5/15/2003
            • Location: USA
            • Status: offline
            RE: Test my VB Script please Tuesday, June 02, 2009 11:49 PM (permalink)
            0
            Boy, ehvbs and ebgreen's mothers sure are good at scripting.  I bet you guys had it rough growing up and were only allowed to script in notepad...
            Mike

            For useful Scripting links see the Read Me First stickey!

            Always remember Search is your friend.
             
            #6
              ebgreen

              • Total Posts : 8227
              • Scores: 98
              • Reward points : 0
              • Joined: 7/12/2005
              • Status: offline
              RE: Test my VB Script please Wednesday, June 03, 2009 2:17 AM (permalink)
              0
              Nope, I was weaned on vi. That's why I do all my vbscript in GVim now.
              "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
              Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
              http://www.visualbasicscript.com/m_47117/tm.htm
               
              #7
                Amida

                • Total Posts : 30
                • Scores: 0
                • Reward points : 0
                • Joined: 5/31/2009
                • Location: Codoria
                • Status: offline
                RE: Test my VB Script please Thursday, June 04, 2009 2:02 AM (permalink)
                0

                ORIGINAL: ehvbs

                Hi Amida,

                I assume, you started my script by double-clicking on the file entry in Explorer. It's
                not meant to be used in such a way (with wscript.exe). Open a DOS box, cd to the
                directory you saved the script, do

                  cscript computeLine.vbs <Enter>
                ...

                I have no real clue what DOS box is
                 
                #8
                  mbouchard

                  • Total Posts : 2110
                  • Scores: 29
                  • Reward points : 0
                  • Joined: 5/15/2003
                  • Location: USA
                  • Status: offline
                  RE: Test my VB Script please Thursday, June 04, 2009 2:13 AM (permalink)
                  0
                  A command prompt = DOS box.  Goto Start / Run and type in CMD, then follow what ehvbs said.
                  Mike

                  For useful Scripting links see the Read Me First stickey!

                  Always remember Search is your friend.
                   
                  #9
                    Amida

                    • Total Posts : 30
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 5/31/2009
                    • Location: Codoria
                    • Status: offline
                    RE: Test my VB Script please Thursday, June 04, 2009 2:42 AM (permalink)
                    0
                    Thank you
                     
                    #10
                      AHogan

                      • Total Posts : 5
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 9/15/2006
                      • Status: offline
                      RE: Test my VB Script please Friday, June 05, 2009 5:43 AM (permalink)
                      0

                      ORIGINAL: ebgreen

                      Nope, I was weaned on vi. That's why I do all my vbscript in GVim now.


                      Not sure if you are serious here or not.. :-)
                      But I've just started using Gvim in windows and find it to be an excellent editor (does good syntax coloring for VB as well).

                      If you are serious I might have some questions on how to   run a vbscript from within GVIM & jump to the error line ...

                      Best wishes,

                      Anthony_S_Hogan
                       
                      #11
                        ebgreen

                        • Total Posts : 8227
                        • Scores: 98
                        • Reward points : 0
                        • Joined: 7/12/2005
                        • Status: offline
                        RE: Test my VB Script please Friday, June 05, 2009 6:09 AM (permalink)
                        0
                        I am serious, but I do not run the script from inside GVim. I do that from a powershell command line.  Just a matter of habit. The Alt-Tab to get to powershell to run the script doesn't bother me. The alt-tab and ##gg to go to the error line doesn't either. I've just been doing it that way for so long not sure that I could change easily enough to be worth the effort.
                        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                        http://www.visualbasicscript.com/m_47117/tm.htm
                         
                        #12
                          AHogan

                          • Total Posts : 5
                          • Scores: 0
                          • Reward points : 0
                          • Joined: 9/15/2006
                          • Status: offline
                          RE: Test my VB Script please Friday, June 05, 2009 6:38 AM (permalink)
                          0

                          ORIGINAL: ebgreen
                          I am serious, but I do not run the script from inside GVim. I do that from a powershell command line.  Just a matter of habit. The Alt-Tab to get to powershell to run the script doesn't bother me. The alt-tab and ##gg to go to the error line doesn't either. I've just been doing it that way for so long not sure that I could change easily enough to be worth the effort.


                          Sounds good, a quick internet browse later gives the following...
                          :imap <F5> <Esc>:w<CR>:!%<CR>
                          Which saves current file & then runs it when pressing <f5> in insert mode. I'm sure there must be a way to collect the error line and jump directly...
                          I'm new to gvim and have a ton of stuff to learn... Also this way brings up a Console (I'm not sure if it's possible to run wscript directly??) GVim is so flexible, I'm sure there is going to be a way to configure this :-)



                          Anthony_S_Hogan
                           
                          #13

                            Online Bookmarks Sharing: Share/Bookmark

                            Jump to:

                            Current active users

                            There are 0 members and 1 guests.

                            Icon Legend and Permission

                            • New Messages
                            • No New Messages
                            • Hot Topic w/ New Messages
                            • Hot Topic w/o New Messages
                            • Locked w/ New Messages
                            • Locked w/o New Messages
                            • Read Message
                            • Post New Thread
                            • Reply to message
                            • Post New Poll
                            • Submit Vote
                            • Post reward post
                            • Delete my own posts
                            • Delete my own threads
                            • Rate post

                            2000-2012 ASPPlayground.NET Forum Version 3.9