Skip to Content
Object-Oriented Programming with Visual Basic .NET
book

Object-Oriented Programming with Visual Basic .NET

by J.P. Hamilton
October 2002
Beginner
312 pages
7h 22m
English
O'Reilly Media, Inc.
Content preview from Object-Oriented Programming with Visual Basic .NET

6.3. The StackTrace Object

Whenever a method call is made, the Common Language Runtime creates information about that call. This information includes the location of the call within your program, the arguments that have been passed, and the local variables of the target method. The information is saved in a block of data called a stack frame. These stack frames are allocated in a region of memory called the call stack. The call stack contains all the methods that were called but have not returned to their callers. You saw this output to the console in Example 6-4.

This information can be very helpful when you track down bugs in your code. Even more helpful is the fact that you can inspect the call stack programmatically whenever you want. Example 6-5 demonstrates the process of iterating the call stack to obtain the method names that are currently active.

Example 6-5. Iterating the call stack
Imports System
Imports System.Diagnostics
Imports System.Reflection
   
Public Class StackTest
   
  Public Sub MethodA( )
    MethodB( )
  End Sub
   
  Public Sub MethodB( )
    MethodC( )
  End Sub
   
  Public Sub MethodC( )
   
    Dim i As Integer = 0
    Dim st As New StackTrace( )
    For i = 0 To st.FrameCount - 1
      Dim sf As StackFrame = st.GetFrame(i)
      Console.WriteLine(sf.GetMethod.Name)
    Next i
  End Sub
   
End Class
   
Public Class Application
   
  Public Shared Sub Main( )
    Dim test As New StackTest( )
    test.MethodA( )
    Console.ReadLine( )
  End Sub
   
End Class

The output from this program looks like this:

MethodC
MethodB
MethodA
Main

This ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming Visual Basic .NET

Programming Visual Basic .NET

Dave Grundgeiger

Publisher Resources

ISBN: 0596001460Catalog PageErrata