I like so many others seek out code snippets from time to time to provide some functionality. Most are easy to incorporate. But I have not yet figured out how to use a function, like below. How does one call something like this in another program?

Private Function Search(path As String, Recursive As Boolean) As Boolean

'Whatever

End Function

Dani AI

Generated

Brief practical notes that build on 's helpful reply and the sample Search(...) signature posted by .

Access and where the routine lives matter more than its syntax. A method declared Private is only visible inside the declaring type; to call it from another form/class make the method (and its containing class or module) Public, place it in a Module, or expose a Public wrapper. See the Visual Basic access-modifiers guidance for details: .

Calling patterns depend on whether the routine is an instance method, a Shared method, or in a Module:

  • Instance methods require an instance of the class.
  • Shared methods can be invoked from the type name without creating an instance.
  • Module procedures are available by name across the project.

A common, clearer pattern for routines that may recurse is to offer a simple Public entry point that validates inputs and calls a private helper that carries the extra state (depth counter, visited set, etc.). This avoids passing a recursion flag in normal calls and keeps the API easier to use. Also be cautious with deep recursion for disk traversal; iterative approaches (stack/queue) or .NET enumeration helpers are safer for large trees.

Troubleshooting checklist (items that often block calls):

  • Confirm the method and its class/module are Public if called from another class or assembly.
  • Ensure the correct namespace is imported or use the fully qualified type name.
  • If the caller is in a different project, make sure the assembly is referenced.
  • Turn on Option Strict to reveal implicit-conversion issues and compile-time errors.
  • Prefer throwing/handling exceptions for unexpected failures rather than returning only a Boolean; use Try-style patterns when appropriate.

For parameter details (ByVal/ByRef, Optional, named arguments) see the procedure-parameters guidance: .

Recommended Answers

All 2 Replies

A Sub is a piece of code that takes zero or more parameters and does something. It does not return a value. A Function is the same thing except it returns a value that can be assigned to another variable. For example

Private Function AddTwo(firstNum As Integer, secondNum As Integer) As Integer
    Return firstNum + secondNum
End Function

Anywhere else in your program you can code

myResult = AddTwo(5,3)

Just like a varible, a Function has a type. The type of the function is specified on the same line as the declaration. The type refers to the type of value that is returned. In the above example, the type is specified like "As Integer". Functions can return any type of variable, be it simple (like Integer) or complex (like Dictionary, List or Array).

Thank you for your explanation...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.