Methods
In VB, methods define the behavior or actions that objects can perform. Methods are described within classes and are responsible for performing specific tasks or calculations.
Method declaration
To define a method in VB, use the Sub
or Function
keyword, followed by the method’s name and any parameters it accepts. Here’s an example:
Public Sub MyMethod(parameter1 As Integer, parameter2 As String) ' Method body End Sub
In the preceding example, MyMethod
is a public method that accepts an integer parameter named parameter1
and a string parameter named parameter2
.
Next, let’s drill into the types of methods we can have in our classes:
- Sub procedures: Sub procedures are methods that do not return a value. They are typically used for performing actions or tasks without producing a result. Here’s an example:
Public Sub DisplayMessage(message As String) Console.WriteLine...