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?

Community
  • 1
  • 1
Keith Beard
  • 1,601
  • 4
  • 18
  • 36
  • You could try doing a recursive method by applying simple mathematical equations to the formal method –  Apr 03 '13 at 02:34

4 Answers4

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.
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 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 ? – Keith Beard Nov 18 '11 at 02:49
  • @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 Nov 18 '11 at 06:03
  • @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 ? – Keith Beard Nov 18 '11 at 06:30
  • @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. – competent_tech Nov 18 '11 at 17:07
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
CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
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...

dbasnett
  • 11,334
  • 2
  • 25
  • 33
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

mchar
  • 158
  • 5
  • 14