Home > Microsoft .Net Development Tips > ARCHIVE: Tips & Tricks > Named Visual Basic Arguments
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ARCHIVE: TIPS & TRICKS

Named Visual Basic Arguments


John Smiley
08.08.2000
Rating: -4.38- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Named Visual Basic Arguments

One of the themes I emphasize in my computer classes over and over again is the importance of writing code that is readable -- that is, code that other programmers and developers will be able to understand, and if necessary, easily modify in the future. Some obvious ways to write readable code include the use of program comments in your code -- no matter what the language you are using to develop your program, all major languages provide for comments. Something else that can make your Visual Basic more readable is the use of Named Arguments.

Let me illustrate by executing the Visual Basic MsgBox Function to display a Windows Message Box. The Visual Basic MsgBox function has one required argument (Prompt), and four optional arguments (Buttons, Title, HelpFile and Context).

MsgBox "I love Visual Basic"

By default, this code will display a Message Box with a single command button captioned OK, with the text "I love Visual Basic", and the Visual Basic Project name displayed in the Title Bar of the Message Box.

Suppose I'm not happy with the default Title in the Message Box, and I decide I want to customize it. Doing this is easy-all I need to do is supply the Title argument to the MsgBox function. However, since Title is the third argument, I either need to supply the second argument -- Buttons, which is by default presumed to be the value vbOKOnly -- or provide a 'comma placeholder', like this.

MsgBox "I love Visual Basic",, "SearchVB.Com"



Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
ARCHIVE: Tips & Tricks
SearchVB wants your tips
Find out what VS.NET modules are installed
Retrieving icons from an EXE-file
Toggling Boolean values
VS.NET multiple tabbed windows
Set the value of a DataField in code behind
Easy grid design
Open using last Visual Studio.NET layout
Save development time by using inherited forms
Reusing code in VS.NET

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


Notice the two commas back-to-back, with no value in-between. This is the 'comma placeholder' and is how we tell VB that although we have a value for the third argument, we have no explicit value for the second argument.

When we execute this code, we'll see a Message Box that reads "I love Visual Basic", and that has "SearchVB.Com" for its Title Bar.

Named Arguments can make passing optional arguments easier-and make your code infinitely easier to read and modify. For instance, the code we wrote above can be re-written the following way using Named Arguments.

MsgBox Prompt:="I love Visual Basic", Title:="SearchVB.Com"

With Named Arguments, we specify the name of the argument, followed by a colon and equals sign (:=), then the value for the argument. By using Named Arguments, we don't need to provide a 'comma placeholder' for the second argument Buttons. Since we are naming the argument, VB knows that 'SearchVB.Com' is the value for the Optional Argument 'Title'. And since we name the arguments, being able to read and understand the code in the future is much easier.

********************************************************************
Written by John Smiley, MCP, MCSD and MCT, author, and adjunct professor of Computer Science at Penn State University in Abington, Philadelphia University, and Holy Family College. John has been teaching computer programming for nearly 20 years.

John Smiley is president of Smiley and Associates, http://www.johnsmiley.com/smass/smass.htm a computer consulting firm located in New Jersey.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts