.NET Tip: Using a Parameter Array in Your Functions Miami FL

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

Local Companies

Truelogic Software Solutions
(305) 407-9432
8345 NW 66th St.
Miami, FL
IBLESOFT INC
305.908.7957 EXT 100
7801 NW 37th Street
Doral, FL
Acl Computers Inc
(305) 826-5000
1711 W 38th Pl
Hialeah, FL
Game Rak Inc
(786) 517-1882
1790 W 49th St
Hialeah, FL
Software Development Solutions
305-274-2147
9745 SW 72nd St
Miami, FL
Home Care Software Solutions Inc
786-433-4700
9500 S Dadeland Blvd
Miami, FL
Alta Star Software Inc
305-279-8898
7700 N Kendall DR
Miami, FL
BlueClaw Group LLC
(305) 433-2006
1508 bay rd
miami beach, FL
TrueSoft
408-647-1434
911 Meridian Ave, Suite 201
Miami Beach, FL
Creative Technical Systems
(305) 512-2872
8181 NW 154th St
Hialeah, FL

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

Truelogic Software Solutions

3054079432
8345 NW 66th St.
Miami, FL
www.truelogicsoftware.com

Related Local Events
SCORE Business Counseling
Dates: 12/9/2009 - 12/9/2009
Location: The Chamber Office
Coral Springs, FL
View Details

Wake Up Coral Springs Breakfast!!
Dates: 12/15/2009 - 12/15/2009
Location: Marriott Heron Bay
Coral Springs, FL
View Details

Wednesday Evening Referral Group
Dates: 12/16/2009 - 12/16/2009
Location: WineStyles
Coral Springs, FL
View Details

Afternoon Chamber Referral Group Meeting
Dates: 1/21/2010 - 1/21/2010
Location: The Coral Springs Chamber Office
Coral Springs, FL
View Details

Wednesday Evening Referral Group
Dates: 2/3/2010 - 2/5/2010
Location: WineStyles
Coral Springs, FL
View Details