School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 308,428 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,225 people online right now. Registration is fast and FREE... Join Now!




> Named Parameters., How they make you code more Readable.

AdamSpeight2008
Group Icon



post 11 Mar, 2009 - 06:08 PM
Post #1


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
CODE

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 B


Another 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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Posts in this topic


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/24/09 01:48PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month