.NET Tip: Using a Parameter Array in Your Functions South Carolina

When you're creating a function that needs a variable list of arguments, use a parameter array.

Local Companies

Charlotte Web Design & Development
(704) 577-8242
Commodore point rd
Lake Wylie, SC
Pinnacle Consulting LLC
803 629 5243
98 Groves Wood Ct
Columbia, SC
Computer Control & Integration Inc
(864) 458-7587
1200 Woodruff Rd
Greenville, SC
Technology Resources For Effective Environments
(864) 223-0640
Greenwood, SC
Shaklee Products Distributors
(843) 821-4242
121 Stratford Dr
Summerville, SC
Net Solutions Technology Center Llc
(843) 525-6469
38 Sams Point Rd
Beaufort, SC
U S Computing Inc
(803) 806-8392
2026 Assembly St Ste 203
Columbia, SC
Paris Mountain Consulting
(864) 232-6011
833 Altamont Rd
Greenville, SC
Day Consultants Inc
(843) 626-6859
1293 Professional Dr Ste D
Myrtle Beach, SC
Sterling Software Systems Inc
(803) 407-3680
130 Centrum Dr
Irmo, SC

.NET Tip: Using a Parameter Array in Your Functions

provided by: 
Originally published at Internet.com


Most functions require a set list of parameters to operate. However, you'll create some functions that need a variable list of parameters. A good example of this is the AppendFormat method of the StringBuilder class. Depending on the format string you specify, you may need zero additional arguments, or nine, or any number.

This tip shows you how to create a function that concatenates any number of arguments and returns a single string. The function definition is as follows: private string Concatenate(string separator, params object[] parts) { System.Text.StringBuilder buffer = new System.Text.StringBuilder(); string sepValue = ""; foreach (object o in parts) { buffer.AppendFormat("{0}{1}", sepValue, o); sepValue = separator; } return buffer.ToString(); }

You call the function as follows: string result = Concatenate(" ", "test1", "test2", "test3");

The important thing to notice here is the params keyword in the parameter list. This indicates that the function accepts a variable list of arguments-in this case, an array of objects called parts. The parameter array needs to be the final parameter in the list of parameters, and as shown here, you can have non-parameter array parameters, as well.

If you called this function with just the first string, the first string value would be put in the separator parameter and the parts array would be empty. Although this may not make sense, as long as you have the first string, the call is syntactically valid.

Within the function, you create a StringBuilder object to create the concatenated string. You prepend each parameter array member with the separator string. The first time around, however, you don't have a separator string other than an empty string. This saves you from any messy calculations to determine whether you need to remove the last separator at the end of the string, for instance.

I have an instance of this function that generates a SqlCommand object for me. In that case, I use a trio of parameters in the parameter array: the parameter name, the data type (SqlDbType), and the value. The function verifies that the number of parameters is divisible by three. If it isn't, an exception is thrown.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

Charlotte Web Design & Development

7045778242
Commodore point rd
Lake Wylie, SC
http://www.charlottewebdevelopment.com

Regional Articles
- .NET Tip: Using a Parameter Array in Your Functions Aiken SC
- .NET Tip: Using a Parameter Array in Your Functions Anderson SC
- .NET Tip: Using a Parameter Array in Your Functions Beaufort SC
- .NET Tip: Using a Parameter Array in Your Functions Bennettsville SC
- .NET Tip: Using a Parameter Array in Your Functions Bluffton SC
- .NET Tip: Using a Parameter Array in Your Functions Boiling Springs SC
- .NET Tip: Using a Parameter Array in Your Functions Charleston SC
- .NET Tip: Using a Parameter Array in Your Functions Clemson SC
- .NET Tip: Using a Parameter Array in Your Functions Clover SC
- .NET Tip: Using a Parameter Array in Your Functions Columbia SC
- .NET Tip: Using a Parameter Array in Your Functions Conway SC
- .NET Tip: Using a Parameter Array in Your Functions Darlington SC
- .NET Tip: Using a Parameter Array in Your Functions Dillon SC
- .NET Tip: Using a Parameter Array in Your Functions Easley SC
- .NET Tip: Using a Parameter Array in Your Functions Florence SC
- .NET Tip: Using a Parameter Array in Your Functions Fort Mill SC
- .NET Tip: Using a Parameter Array in Your Functions Fountain Inn SC
- .NET Tip: Using a Parameter Array in Your Functions Gaffney SC
- .NET Tip: Using a Parameter Array in Your Functions Gaston SC
- .NET Tip: Using a Parameter Array in Your Functions Georgetown SC
- .NET Tip: Using a Parameter Array in Your Functions Goose Creek SC
- .NET Tip: Using a Parameter Array in Your Functions Greenville SC
- .NET Tip: Using a Parameter Array in Your Functions Greenwood SC
- .NET Tip: Using a Parameter Array in Your Functions Greer SC
- .NET Tip: Using a Parameter Array in Your Functions Hartsville SC
- .NET Tip: Using a Parameter Array in Your Functions Hilton Head Island SC
- .NET Tip: Using a Parameter Array in Your Functions Inman SC
- .NET Tip: Using a Parameter Array in Your Functions Irmo SC
- .NET Tip: Using a Parameter Array in Your Functions Ladson SC
- .NET Tip: Using a Parameter Array in Your Functions Lancaster SC
- .NET Tip: Using a Parameter Array in Your Functions Laurens SC
- .NET Tip: Using a Parameter Array in Your Functions Lexington SC
- .NET Tip: Using a Parameter Array in Your Functions Manning SC
- .NET Tip: Using a Parameter Array in Your Functions Moncks Corner SC
- .NET Tip: Using a Parameter Array in Your Functions Mount Pleasant SC
- .NET Tip: Using a Parameter Array in Your Functions Murrells Inlet SC
- .NET Tip: Using a Parameter Array in Your Functions Myrtle Beach SC
- .NET Tip: Using a Parameter Array in Your Functions Newberry SC
- .NET Tip: Using a Parameter Array in Your Functions North Augusta SC
- .NET Tip: Using a Parameter Array in Your Functions North Charleston SC
- .NET Tip: Using a Parameter Array in Your Functions Orangeburg SC
- .NET Tip: Using a Parameter Array in Your Functions Pickens SC
- .NET Tip: Using a Parameter Array in Your Functions Piedmont SC
- .NET Tip: Using a Parameter Array in Your Functions Rock Hill SC
- .NET Tip: Using a Parameter Array in Your Functions Seneca SC
- .NET Tip: Using a Parameter Array in Your Functions Simpsonville SC
- .NET Tip: Using a Parameter Array in Your Functions Spartanburg SC
- .NET Tip: Using a Parameter Array in Your Functions Summerville SC
- .NET Tip: Using a Parameter Array in Your Functions Sumter SC
- .NET Tip: Using a Parameter Array in Your Functions Taylors SC
- .NET Tip: Using a Parameter Array in Your Functions Travelers Rest SC
- .NET Tip: Using a Parameter Array in Your Functions Walterboro SC
- .NET Tip: Using a Parameter Array in Your Functions West Columbia SC
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Entertainment Home Services Retail & Consumer Services
Business Services Environmental Industrial Goods & Services Software
Career Family Insurance Technology
Cars Fashion Internet Telecommunications
Computer Hardware Financial Services Legal Transportation & Logistics
Construction Food & Beverage Pets Travel
Education Health Real Estate Wedding
Engineering Home Electronics