Home All Groups Group Topic Archive Search About

Function returns two values

Author
30 Mar 2006 7:17 PM
Brian Cahill
Hello,

I am trying to write a function that returns two values but I can't
figure out the syntax or if it's even possible.

        Private Function CTXScanner(ByVal mfSvr As String, ByVal xmlstr
As String, ByVal xslstr As String)
            Try
                    For Each HotfixItem In MFRecHotfix.Items
                        xmlstr = xmlstr & "<MFHOTFIX ID=""" &
HotfixItem & """>" & Environment.NewLine
                        xmlstr = xmlstr & "<SERVER>" & mfSvr &
"</SERVER>" & Environment.NewLine
                        xmlstr = xmlstr & "</MFHOTFIX>"
                    Next

                    Return xmlstr
                    Return xslstr
                End If
                srv2 = Nothing
                winSrv2 = Nothing

            Catch ex As Exception
                MsgBox(ex.Message.ToString())
                Exit Function
            End Try

        End Function

Author
30 Mar 2006 7:29 PM
Marina Levit [MVP]
It can't.

You either have to have a ByRef parameter that you set in your function, or
you need to create an object that has multiple properties. Then you function
creates an instance of that object, sets its properties, and returns it.

Show quoteHide quote
"Brian Cahill" <bcah***@wfs-ops.org> wrote in message
news:1143746266.677705.58390@i40g2000cwc.googlegroups.com...
> Hello,
>
> I am trying to write a function that returns two values but I can't
> figure out the syntax or if it's even possible.
>
>        Private Function CTXScanner(ByVal mfSvr As String, ByVal xmlstr
> As String, ByVal xslstr As String)
>            Try
>                    For Each HotfixItem In MFRecHotfix.Items
>                        xmlstr = xmlstr & "<MFHOTFIX ID=""" &
> HotfixItem & """>" & Environment.NewLine
>                        xmlstr = xmlstr & "<SERVER>" & mfSvr &
> "</SERVER>" & Environment.NewLine
>                        xmlstr = xmlstr & "</MFHOTFIX>"
>                    Next
>
>                    Return xmlstr
>                    Return xslstr
>                End If
>                srv2 = Nothing
>                winSrv2 = Nothing
>
>            Catch ex As Exception
>                MsgBox(ex.Message.ToString())
>                Exit Function
>            End Try
>
>        End Function
>
Author
30 Mar 2006 7:29 PM
Al Reid
It looks like you are mis-using or not understanding a Function vs Sub.  From what I see here you should change the Function to Sub
and change the ByVal to ByRef on the xmlstr and xslstr parameters.  Also remove the two return statements.

--
Al Reid


Show quoteHide quote
"Brian Cahill" <bcah***@wfs-ops.org> wrote in message news:1143746266.677705.58390@i40g2000cwc.googlegroups.com...
> Hello,
>
> I am trying to write a function that returns two values but I can't
> figure out the syntax or if it's even possible.
>
>         Private Function CTXScanner(ByVal mfSvr As String, ByVal xmlstr
> As String, ByVal xslstr As String)
>             Try
>                     For Each HotfixItem In MFRecHotfix.Items
>                         xmlstr = xmlstr & "<MFHOTFIX ID=""" &
> HotfixItem & """>" & Environment.NewLine
>                         xmlstr = xmlstr & "<SERVER>" & mfSvr &
> "</SERVER>" & Environment.NewLine
>                         xmlstr = xmlstr & "</MFHOTFIX>"
>                     Next
>
>                     Return xmlstr
>                     Return xslstr
>                 End If
>                 srv2 = Nothing
>                 winSrv2 = Nothing
>
>             Catch ex As Exception
>                 MsgBox(ex.Message.ToString())
>                 Exit Function
>             End Try
>
>         End Function
>
Author
31 Mar 2006 11:52 AM
Phill W.
"Brian Cahill" <bcah***@wfs-ops.org> wrote in message
news:1143746266.677705.58390@i40g2000cwc.googlegroups.com...

> I am trying to write a function that returns two values but I can't
> figure out the syntax or if it's even possible.

Probably the best way is to return Structure or Class that contains both
values, as in

Private Class CtxScanner_R
    Public Xml As String
    Public Xsl As String
    Friend Sub New( ByVal sXml As String, ByVal sXsl As String )
        Me.Xml = sXml
        Me.Xsl = sXsl
    End Sub
End Class

Private Function CTXScanner( _
  ByVal mfSvr As String _
, ByVal xmlstr As String _
, ByVal xslstr As String _
) As CtxScanner_R

    . . . whatever code you need . . .

    Return New CtxScanner_R( xmlstr, xslstr )
End Function

HTH,
    Phill  W.
Author
31 Mar 2006 3:09 PM
Brian Cahill
I was handling Functions and Subs the wrong way like Al said.  I should
have been passing byRef instead of byVal.  Thanks for the code though.
Much appreciated.