.NET Tip: Using a Parameter Array in Your Functions Baltimore MD

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

Local Companies

Gp Solutions Inc
410-244-8548
201 N Charles St
Baltimore, MD
Legal Technology Solutions
410-547-8596
36 S Charles St
Baltimore, MD
Bithgroup Inc
410-962-1188
113 W Monument St
Baltimore, MD
Early Morning Software Inc
410-539-0901
227 Holliday St
Baltimore, MD
Affiliated Computer Services Inc
410-347-0794
200 Holliday St
Baltimore, MD
Accessing Cadd
410-296-0888
2545 Wilkens Ave
Baltimore, MD
Hyperion Solutions Corp
410-366-4211
5100 Falls RD
Baltimore, MD
Universal Software Solutions Inc
410-358-8851
6506 Cross Country Blvd
Baltimore, MD
Alherafi System Inc
410-433-7833
5209 York RD
Baltimore, MD
Albert D Valente
410-536-4255
4505 Leeds Ave
Baltimore, MD

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

Gp Solutions Inc

410-244-8548
201 N Charles St
Baltimore, MD

Related Local Events
DC Chamber Technology Series: Session 4
Dates: 12/10/2009 - 12/10/2009
Location: Robert H. Smith School at the Ronald Reagan Building and International Trade Center
Washington, DC
View Details

National Facilities Management & Technology (NFMT)
Dates: 3/16/2010 - 3/28/2010
Location: Baltimore Convention Center
Baltimore, MD
View Details

30th Annual York County Chamber Business & Technology Expo Sneak Preview
Dates: 5/4/2010 - 5/4/2010
Location: Toyota Arena York Expo Center
York, PA
View Details

The 30th Annual York County Chamber Business & Technology Expo presented by DOCEO Office Solutions
Dates: 5/5/2010 - 5/5/2010
Location: Toyota Arena York Expo Center
York, PA
View Details

CSI 2009: The Next Phase In Security
Dates: 10/24/2009 - 10/30/2009
Location: Gaylord National Resort and Convention Center
National Harbor, MD
View Details