.NET Tip: Using a Parameter Array in Your Functions Albany NY

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

Local Companies

CompareTechProviders.com
518-207-3445
5 Washington Place
Troy, NY
Cgi-Ams
(518) 434-0294
12 Corporate Woods B
Albany, NY
Compass Systems Group
(518) 426-9540
100 State St
Albany, NY
Keane Inc
(518) 432-3209
18 Corporate Woods Blvd Ste 43
Albany, NY
Computer Visions
(518) 437-1600
16 Corporate Woods
Albany, NY
Softtek Integration Systems Inc
(518) 464-9344
1500 Central Ave
Albany, NY
T I M Formation Inc
(518) 456-6211
1913 Central Ave Ste 1
Albany, NY
Comsys Technical Svcs Inc
(518) 458-6145
125 Wolf Rd
Albany, NY
Cgi-Ams
(518) 218-7700
100 Great Oaks Blvd
Albany, NY
I Mg
(518) 482-0558
8 Automation Ln
Albany, NY

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

CompareTechProviders.com

Compare providers of business essential software solutions. Request information, pricing from multiple providers.

518-207-3445
5 Washington Place
Troy, NY
CompareTechProviders.com

Review and compare providers of business essential software solutions. Request information, pricing and demos from multiple providers. Make the right choice for your business.

Related Local Event
Thirty Low/No-Cost Things to Keep Your Computers and Network Running Smoothly
Dates: 7/21/2009 - 7/21/2009
Location: Albany-Colonie Regional Chamber of Commerce
Albany, NY
View Details