471,610 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,610 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 1997
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by | last post: by
4 posts views Thread by Nicolas Vigier | last post: by
9 posts views Thread by Bill Borg | last post: by
9 posts views Thread by Csaba Gabor | last post: by
41 posts views Thread by Harry | last post: by
10 posts views Thread by AsheeG87 | last post: by
4 posts views Thread by ThEoNeAnDOnLy | last post: by
3 posts views Thread by from.future.import | last post: by
3 posts views Thread by Davy | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.