Answered by:
Multiple variable from one Function?

Question
-
User1347560833 posted
Is it possible to get more than one variable from a function? In my attempt below _currmiles is simply being displayed in both labels instead of _currmiles to Currmiles.Text and _currdays to Currdays.Text. I created a separate function for _currdays and that works but it seemed like too much code to get 2 variables [:D]
Thanks in advance for any help!
Eric
---
Public Function Getmiles(ByVal journal As String) As String
Dim conn As New Data.SqlClient.SqlConnection
Dim sql As String
Dim objSql As Data.SqlClient.SqlCommand
Dim MyDataReader As Data.SqlClient.SqlDataReader
conn.ConnectionString = ConfigurationManager.ConnectionStrings("JournalCS").ConnectionString
Dim _currmiles As String = Nothing
Dim _currdays As String = Nothing
sql = "SELECT cast(SUM(mile) as varchar(4)) AS milesum, cast(COUNT(mile) as varchar(3)) AS milecnt FROM Journal WHERE (mile <> 0)"
objSql = New Data.SqlClient.SqlCommand(sql, Conn)
Conn.Open()
MyDataReader = objSql.ExecuteReader
While (MyDataReader.Read())
_currmiles = MyDataReader.Item("milesum")
_currdays = MyDataReader.Item("milecnt")
End While
Conn.Close()
Return _currmiles.ToString
Return _currdays.ToString
End FunctionPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CurrMiles.Text = Getmiles("_Currmiles".ToString)
Currdays.Text = (GetDays("_Currdays".ToString))
End Sub---
<asp:label ID="CurrMiles" runat=server Text="" ></asp:label>
<asp:label ID="Currdays" runat=server Text=""></asp:label>---
P.S. If this is an easy answer I have one more lol. The column mile is numeric but I was not able to figure out how to convert it to String in VB so I fell back on what I know by usign CAST in the SQL statement. How would you do this in VB?
Wednesday, November 22, 2006 6:28 PM
Answers
-
User-389939489 posted
Hello apathy,Return _currmiles.ToString
Return _currdays.ToStringYour functions returns one only string as soon as it gets to the first Return statement, and the second Return gets never reached - actually, the compiler should be issuing a warning about unreachable code.
To return multiple values, either return a Structure, or simply an Array or some List...
Hope this helps. -LV
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, November 24, 2006 3:11 AM -
User-524013004 posted
An example of how to return both values as a Class Object. If the DB values are integers you would ammend the Class fields accordingly.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
' note the changed return type - custom class, definition below<o:p></o:p>
Public Function Getmiles(ByVal journal As String) As MilesResponse<o:p></o:p>
Dim conn As New Data.SqlClient.SqlConnection<o:p></o:p>
Dim sql As String<o:p></o:p>
Dim objSql As Data.SqlClient.SqlCommand<o:p></o:p>
Dim MyDataReader As Data.SqlClient.SqlDataReader<o:p></o:p>
conn.ConnectionString = ConfigurationManager.ConnectionStrings("JournalCS").ConnectionString<o:p></o:p>
Dim _currmiles As String = Nothing<o:p></o:p>
Dim _currdays As String = Nothing<o:p></o:p>
sql = "SELECT cast(SUM(mile) as varchar(4)) AS milesum, cast(COUNT(mile) as varchar(3)) AS milecnt FROM Journal WHERE (mile <> 0)"<o:p></o:p>
objSql = New Data.SqlClient.SqlCommand(sql, conn)<o:p></o:p>
conn.Open()<o:p></o:p>
MyDataReader = objSql.ExecuteReader<o:p></o:p>
<o:p> </o:p>
' Declare return object<o:p></o:p>
Dim rsp As New MilesResponse<o:p></o:p>
<o:p> </o:p>
While (MyDataReader.Read())<o:p></o:p>
'_currmiles = MyDataReader.Item("milesum")<o:p></o:p>
'_currdays = MyDataReader.Item("milecnt")<o:p></o:p>
' Set object values<o:p></o:p>
rsp.CurrentMiles = MyDataReader.Item("milesum")<o:p></o:p>
rsp.CurrentDays = MyDataReader.Item("milecnt")<o:p></o:p>
<o:p> </o:p>
End While<o:p></o:p>
conn.Close()<o:p></o:p>
<o:p> </o:p>
Return rsp ' return Object of type MilesResponse<o:p></o:p>
'Return _currmiles.ToString<o:p></o:p>
'Return _currdays.ToString<o:p></o:p>
<o:p> </o:p>
End Function<o:p></o:p>
<o:p> </o:p>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<o:p></o:p>
<o:p> </o:p>
Dim ans As MilesResponse = Getmiles("_Currmiles")<o:p></o:p>
currMiles.Text = ans.CurrentMiles<o:p></o:p>
currDays.Text = ans.CurrentDays<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
Public Class MilesResponse<o:p></o:p>
Friend CurrentMiles As String<o:p></o:p>
Friend CurrentDays As String<o:p></o:p>
End Class<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Regards,<o:p></o:p>
Martin.<o:p></o:p>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, November 26, 2006 10:56 PM
All replies
-
User-389939489 posted
Hello apathy,Return _currmiles.ToString
Return _currdays.ToStringYour functions returns one only string as soon as it gets to the first Return statement, and the second Return gets never reached - actually, the compiler should be issuing a warning about unreachable code.
To return multiple values, either return a Structure, or simply an Array or some List...
Hope this helps. -LV
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, November 24, 2006 3:11 AM -
User1347560833 posted
Hi Ludovico,
I see what you are saying after reading about using return in a function. I haven't figured out how to return it to a list or array yet but will keep reading. In the interim I just moved the connection string et al into my Page_load until I work it out. Thanks for the help!
Sunday, November 26, 2006 2:44 PM -
User-524013004 posted
An example of how to return both values as a Class Object. If the DB values are integers you would ammend the Class fields accordingly.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
' note the changed return type - custom class, definition below<o:p></o:p>
Public Function Getmiles(ByVal journal As String) As MilesResponse<o:p></o:p>
Dim conn As New Data.SqlClient.SqlConnection<o:p></o:p>
Dim sql As String<o:p></o:p>
Dim objSql As Data.SqlClient.SqlCommand<o:p></o:p>
Dim MyDataReader As Data.SqlClient.SqlDataReader<o:p></o:p>
conn.ConnectionString = ConfigurationManager.ConnectionStrings("JournalCS").ConnectionString<o:p></o:p>
Dim _currmiles As String = Nothing<o:p></o:p>
Dim _currdays As String = Nothing<o:p></o:p>
sql = "SELECT cast(SUM(mile) as varchar(4)) AS milesum, cast(COUNT(mile) as varchar(3)) AS milecnt FROM Journal WHERE (mile <> 0)"<o:p></o:p>
objSql = New Data.SqlClient.SqlCommand(sql, conn)<o:p></o:p>
conn.Open()<o:p></o:p>
MyDataReader = objSql.ExecuteReader<o:p></o:p>
<o:p> </o:p>
' Declare return object<o:p></o:p>
Dim rsp As New MilesResponse<o:p></o:p>
<o:p> </o:p>
While (MyDataReader.Read())<o:p></o:p>
'_currmiles = MyDataReader.Item("milesum")<o:p></o:p>
'_currdays = MyDataReader.Item("milecnt")<o:p></o:p>
' Set object values<o:p></o:p>
rsp.CurrentMiles = MyDataReader.Item("milesum")<o:p></o:p>
rsp.CurrentDays = MyDataReader.Item("milecnt")<o:p></o:p>
<o:p> </o:p>
End While<o:p></o:p>
conn.Close()<o:p></o:p>
<o:p> </o:p>
Return rsp ' return Object of type MilesResponse<o:p></o:p>
'Return _currmiles.ToString<o:p></o:p>
'Return _currdays.ToString<o:p></o:p>
<o:p> </o:p>
End Function<o:p></o:p>
<o:p> </o:p>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<o:p></o:p>
<o:p> </o:p>
Dim ans As MilesResponse = Getmiles("_Currmiles")<o:p></o:p>
currMiles.Text = ans.CurrentMiles<o:p></o:p>
currDays.Text = ans.CurrentDays<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
Public Class MilesResponse<o:p></o:p>
Friend CurrentMiles As String<o:p></o:p>
Friend CurrentDays As String<o:p></o:p>
End Class<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Regards,<o:p></o:p>
Martin.<o:p></o:p>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, November 26, 2006 10:56 PM -
User1347560833 posted
Thanks Martin! That did the trick [:D]
Much appreciated,
Eric
Monday, November 27, 2006 2:47 PM -
User-524013004 posted
Glad to be help.Monday, November 27, 2006 2:53 PM -
User870110914 posted
Very useful. Thanks.
Thursday, June 11, 2009 2:12 PM -
User-1378500319 posted
Awesome answer, I usually use a datatable for this but I only need 1 row to be returned.
Thanks
Tuesday, January 26, 2010 1:44 PM -
User668634156 posted
it is not working
i have used it in
Public Shared Function UserRights
. . . . . . .
end function
public Class rights
friend new as boolean
end class
in pageload
is not working rights.new
showing error: " rights.new is accessable in this context because it is 'Friend'
Sunday, May 23, 2010 4:59 AM