BuddyJoe
BuddyJoe

Reputation: 71171

Attribute to Skip over a Method while Stepping in Debug Mode

Is there an attribute I can use on a method so that when stepping through some code in Debug mode the Debugger stays on the outside of the method?

Upvotes: 114

Views: 24664

Answers (4)

zxMarce
zxMarce

Reputation: 131

Answering in general (and to @Marchy in particular, even if 14 years after the fact).

A word of warning: Verbose code ahead. I don't like to have VB's Imports or C# using in my code because most of the example code I stumble upon almost invariably omit these declarations and just show the code, with no clue to the reader as from where the objects/classes/methods invoked come out.

In C#, you flag classes and functions as "debugger step thru" as follows:

[System.Diagnostics.DebuggerStepThrough]
class someClass {
    ...
}

[System.Diagnostics.DebuggerStepThrough]
void someMethod (args...){
    ...
}

In VB, on the other hand, the syntax is almost the same; just use angle-brackets whenever you see C#'s square-brackets:

<System.Diagnostics.DebuggerStepThrough>
Friend Class someClass
    ...
End Class

<System.Diagnostics.DebuggerStepThrough>
Private Sub someMethod (args...)
    ...
End Sub

But what about properties, @Marchy says? These kick you in the face with an error* if you add the attribute to the property declaration itself. The solution is to add the attribute to the Getter/Setter themselves instead, as it affects code in contrast to declarations. In VB:

Public Property propertyName

    <System.Diagnostics.DebuggerStepThrough>
    Get
        ...
    End Get

    <System.Diagnostics.DebuggerStepThrough>
    Set (args...)
        ...
    End Set

End Property

Hope it helps.

*The error is: Attribute 'DebuggerStepThroughAttribute' cannot be applied to '{propertyName}' because the attribute is not valid on this declaration type.

Upvotes: 3

BaSsGaz
BaSsGaz

Reputation: 794

It's written <DebuggerStepThrough> in VB.NET.

To use it just put on top of the method like :

<DebuggerStepThrough>
Private Sub form_Paint(sender As Object, e As PaintEventArgs) Handles form.Paint
     ' Picasso
End Sub

Upvotes: 8

Andrew Rollings
Andrew Rollings

Reputation: 14571

 [DebuggerStepThrough]

(docs)

Upvotes: 194

Ben
Ben

Reputation: 423

Not forgetting to add:

using System.Diagnostics;

Upvotes: 14

Related Questions