Today I realized what they are used for. So I wrote this tutorial so you can understand them too.
Named Parameters
To 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 tutorial
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.
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.
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 B
Another Example
Private Sub PrintHello(ByVal DoYouWantTheHello As Boolean)
If DoYouWantTheHello =True Then
Console.WriteLine("Hello World")
Else
Console.WriteLine("World")
End If
End Sub
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.






MultiQuote





|