[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

How to Return more than one value from a VBScript Function

How do you return additional return values for a VBScript function besides the value that is returned by setting the function name to the return value?
0
dba123
Asked:
dba123
  • 4
  • 2
  • 2
  • +2
4 Solutions
 
suramsureshbabuCommented:
0
 
dba123Author Commented:
I don't think you understand.  I want to take this function and return not only StatusColor but an additional variable in my Case statment that I could grab after the function has been called:


      Dim totalused, status, totalspace

      strSQL3 = "sp_Get_DiskSpaceUsed @ResourceID=" & ResourceID
      Set Query3 = objConnection.Execute(strSQL3)

      status = "open"
      totalused = 0


      If Not Query3.EOF Then
            totalspace = Query3("totalspace")
      else
            totalspace = 0
      End If

      Set Query3 = Query3.NextRecordSet


      '### get total space used by all users for this resource and set flag
            If NOT Query3.EOF Then
                  Do While NOT Query3.EOF
                        totalused = totalused + Query3("spaceused")
                        Query3.MoveNext
                  Loop
            End If

            Set Query3 = Nothing

      '### Check space left; Leave 10 gigs free or else full
            If totalused > totalspace - 11 Then
                  status = "full"
            End If



      '### Check to see if I'm using this resource and set flag if so
            strSQL3 = "sp_Check_DoIUseThisResource @CheckedOutByID=" & EmployeeID & ", @ResourceID=" & ResourceID
            Set Query3 = objConnection.Execute(strSQL3)
            If Not Query3.EOF Then
                  status = "me"
            End If

            set Query3 = Nothing

            Select Case status
                  Case "open"
                        StatusColor = "<img src=""images/gr.gif"" border=""0"">"
                  Case "full"
                        StatusColor = "<img src=""images/rd.gif"" border=""0"">"
                  Case "me"
                        StatusColor = "<img src=""images/bl.gif"" border=""0"">"
            End Select

End Function
0
 
suramsureshbabuCommented:
try returning an array

this will help to know more
http://www.asp101.com/resources/jonmgohr/081899/class.asp
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dba123Author Commented:
so basically what you are saying is that I'd do something like this:

Function FunctionName(parameter1,....)

Dim temparray()

<code to fill in array with values>
....

FunctionName = temparray()

End Function

Then I'd access those values just like FunctionName is an array doing something like FunctinName(2) ?
0
 
VirusMinusCommented:
probably easier to declare two global variables and set them in the function
0
 
techjoshCommented:
another way is to pass one parameter byref and then use that var to return a value:

Function GetRGB(ByVal hex, ByRef red, ByRef green, ByRef blue)

  If Not IsNumeric(hex) Then
    red = 0
    Greeen = 0
    blue = 0
    Exit Function
  End If

  ' ... extract red green and blue values here and place them in the right vars
End Function

Dim red, green, blue

Call GetRGB( 1234567, red, green, blue)

Response.Write "Red value: " & red & "; Green value: " & green & "; Blue value: " blue & ";"
0
 
fritz_the_blankCommented:
One other option is to create your own object and then use the function to set your properties.

FtB
0
 
dba123Author Commented:
so you don't have to set the function's name to the output value?  You can setup variables and then access them from any function like this?
0
 
dba123Author Commented:
>> ' ... extract red green and blue values here and place them in the right vars

can you elaborate?
0
 
fritz_the_blankCommented:
Here is what I had in mind:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="VBScript">
Class objSpace
            Public SpaceAvailable
            Public SpaceUsed
            Public ObjectStatus
End Class

Dim objDBA123
sub showMe()
      Set objDBA123 = New objSpace
      objDBA123.SpaceAvailable = 100
      objDBA123.SpaceUsed = 50
      objDBA123.ObjectStatus = "The Status is fine!"
end sub

sub displayValues
      call showMe()
      msgbox objDBA123.SpaceUsed
      msgbox objDBA123.SpaceAvailable
      msgbox objDBA123.ObjectStatus
end sub

</script>
</head>

<body onLoad=displayValues>
</body>
</html>
0
 
techjoshCommented:
ok, in vbscript you can pass a variable to a function (or sub) one of two ways: byref and byval.  Byval copies the value of the variable to a new variable when calling the function, so that any modification of the variable inside the function does not modify the original value.  ByRef, on the other hand, passes a reference to the original variable to the function so that when you edit it inside the function you are editing the origninal variable and not just a copy of its value.  You could  still return a value from a function just as you always have before in addition to passing variables byref and using them as return variables.

As for the "function" that I made up... I did just that... I didn't actually spend the time to make the function.  Basically though, the intention of the function was to extract color values from a six character hex string commonly used in html to represent colors, like "#FFFFFF" for white, "#000000" for black, and everything in between.  To complete this function you would have to separate the characters into three groups: "FF-FF-FF" -- representing "Red-Green-Blue" respectively.  Then convert each of these values into numeric format and store them in the appropriate variables.  This function does all that:

Function GetRGB(ByVal strHex, ByRef red, ByRef green, ByRef blue)
      
      If Left(strHex, 1) = "#" Then
            strHex = Right(strHex, Len(strHex)-1)
      End If
      
      If Len(strHex) <> 6 Then
            red = 0
            green = 0
            blue = 0
            
            GetRGB = False
            Exit Function
      Else
            red = CInt("&H" & Left(strHex, 2))
            green = CInt("&H" & Mid(strHex, 3, 2))
            blue = CInt("&H" & Right(strHex, 2))
            
            GetRGB = True
            Exit Function
      End If

End Function

Dim test, red, green, blue
test = "#ffc0c0"
If GetRGB(test, red, green, blue) Then
      Response.Write(test & " is a valid color comprised of Red: " & Red & " Green: " & green & " Blue: " & blue)
Else
      Response.Write(test & " is not a valid color")
End If
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now