Answered Dim Result in functions??

  • Friday, July 27, 2012 6:36 AM
     
      Has Code

    how am I supposed to declare the "result variable in a function

    I havnt been able to find any examples of functions, only subs and in those there is usually a "result" variable initially set = ""

    I've modified this logic for my functions but dont understand why the result is set to = "" and not delared as the same data type as the funtion e.g

    Public Shared Function fncParamNumber(UID As Integer, UserID As String) As Decimal
            Dim result As Decimal
            Try
                Using ws As New LightSwitchApplication.DataWorkspace()
                    Dim param = (From x In ws.Tech_MembersData.tbl_UPV_UserParamValues
                                Where x.UPV_UserParamUID = UID AndAlso x.UPV_UserUID = UserID
                                Select x).FirstOrDefault()
                    If (param Is Nothing) Then
                        result = Nothing
                    Else
                        result = param.UPV_ValueNumeric
                    End If
                End Using
            Catch ex As Exception
                result = ex.Message
            End Try
            fncParamNumber = result
        End Function

    Problem with this function is its not trapping exceptiosn proerly as I have declared the Result as a decimal, but if htere is an exception its going to crap out.

    How are you supposed to handle the exceptions ?

    Thanks

    Grant

All Replies

  • Friday, July 27, 2012 9:00 AM
     
     

    Wrong forum for this kind of thing, but for Functions in VB you either:

    Return x
    (where x is the variable you want the function to return)
    or
    fncParamNumber = x
    (where you set the name of the function to be the result you want to return)

    You probably want to return "Nothing" if there's an exception, and whatever is calling the function should check for it.

    Where are you expecting the exception to occur?


    Free Visual Studio LightSwitch extensions: Elyl's Extensions http://visualstudiogallery.msdn.microsoft.com/bbe013bf-45b6-46c4-ba13-537cc23c5118


    • Edited by ElylV Friday, July 27, 2012 9:02 AM
    •  
  • Friday, July 27, 2012 12:02 PM
    Moderator
     
     Answered Has Code

    If you need to return two different values back from a function, there are two basic ways to do it:

    • Make the return value a complex class that consists of the two (or more) properties that you need to return, usinmg a result class:
    Public Class ComplexResult
        Public Property Value as Decimal?
        Public Property Error as String
    End Class
    
    Public Shared Function fncParamNumber(UID As Integer, UserID As String) As ComplexResult
        Dim result As ComplexResult
        result.Value = Nothing
        result.Error = ""
        
        Try
            Using ws As New LightSwitchApplication.DataWorkspace()
                Dim param = ( _
                    From x In ws.Tech_MembersData.tbl_UPV_UserParamValues
                    Where x.UPV_UserParamUID = UID AndAlso x.UPV_UserUID = UserID
                    Select x).FirstOrDefault()
                    
                If (param IsNot Nothing) Then
                    result.Value = param.UPV_ValueNumeric
                End If
            End Using
            
        Catch ex As Exception
            result.Error = ex.Message       
        End Try
        
        return result
    End Function

    You'll notice that I changed your line "fncParamNumber =result" to "return result". Setting the function's name equal to the return value is the "old" way (pre .NET) of returning values. .NET introduced the ability to "Return" a result.

    • The other way that you can return more than one value uses ByRef parameters to store the values:

    Public Shared Function fncParamNumber(UID As Integer, UserID As String, ByRef ErrorMessage as string) As Decimal
        Dim result As Decimal = 0D

        ErrorMessage = ""

        Try
            Using ws As New LightSwitchApplication.DataWorkspace()
                Dim param = ( _
                    From x In ws.Tech_MembersData.tbl_UPV_UserParamValues
                    Where x.UPV_UserParamUID = UID AndAlso x.UPV_UserUID = UserID
                    Select x).FirstOrDefault()
                   
                If (param IsNot Nothing) Then
                    result = param.UPV_ValueNumeric
                End If
            End Using
           
        Catch ex As Exception
            ErrorMessage = ex.Message      
        End Try
       
        return result
    End Function


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

  • Monday, July 30, 2012 3:09 AM
     
     

    Thanks Yann, you have answered my question and more...now I can have one function to retrieve, text, numeric or date values from my parameter table!

    Will have a play with this once I get LS workin again!

    Cheers

    Grant


    • Edited by NZTechworks Monday, July 30, 2012 3:10 AM correct spelling
    •  
  • Monday, July 30, 2012 10:54 AM
    Moderator
     
     
    You're welcome.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.