Coding For a Living

rss

A place to share and comment about coding for a living.


Visual Basic 2012– Caller Information Mike McIntyre

New in Visual Basic 2012 is the caller information feature. This feature enables you to obtain information about the caller of a method.

By using Caller Info attributes, you can obtain information about the caller to a method. You can obtain file path of the source code, the line number in the source code, and the member name of the caller. This information is helpful for tracing, debugging, and creating diagnostic tools.

To obtain this information, you use attributes that are applied to optional parameters, each of which has a default value. 

For more information about using the Caller Information feature click:

       Caller Information (C# and Visual Basic)

Sample Use

' Imports System.Runtime.CompilerServices ' Imports System.Diagnostics Private Sub DoProcessing() TraceMessage("Something happened.") End Sub Public Sub TraceMessage(message As String, <CallerMemberName> Optional memberName As String = Nothing, <CallerFilePath> Optional sourcefilePath As String = Nothing, <CallerLineNumber()> Optional sourceLineNumber As Integer = 0) Trace.WriteLine("message: " & message) Trace.WriteLine("member name: " & memberName) Trace.WriteLine("source file path: " & sourcefilePath) Trace.WriteLine("source line number: " & sourceLineNumber) End Sub ' Sample output: ' message: Something happened. ' member name: DoProcessing ' source file path: C:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoVB\CallerInfoVB\Form1.vb ' source line number: 15