Results 1 to 8 of 8

Thread: [RESOLVED] Getting Function Name inside the function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    429

    Resolved [RESOLVED] Getting Function Name inside the function

    Hi,

    I am wondering if it is possible to get the function name dynamically inside the function call

    The idea is to standardize the MessageBox so it will thow function name in case of an error handled via error-exception.

    For eg

    Code:
    Public function myTestFunction(byval myVal as double) as double
    
    
       Try
    
          dim myCalc as double = myVal *1.2345
    
          dim myModuleName = GetModuleName()
    
       Catch ex As Exception
                MessageBox.show(ex, "Error in " & myModuleName)
    
       End Try
    
    end function
    Thanks..


    edited: Added Code Tags as suggested by JMC
    Last edited by surya; Apr 7th, 2019 at 11:38 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,016

    Re: Getting Function Name inside the function

    Not surprisingly, QUOTE tags are for quotes and CODE tags are for code. If you're posting code, please wrap it in CODE tags.

    As for the question, the thing to do is to display your message in another method and you can then get the method name automatically using the CallerMemberName attribute. In your case, ignoring the other issues, that might look like this:
    vb.net Code:
    1. Public Function myTestFunction(ByVal myVal As Double) As Double
    2.     Try
    3.         Dim myCalc As Double = myVal * 1.2345
    4.     Catch ex As Exception
    5.         DisplayErrorMessage(ex.ToString())
    6.     End Try
    7. End Function
    8.  
    9. Private Sub DisplayErrorMessage(text As String,
    10.                                 <CallerMemberName> Optional methodName As String = Nothing)
    11.     MessageBox.Show(text, "Error in " & methodName)
    12. End Sub
    As you can see, you don't even pass an argument to that parameter because the system does it for you.

    That said, you really ought not to be displaying a message box like that for exceptions. The user generally shouldn't need to consider a method name or specific details of an exception. You should be displaying a user-friendly message to the user and then logging the full details somewhere else for you, as the developer, to look at separately. You can use tools like NLog or log4net to handle the logging for you, or you can roll your own.

  3. #3
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Getting Function Name inside the function

    I believe the MessageBox.Show(ex.ToString()) in the Catch block should be DisplayErrorMessage(ex.ToString()).

    At least in VS2013 I also had to declare methodName as Optional.

    VB.Net Code:
    1. ...
    2.         Catch ex As Exception
    3.             DisplayErrorMessage(ex.ToString())
    4.         End Try
    5.     End Function
    6.  
    7.     Private Sub DisplayErrorMessage(text As String, <CallerMemberName> Optional methodName As String = "")
    8.         MessageBox.Show(text, "Error in " & methodName)
    9.     End Sub

    LOL, looks like you discovered the same things at nearly the same time.
    Last edited by topshot; Apr 5th, 2019 at 07:39 AM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,016

    Re: Getting Function Name inside the function

    Quote Originally Posted by topshot View Post
    I believe the MessageBox.Show(ex.ToString()) in the Catch block should be DisplayErrorMessage(ex.ToString()).

    At least in VS2013 I also had to declare methodName as Optional.

    VB.Net Code:
    1. ...
    2.         Catch ex As Exception
    3.             DisplayErrorMessage(ex.ToString())
    4.         End Try
    5.     End Function
    6.  
    7.     Private Sub DisplayErrorMessage(text As String, <CallerMemberName> Optional methodName As String = "")
    8.         MessageBox.Show(text, "Error in " & methodName)
    9.     End Sub
    Correct on both counts. I have fixed my code accordingly. Thanks for pointing my m..., my mi..., um, the part of my post that was not quite as correct as it could have been.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,016

    Re: Getting Function Name inside the function

    Quote Originally Posted by topshot View Post
    LOL, looks like you discovered the same things at nearly the same time.
    I could try to take credit for fixing my own mistake but I did so after reading your post.

  6. #6
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    134

    Re: Getting Function Name inside the function

    Yes I read the documentation and was going to respond but I was a little frightened too.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    429

    Re: Getting Function Name inside the function

    JMC, many thanks for your quick response.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    429

    Re: Getting Function Name inside the function

    Topshot, many thanks for your response..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width