I been wondering for a quite a while what
VariableName:= was used for when you entering the values of parameter of a subroutine, function or method.
Today I realized what they are used for. So I wrote this tutorial so you can understand them too.
Named ParametersTo explain them, imagine that in your code you have the following routine. (Without using Named Parameters)
I know it's a bit simple but the principles that count in this tutorialCODE
Private Function Divide(Byval Divider as Double, ByVal Divisor As Double) As Double
' Work out the quotient
Return Divider / Divisor
End Function
And you use it like.
CODE
Dim A As Double =10
Dim B As Double =2
Dim C As Double = Divide(A,B)
Without using Intellisense what A & B are being using for by the Divide Function is a bit hard.
In this example you could probably guess but what if the function was called
xyz it is a lot harder.
But by using
named parameters it's a lot easier.
CODE
Dim A As Double =10
Dim B As Double =2
Dim C As Double = Divide(Divider:=A,Divisor:=B)
Which is saying;-
The
Divide function's
Divider parameter is the contents of variable
A The
Divide function's
Divisor parameter is the contents of variable
BAnother Example
CODE
Private Sub PrintHello(ByVal DoYouWantTheHello As Boolean)
If DoYouWantTheHello =True Then
Console.WriteLine("Hello World")
Else
Console.WriteLine("World")
End If
End Sub
CODE
PrintHello(DoYouWantTheHello:=True)
Using this alongside with using good variable names makes, in my opinion, the code a lot easier to understand by looking at it.