473,394 Members | 1,971 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.

Recursive function, where am I?

Hi all,

using C#, .NET 1.1, ASP.NET

I have created a recursive??? function. It looks a little like...

MyContext.Current.Folder.Parent.Parent.Parent.Pare nt.Name

As you can see, Parent is the recursive??? part.

Say I start from
/root/Folder1/Folder2/Folder3/Folder4/Folder5, I need to somehow record
where I am in the list, so that when I get to .Name, I see "Folder1".

Is there an easy way to see how many steps I have taken with Parent to reach
the folder name?

I had thought of using a property, but if I then call another similar
routine later, the property will remain.

I am thinking out loud here... this maybe a solution. Please glance your
eyes over it and let me know if it will work and if there are likely to be
any problems. What I do not know is if I reach the ".Folder", whether the
..Folder routine actually gets called before I run the .Parent, if you see
what I mean.

1. Set up a CurrentFolder property.
2. in .Folder, reset the CurrentFolder property to the actual folder where
we are at, i.e. "/root/Folder1/Folder2/Folder3/Folder4/Folder5"
3. Each time .Parent is called, change the CurrentFolder property to the new
Folder Path.
4. When Name is called, read off the CurrentFolder.

Will this actually run?

I may have a scope problem here as well...
MyContext is a class that has a "Folder" property, which is based on a
Folders class. The Folders class has "Parent", which is the recursive Folders
class.
(Code sample may help)

public class MyContext
{
public Folders Folder
{ get {return null;} } // ignore the return value, this will be filled
later.
}
public class Folders
{
private Folders thisParent;

public Folders()
{
thisParent = this;
}

public Folders Parent
{
get { return thisParent; }
}

public string Name
{
get { return "FolderName"; } // This will return something properly.
}
}
So, with the above, how do I keep track of the .Parent ?

Thanks

Regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
Jan 16 '07 #1
7 2078
David Colliver wrote:

[...snip...]
I have created a recursive??? function. It looks a little like...

MyContext.Current.Folder.Parent.Parent.Parent.Pare nt.Name
This does not look recursive at all; but OTOH this does not look like a
function, too...
As you can see, Parent is the recursive??? part.

Say I start from
/root/Folder1/Folder2/Folder3/Folder4/Folder5, I need to somehow record
where I am in the list, so that when I get to .Name, I see "Folder1".
Pass the name of the current folder as a parameter to your function...
>
Is there an easy way to see how many steps I have taken with Parent to
reach
the folder name?
[...snip...]
Pass the number of steps you took as a parameter to your function...

public something getParentFolder(Folder currentFolder, int stepsTaken)
{
// Your code here...
}

Jan 16 '07 #2
You can pass a parameter that increments with each recursion and is passed
to the next.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.

"David Colliver" <Da***********@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
Hi all,

using C#, .NET 1.1, ASP.NET

I have created a recursive??? function. It looks a little like...

MyContext.Current.Folder.Parent.Parent.Parent.Pare nt.Name

As you can see, Parent is the recursive??? part.

Say I start from
/root/Folder1/Folder2/Folder3/Folder4/Folder5, I need to somehow record
where I am in the list, so that when I get to .Name, I see "Folder1".

Is there an easy way to see how many steps I have taken with Parent to
reach
the folder name?

I had thought of using a property, but if I then call another similar
routine later, the property will remain.

I am thinking out loud here... this maybe a solution. Please glance your
eyes over it and let me know if it will work and if there are likely to be
any problems. What I do not know is if I reach the ".Folder", whether the
.Folder routine actually gets called before I run the .Parent, if you see
what I mean.

1. Set up a CurrentFolder property.
2. in .Folder, reset the CurrentFolder property to the actual folder where
we are at, i.e. "/root/Folder1/Folder2/Folder3/Folder4/Folder5"
3. Each time .Parent is called, change the CurrentFolder property to the
new
Folder Path.
4. When Name is called, read off the CurrentFolder.

Will this actually run?

I may have a scope problem here as well...
MyContext is a class that has a "Folder" property, which is based on a
Folders class. The Folders class has "Parent", which is the recursive
Folders
class.
(Code sample may help)

public class MyContext
{
public Folders Folder
{ get {return null;} } // ignore the return value, this will be filled
later.
}
public class Folders
{
private Folders thisParent;

public Folders()
{
thisParent = this;
}

public Folders Parent
{
get { return thisParent; }
}

public string Name
{
get { return "FolderName"; } // This will return something
properly.
}
}
So, with the above, how do I keep track of the .Parent ?

Thanks

Regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available

Jan 16 '07 #3

"David Colliver" <Da***********@discussions.microsoft.comwrote in message
news:FB**********************************@microsof t.com...
| Glossary....
|
| Recursion, see recursion.
|

LOL !!!
Jan 16 '07 #4
First of all remeber general rule no. 1.
To understand recurence you must understand recurence!

Użytkownik "David Colliver" <Da***********@discussions.microsoft.com>
napisał w wiadomo¶ci
news:99**********************************@microsof t.com...
Hi all,

using C#, .NET 1.1, ASP.NET

I have created a recursive??? function. It looks a little like...

MyContext.Current.Folder.Parent.Parent.Parent.Pare nt.Name

As you can see, Parent is the recursive??? part.

Say I start from
/root/Folder1/Folder2/Folder3/Folder4/Folder5, I need to somehow record
where I am in the list, so that when I get to .Name, I see "Folder1".

Is there an easy way to see how many steps I have taken with Parent to
reach
the folder name?

