Visual Basic Methods

We use methods (or procedures) for reusing code and making code more understandable. A method is just a block of code that you can call, and huge programs have methods. You will see how we can use methods in later programs we create. You have already used methods before, like WriteLine(); some methods allow you to pass data, as you will see later in this tutorial.

Syntax

Private Sub methodName()

End Sub

You can also use the public keyword or the private keyword; you will learn more about public vs private in the ‘classes’ tutorial.

Sub Main()

    End Sub

    Private Sub hello()

        Console.WriteLine("Hello There!")
        Console.ReadLine()

    End Sub

If you run this (F5) you will see a black console window appear and it will close quickly. This is because we have not called the method yet. We call the method by its name, which is ‘hello’. We put the method in Sub Main() like this:

Sub Main()

        hello()

    End Sub

The program should now run and you should see it say Hello There!  You can create methods using sub, as above, or function. Sub means subroutine and does not return a value, while function returns a value.

Passing Data

You can pass data in a method (known as parameters).

Syntax

  Function <FunctionName)(parameter_1, parameter_2)

        
  End Function

In this example we use the Function keyword; however, you can use the Public Function or Private Function, which controls the visibility of the method. Private means it is available in the current class only, while public means it is available to other classes as well. You will learn more about classes in the next tutorial. If you pass any parameters into a method it will expect a return value. If you’re not going to pass any values, use Private/Public Sub().

Example VAT Calculator

   Function VAT(ByVal pCost As Double, ByVal cRate As Double)

        cRate = (cRate + 100) / 100
        Return pCost * cRate

    End Function

Code Explained

  • Create a method using the function keyword
  • Specify the function parameters, in this case there are two: pCost (product cost) and cRate (current VAT rate). The data type is double because product cost can contain decimals, as can VAT rate
  • Then cRate = cRate + 100 / 100, which gets the percentage multiplier. The cRate will be the value the user enters, for example 20. 20+100 = 120. Then 120/100 = 1.2
  • Then return the value: pCost multiplied by cRate

Next, put this in the Sub Main(), and make sure you comment out the hello method:

 Console.Write("Please Enter product cost:")
 Dim PC As String = Console.ReadLine()

 Console.Write("Please enter current VAT rate:")
 Dim cR As String = Console.ReadLine()


 Dim result As Double = VAT(Convert.ToDouble(PC), Convert.ToDouble(cR))
 Console.WriteLine("{0:C}", result)

 Console.ReadLine()

This takes the user input and stores it in the variables PC and cR, and then converts it to doubles. VAT is the method we made and it needs arguments; when you type VAT( you will see a tool-tip showing you the data types this method accepts. You need to convert the user input (string) to double. The variables made in a method are local and cannot be used outside that method.

[ Continued ]