Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive function to array

Status
Not open for further replies.

QuantumDoja

Programmer
Jun 11, 2004
76
GB
Hi, I have a recursive function that takes a categoryid and recurses until its parent categories are all found.

e.g

Hard Drives | Seagate | ATA

I take ATA and recurse until i get seagate and Hard drives, but how do i output all the values into an array, i can print them on screen.....can anyone point me in the right direction?

Thanks

 
Some example code of how you do this recursive function may help.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Code:
Function getCategory(ByVal uid As Integer) As Integer
        Dim oConn As SqlConnection
        Dim oComm As SqlCommand
        Dim ParentID As Integer
        Dim ParentLevel As Integer

        'get parent id
        oConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        oConn.Open()
        oComm = New SqlCommand("SELECT ParentID FROM Categories WHERE UID = " & uid & "", oConn)
        ParentID = oComm.ExecuteScalar
        oConn.Close()

        'get parent level
        oConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        oConn.Open()
        oComm = New SqlCommand("SELECT ParentLevel FROM Categories WHERE UID = " & uid & "", oConn)
        ParentLevel = oComm.ExecuteScalar
        oConn.Close()

        If ParentLevel = 0 Then
            Response.Write("Lowest Point")
            Return uid
        Else
            Response.Write("Next Point" & ParentID)
            Return getCategory(ParentID)
        End If

    End Function

 
OK - i think the main part of your code that you need to look at is:
Code:
        If ParentLevel = 0 Then
            Response.Write("Lowest Point")
            Return uid
        Else
            Response.Write("Next Point" & ParentID)
            Return getCategory(ParentID)
        End If
As this is where you decide whether it is the parent level or not, you can simply store the details of each level as a variable. e.g.
Code:
    If ParentLevel = 0 Then
        MyString &= ParentID & " > "
        Response.Write("Lowest Point")
        Return uid
    Else
        MyString &= ParentID & " > "
        Response.Write("Next Point" & ParentID)
        Return getCategory(ParentID)
    End If
Either storing it in a string or session variable should do the trick (note the code is just an example of how to do it to hopefully illustrate my point).

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top