4

I have seen other posts, but they are mostly in C#. For someone looking to learn recursion, seeing real world working examples in VB.Net could prove helpful. It's an added difficulty to try to decipher and convert C# if someone is just getting their feet wet programming in VB.Net. I did find this post which I understand now, but if there had been a post of VB.Net examples, I may have been able to pick it up faster.This is in part why I am asking this question:

Can anyone could show some simple examples of recursion functions in VB.Net?

1
  • You could try doing a recursive method by applying simple mathematical equations to the formal method Commented Apr 3, 2013 at 2:34

4 Answers 4

3

This one from the wiki article is great

Sub walkTree(ByVal directory As IO.DirectoryInfo, ByVal pattern As String)
 For Each file In directory.GetFiles(pattern)
    Console.WriteLine(file.FullName)
 Next
 For Each subDir In directory.GetDirectories
    walkTree(subDir, pattern)
 Next
End Sub
Sign up to request clarification or add additional context in comments.

Comments

3

Take a look at MSDN article - Recursive Procedures (Visual Basic). This article will help you to understand the basics of recursion.

Please refer the following links:

  1. Recursive function to read a directory structure
  2. Recursion, why it's cool.

4 Comments

I saw a comment in another post and was wondering is it true the only times you really need to write a recursive function to solve a problem, that isn't solved with iteration is looping through a directory or file system and looping through nested controls ?
@kcbeard: No, that is not true. There are numerous cases where recursion is useful (I can think of a dozen or more in the application I'm working on now). For example, if you want to process objects that are stored internally in a tree structure, then recursion is definitely your friend.
@competent_tech: At what point in a web app would a recursive function hinder its performance. Would looping through nested controls be normal us of a recursive function ?
@kcbeard: You shouldn't think of a recursive function much differently than you do a standard loop; if you have to use recursion to perform a task, then you should use it. However, like looping, if you use it excessively AND there are other ways to accomplish the same goal (i.e. caching data that you keep looking through), then you should modify your code. And yes, looping through nested controls is a very natural, normal use of recursion.
1

One of the classics

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Try
        Label1.Text = Factorial(20).ToString("n0")
    Catch ex As Exception
        Debug.WriteLine("error")
    End Try
End Sub

Function Factorial(ByVal number As Long) As Long
    If number <= 1 Then
        Return (1)
    Else
        Return number * Factorial(number - 1)
    End If
End Function 'Factorial

In .Net 4.0 you could use BigInteger instead of Long...

Comments

0

This is a recursion example direct from msdn for VB.NET 2012:

Function factorial(ByVal n As Integer) As Integer
    If n <= 1 Then 
        Return 1
    Else 
        Return factorial(n - 1) * n
    End If 
End Function

Reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.