I had thought of using a property, but if I then call another similar
routine later, the property will remain.

I am thinking out loud here... this maybe a solution. Please glance your
eyes over it and let me know if it will work and if there are likely to be
any problems. What I do not know is if I reach the ".Folder", whether the
.Folder routine actually gets called before I run the .Parent, if you see
what I mean.

1. Set up a CurrentFolder property.
2. in .Folder, reset the CurrentFolder property to the actual folder where
we are at, i.e. "/root/Folder1/Folder2/Folder3/Folder4/Folder5"
3. Each time .Parent is called, change the CurrentFolder property to the
new
Folder Path.
4. When Name is called, read off the CurrentFolder.

Will this actually run?

I may have a scope problem here as well...
MyContext is a class that has a "Folder" property, which is based on a
Folders class. The Folders class has "Parent", which is the recursive
Folders
class.
(Code sample may help)

public class MyContext
{
public Folders Folder
{ get {return null;} } // ignore the return value, this will be filled
later.
}
public class Folders
{
private Folders thisParent;

public Folders()
{
thisParent = this;
}

public Folders Parent
{
get { return thisParent; }
}

public string Name
{
get { return "FolderName"; } // This will return something
properly.
}
}
So, with the above, how do I keep track of the .Parent ?

Thanks

Regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available

Jan 16 '07 #5

"David Colliver" <Da***********@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
Hi all,

using C#, .NET 1.1, ASP.NET

I have created a recursive??? function. It looks a little like...

MyContext.Current.Folder.Parent.Parent.Parent.Pare nt.Name

As you can see, Parent is the recursive??? part.

Say I start from
/root/Folder1/Folder2/Folder3/Folder4/Folder5, I need to somehow record
where I am in the list, so that when I get to .Name, I see "Folder1".

Is there an easy way to see how many steps I have taken with Parent to
reach
the folder name?

I had thought of using a property, but if I then call another similar
routine later, the property will remain.

I am thinking out loud here... this maybe a solution. Please glance your
eyes over it and let me know if it will work and if there are likely to be
any problems. What I do not know is if I reach the ".Folder", whether the
.Folder routine actually gets called before I run the .Parent, if you see
what I mean.

1. Set up a CurrentFolder property.
2. in .Folder, reset the CurrentFolder property to the actual folder where
we are at, i.e. "/root/Folder1/Folder2/Folder3/Folder4/Folder5"
3. Each time .Parent is called, change the CurrentFolder property to the
new
Folder Path.
4. When Name is called, read off the CurrentFolder.

Will this actually run?

I may have a scope problem here as well...
MyContext is a class that has a "Folder" property, which is based on a
Folders class. The Folders class has "Parent", which is the recursive
Folders
class.
(Code sample may help)

public class MyContext
{
public Folders Folder
{ get {return null;} } // ignore the return value, this will be filled
later.
}
public class Folders
{
private Folders thisParent;

public Folders()
{
thisParent = this;
}

public Folders Parent
{
get { return thisParent; }
}

public string Name
{
get { return "FolderName"; } // This will return something
properly.
}
}
So, with the above, how do I keep track of the .Parent ?
something like

Folders Parent { get { int lastSlash = Name.LastIndexOf('/'); if (lastSlash
< 0) return null; return new Folders(Name.Substring(0, lastSlash)); } }
>
Thanks

Regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available

Jan 16 '07 #6
>So, with the above, how do I keep track of the .Parent ?

something like

Folders Parent { get { int lastSlash = Name.LastIndexOf('/'); if
(lastSlash < 0) return null; return new Folders(Name.Substring(0,
lastSlash)); } }
Please note, however, that this won't act properly on paths like:

/root/Folder1/Folder2/Folder3/Folder4/..

because the correct Parent path is /root/Folder1/Folder2, not
/root/Folder1/Folder2/Folder3/Folder4
Jan 16 '07 #7

"David Colliver" <Da***********@discussions.microsoft.comwrote in message
news:77**********************************@microsof t.com...
Thank you, I will try it.

Your note... Folder4/.. I am assuming you mean that /.. is normally up one
folder (to get to Folder2) where as the code will only get you to Folder4.

That shouldn't be a problem in this case. I am writing a developer tool
and
the function itself should know where it is direct from the system, not
from
developer input.

(To all others who are trying to explain recursion to me. I know what
recursion is. I can do recursion. I just didn't know what to call what I
am
doing, the closest word I could think of was recursion. I commented
earlier
in this thread with an actual recursive function to demonstrate this.)
The proper term would be composition I suspect, as the output from each call
becomes the input to the next. The resulting compiled code is virtually
identical to unrolled iteration so that could be used to describe this as
well.
>
Regards,
Dave Colliver.
http://www.MatlockFOCUS.com
~~
http://www.FOCUSPortals.com - Portal franchises available
"Ben Voigt" wrote:
>>
>So, with the above, how do I keep track of the .Parent ?

something like

Folders Parent { get { int lastSlash = Name.LastIndexOf('/'); if
(lastSlash < 0) return null; return new Folders(Name.Substring(0,
lastSlash)); } }

Please note, however, that this won't act properly on paths like:

/root/Folder1/Folder2/Folder3/Folder4/..

because the correct Parent path is /root/Folder1/Folder2, not
/root/Folder1/Folder2/Folder3/Folder4

Jan 17 '07 #8

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

Similar topics

2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
4
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2,...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
10
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
4
by: ThEoNeAnDOnLy | last post by:
I recently had an issue with my recursive project in class. Here is the code. // Recursion.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.