Recursive function, where am I?

G

Guest

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.Parent.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
 
M

Michael Voss

David Colliver wrote:

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

MyContext.Current.Folder.Parent.Parent.Parent.Parent.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...
}
 
B

Ben Voigt

David Colliver said:
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.Parent.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)); } }
 
B

Ben Voigt

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
 
B

Ben Voigt

David Colliver said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top