Recursive Array Lesson Question

# Thread: Recursive Array Lesson Question

1. Join Date
Dec 1969
Posts
67

## Recursive Array Lesson Question

I&#039;m using the ASP recusrive array lesson found at http://www.4guysfromrolla.com/webtech/101202-1.shtml<BR>to make a tree view.<BR><BR>However, when reaching the end of the subfolders, my code is not going &#039;up&#039; a level properly, misrepresenting the indentations of the tree slightly.<BR><BR>Here is the function I call using <BR>Response.Write DisplayFolders(aFolders, 0, 0)<BR><BR><BR>Function DisplayFolders(aFolders, ByVal iCurID, ByVal iDepth)<BR> <BR> Const FolderID = 0, FolderName = 1, ParentID = 2 &#039;Absolute positions of columns in array<BR> <BR> Dim iNumRecords, iRowLoop, i<BR> iNumRecords = UBound(aFolders, 2) &#039;Array is (Columns,Rows)<BR> For iRowLoop = 0 to iNumRecords<BR> If CInt(aFolders(ParentID, iRowLoop)) = CInt(iCurID) then<BR> For i = 1 to iDepth<BR> strIndent = strIndent & "&nbsp;&nbsp;"<BR> Next<BR> %&#062;<BR> &#060;option value=&#060;%=(aFolders(FolderID,iRowLoop))%&#062; &#062;&#060;%Response.Write(strIndent & aFolders(FolderName,iRowLoop))%&#062;&#060;/option&#062;&#060;%<BR> DisplayFolders aFolders, aFolders(FolderID,iRowLoop), iDepth+1<BR> iDepth = iDepth - 1<BR> End If<BR> Next<BR><BR> DisplayFolders = strHTML<BR>End Function<BR><BR><BR>I realise I&#039;m putting "iDepth = iDepth - 1" in the wrong place...<BR>Can someone explain what the line <BR>"If CInt(aFolders(ParentID, iRowLoop)) = CInt(iCurID) then"<BR>means and help me get the "iDepth = iDepth - 1" in the right place?<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## A couple of mistakes...

First of all, you should *NEVER* be using<BR> iDepth = iDepth - 1<BR><BR>Each "level" in the recursion has its *OWN COPY* of iDepth, and that copy *should* remain the same for all processing at that level.<BR><BR>When you call the *next* level, you pass<BR> iDepth + 1<BR>correct, and then *that* level has the incremented value. But when that level returns, the iDepth in the caller will *REMAIN UNCHANGED*. That&#039;s the whole point of having LOCAL VARIABLES in the function/proc: They are *specific* to each new call of the function/proc.<BR><BR>And this isn&#039;t really a mistake, but you can change this code:<BR> For i = 1 to iDepth<BR> strIndent = strIndent & " "<BR> Next<BR>to simply this:<BR> strIndent = Space(iDepth*2)<BR><BR>The other big mistake you make is calling this a FUNCTION.<BR><BR>You aren&#039;t *USING* it as a function, at all. Look at your recursive call, for example:<BR> DisplayFolders aFolders, aFolders(FolderID,iRowLoop), iDepth+1<BR><BR>See? No use whatsoever of the return value from the call to DisplayFolders there!<BR><BR>Which is a good thing, because *THIS* line:<BR> DisplayFolders = strHTML<BR>is nonsense. You don&#039;t *HAVE* any variable named strHTML!!!<BR><BR>So...<BR><BR>******************* ****<BR>&#039; constants should be declared *OUTSIDE* any function/sub<BR>Const FolderID = 0, FolderName = 1, ParentID = 2 &#039;Absolute positions of columns in array<BR><BR>SUB DisplayFolders(aFolders, ByVal iCurID, ByVal iDepth)<BR><BR> Dim iNumRecords, iRowLoop, thisID, thisName<BR><BR> iNumRecords = UBound(aFolders, 2) &#039;Array is (Columns,Rows)<BR> For iRowLoop = 0 to iNumRecords<BR> &#039; we look through the array trying to find all items<BR> &#039; that have a ParentID that matches the id of the record<BR> &#039; in the level that invoked this call<BR> If CInt(aFolders(ParentID, iRowLoop)) = CInt(iCurID) then<BR> strIndent = space(iDepth*2)<BR> thisID = aFolders(FolderID,iRowLoop)<BR> thisName = aFolders(FolderName,iRowLoop)<BR>%&#062;<BR> &#060;option value=&#060;%=thisID%&#062;&#062;&#060;%=(strInden t & thisName)%&#062;&#060;/option&#062;<BR>&#060;%<BR> DisplayFolders aFolders, thisID, iDepth+1<BR> End If<BR> Next<BR>End Sub<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## The indent may not work right...

...if you just use spaces for indenting in an &#060;OPTION&#062;, esp. on Netscape browsers.<BR><BR>Also, it&#039;s silly to build the indent for each new line. Make strIndent *also* a local variable and it can be built just once per level.<BR><BR>Let&#039;s fix that and clean up the code a bit at the same time.<BR><BR>&#060;%<BR>SUB DisplayFolders(aFolders, ByVal iCurID, ByVal iDepth)<BR> Dim iRowLoop, thisID, thisName, strIndent<BR> strIndent = Replace( Space(iDepth*2), " ", "&amp;nbsp;" )<BR><BR> For iRowLoop = 0 to UBound(aFolders, 2)<BR> &#039; we look through the array trying to find all items<BR> &#039; that have a ParentID that matches the id of the record<BR> &#039; in the level that invoked this call<BR> If CInt(aFolders(ParentID, iRowLoop)) = CInt(iCurID) then<BR> thisID = aFolders(FolderID,iRowLoop)<BR> thisName = strIndent & aFolders(FolderName,iRowLoop)<BR>%&#062;<BR> &#060;option value="&#060;%=thisID%&#062;" &#062;&#060;%=thisName%&#062;&#060;/option&#062;<BR>&#060;%<BR> DisplayFolders aFolders, thisID, iDepth+1<BR> End If<BR> Next<BR>End Sub<BR>%&#062;<BR>

4. Join Date
Dec 1969
Posts
67

## Thanks that helped a lot (eop)

.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•