473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Is there a built in way to determine the recursion level?

In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM
Nov 16 '05 #1
11 12152

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.


Not really, but you can get access to the call stack:

StackTrace trace = new StackTrace(true);
int stackFrames = trace.FrameCount;

This isn't the same as @@NESTLEVEL, but it does increment for each method
call.

--
Mickey Williams
Author, "Visual C# .NET Core Ref", MS Press
www.neudesic.com
www.servergeek.com
Nov 16 '05 #2
Hi,

NO AFAIK, you could try to use StackTrace and go over the StackFrames
counting.

Or you could implement it using a counter and passing it as a parameter.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM

Nov 16 '05 #3
I usually just code it by adding a class variable counter. E.g.

private int m_iMRGLevel;
private void FnCallingMRF()
{
m_iMRGLevel = 0;
MyRecursiveFunction();
}
private void MyRecursiveFunction()
{
m_iMRFLevel++;
// More code
if( someCondition )
MyRecursiveFunction();
}

Of course, you can also use a ref parameter like this:
private void FnCallingMFR()
{
int iLevel = 0;
MyRecursiveFunction( ref iLevel );
}
private void MyRecursiveFunction( ref int level )
{
level++;
if( someCondition )
MyRecursiveFunction( ref level );
}

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM

Nov 16 '05 #4
This wont work as it will always increment. It wont decrement as levels are
popped back, will it?

JIM
"Tom Clement" <To**********@Apptero.com> wrote in message
news:uF**************@TK2MSFTNGP14.phx.gbl...
I usually just code it by adding a class variable counter. E.g.

private int m_iMRGLevel;
private void FnCallingMRF()
{
m_iMRGLevel = 0;
MyRecursiveFunction();
}
private void MyRecursiveFunction()
{
m_iMRFLevel++;
// More code
if( someCondition )
MyRecursiveFunction();
}

Of course, you can also use a ref parameter like this:
private void FnCallingMFR()
{
int iLevel = 0;
MyRecursiveFunction( ref iLevel );
}
private void MyRecursiveFunction( ref int level )
{
level++;
if( someCondition )
MyRecursiveFunction( ref level );
}

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to
get the current level of a recursive method call? I would like to be
able to tell how many levels down I am at any given point.

thanks

JIM


Nov 16 '05 #5
This doesn't tell you the exact level, it includes all levels prior to the
recursive call all the way from Application.Run

Thanks,

JIM

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:eU**************@TK2MSFTNGP14.phx.gbl...
Hi,

NO AFAIK, you could try to use StackTrace and go over the StackFrames
counting.

Or you could implement it using a counter and passing it as a parameter.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to
get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM


Nov 16 '05 #6
....but you have a solution from Tom if you add the decrementing

"james" <no****@hypercon.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
This wont work as it will always increment. It wont decrement as levels
are popped back, will it?

JIM
"Tom Clement" <To**********@Apptero.com> wrote in message
news:uF**************@TK2MSFTNGP14.phx.gbl...
I usually just code it by adding a class variable counter. E.g.

private int m_iMRGLevel;
private void FnCallingMRF()
{
m_iMRGLevel = 0;
MyRecursiveFunction();
}
private void MyRecursiveFunction()
{
m_iMRFLevel++;
// More code
if( someCondition )
MyRecursiveFunction();
}

Of course, you can also use a ref parameter like this:
private void FnCallingMFR()
{
int iLevel = 0;
MyRecursiveFunction( ref iLevel );
}
private void MyRecursiveFunction( ref int level )
{
level++;
if( someCondition )
MyRecursiveFunction( ref level );
}

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to
get the current level of a recursive method call? I would like to be
able to tell how many levels down I am at any given point.

thanks

JIM



Nov 16 '05 #7
No you don't. In a tree traversal of an n-ary tree this will not work. You
need a static variable internal to the method so that its value is retained
from frame to frame. With the incrementing decrementing global variable, it
will continue to increment for each leaf node it goes into even though those
leaf nodes are on the same level.

JIM
"Joep" <St***@DeStoep.nl> wrote in message
news:41***********************@news.xs4all.nl...
...but you have a solution from Tom if you add the decrementing

"james" <no****@hypercon.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
This wont work as it will always increment. It wont decrement as levels
are popped back, will it?

JIM
"Tom Clement" <To**********@Apptero.com> wrote in message
news:uF**************@TK2MSFTNGP14.phx.gbl...
I usually just code it by adding a class variable counter. E.g.

private int m_iMRGLevel;
private void FnCallingMRF()
{
m_iMRGLevel = 0;
MyRecursiveFunction();
}
private void MyRecursiveFunction()
{
m_iMRFLevel++;
// More code
if( someCondition )
MyRecursiveFunction();
}

Of course, you can also use a ref parameter like this:
private void FnCallingMFR()
{
int iLevel = 0;
MyRecursiveFunction( ref iLevel );
}
private void MyRecursiveFunction( ref int level )
{
level++;
if( someCondition )
MyRecursiveFunction( ref level );
}

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to
get the current level of a recursive method call? I would like to be
able to tell how many levels down I am at any given point.

thanks

JIM



Nov 16 '05 #8
Hi,
This doesn't tell you the exact level, it includes all levels prior to the
recursive call all the way from Application.Run


No really, you know that the recursion's stackframes are a subset , in fact
they are the one at the top of the stack, all you have to do is know where
it ends and find the parent method ( the one that called the recursive
method) , not a very efficient way though.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 16 '05 #9
That's what I was thinking. Something like
void MyRecursiveMethod(...) {
MyRecursiveMethod(..., 1);
}
void MyRecursiveMethod(..., int recursionDepth) {
...
MyRecursiveMethod(...,recursionDepth+1);
}
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

NO AFAIK, you could try to use StackTrace and go over the StackFrames
counting.

Or you could implement it using a counter and passing it as a parameter.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM


Nov 16 '05 #10
The best way to do this that I have found is to pass the level into the
method when you recurse.

Something like this:

void DoSomething(ICollection objects, int level)
{
// do something with the collection
...
DoSomething(children of one of the objects, level + 1)
...
// do something after the recursion (level will be unchanged)
}

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM

Nov 16 '05 #11
This will deffinately work, although I was hoping for a way to avoid passing
parameters.

thanks,

JIM

"Rangi Keen" <ra***@newsgroups.nospam> wrote in message
news:el**************@TK2MSFTNGP12.phx.gbl...
The best way to do this that I have found is to pass the level into the
method when you recurse.

Something like this:

void DoSomething(ICollection objects, int level)
{
// do something with the collection
...
DoSomething(children of one of the objects, level + 1)
...
// do something after the recursion (level will be unchanged)
}

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to
get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM


Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Patrick Lioi | last post by:
def foo(): pass foo is a function foo is a callable object foo has method __call__ defined foo.__call__ is a function foo.__call__ is a callable object foo.__call__ has method __call__...
9
by: JustSomeGuy | last post by:
I have a class that looks something like this (Don't try to compile it, I haven't tested this) class KVP { string key; string value; list<KVP> sublist; };
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
8
by: No bother | last post by:
I have a table with two columns, one named master, the other slave. Each column has a set of numbers. A number in one column can appear in the other. I am trying to see if there is any infinite...
3
by: David Bear | last post by:
Is there an easy way to get the current level of recursion? I don't mean sys.getrecursionlimit. I want to know during a live run of a script how many times the functions has recursed -- curses, I...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
0
by: milbot | last post by:
Hi, I am trying to set-up a database and have decided to implement the built-in user level security to satisfy my security requirements. I was wondering if it were possible to restrict users to...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.