Page 1 of 1

Named Parameters. How they make you code more Readable. Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Posted 11 March 2009 - 07:08 PM

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 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.

Is This A Good Question/Topic? 1
  • +

Replies To: Named Parameters.

#2 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

Posted 20 March 2009 - 04:11 PM

Wow, that could really help me. Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 Lumenii  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 04-January 07

Posted 22 March 2009 - 10:33 PM

Good to know. I'll definitely be using this. I teach (informally) some VB.NET/ASP.NET, and this would make things clearer for some.
Was This Post Helpful? 0
  • +
  • -

#4 Steven.James  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 24-May 09

Posted 24 May 2009 - 01:14 AM

Thanks(person:=AdamSpeight2008, from:=Steven.James)
Was This Post Helpful? 1
  • +
  • -

#5 trythis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-July 12

Posted 17 July 2012 - 03:50 AM

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1