.NET Tip: Returning Mulitple Objects from a Single Method Call Wisconsin

Allow a single call to your method to return multiple objects using the ref keyword with parameters.

Local Companies

HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
Statpac Inc
(715) 442-2261
1200 1st St
Pepin, WI
Realtime Control Works
(608) 755-1085
1011 Alpine Dr
Janesville, WI


.NET Tip: Returning Mulitple Objects from a Single Method Call

provided by: 
Originally published at Internet.com


Each method can return only a single value from the method call. What happens when you need to return more information? One solution is for your method to return a complex data type. It could return a struct or an object of a class you define. In many situations this makes sense, especially when you want to return a set of related data. In other instances, however, a complex data type may not really fit what you need or may be overly complex. Another option is to return values through the parameters to the method. By default, parameters are passed by value; this means that your method is not able to change the value of the parameter and pass it back to the calling routine. C# has a couple keywords that you can use to change the behavior of how parameters are passed. You can use the ref keyword to indicate that the parameter will be passed by reference. This allows your method to modify the value of the parameter and have the calling routine see that change when your method finishes executing. The ref keyword should appear in your method's parameter list before the data type of each parameter that you want to pass by reference. Here is a method that can accept some parameters by reference. private bool RefParameters(int Input, ref int Output, ref string Message) { Output = Input * 10; Message = "Your input value was: " + Input.ToString(); return true; }

First, notice that this method has a boolean return value. The method call itself will always return a true value. Second, the method expects three parameters. The first parameter is an integer that is passed by value so it cannot be changed by the method. The next two parameters are an integer and string that are each passed by reference. Because both these parameters have the ref keyword, they can be modified during the execution of the method and any changes will be reflected back in the calling routine. The body of the method should be strightforward. It assigns new values to the Output and Message parameters before returning true from the method call. Here is an example of calling the above method. bool ReturnValue = false; int IntInput = 5; int IntOutput = 1; string StringOutput = "Initial Text"; Debug.Print(String.Format("Initial Values:\r\nReturnValue: {0}\r\nIntInput: {1}\r\nIntOutput: {2}\r\nStringOutput: {3}\r\n", ReturnValue, IntInput, IntOutput, StringOutput)); ReturnValue = RefParameters(IntInput, ref IntOutput, ref StringOutput); Debug.Print(String.Format("New Values:\r\nReturnValue: {0}\r\nIntInput: {1}\r\nIntOutput: {2}\r\nStringOutput: {3}\r\n", ReturnValue, IntInput, IntOutput, StringOutput));

A variable to hold the return value of the method is declared and initialized as well as variables to hold the input and output parameters to the method. The call to the method has Debug statements before and after it to print out the values of all the variables. One point to notice is that the ref keyword also must precede all ref parameters in the method call itself. Your code will not compile if you forget to include the ref keyword on a call to a method that has ref paremeters. You can see from the following output that the value of the IntOutput and StringOutput variable have changed after the method call. Initial Values: ReturnValue: False IntInput: 5 IntOutput: 1 StringOutput: Initial Text New Values: ReturnValue: True IntInput: 5 IntOutput: 50 StringOutput: Your input value was: 5

If you look at the example calling code, you will notice that the variables passed in as ref parameters have been intialized when they were delcared. The ref keyword requires that any variable passed as a ref parameter be initialized before the method call. As aan alternative, you can use the out keyword in the method declaration and method call instead of ref. Parameters that use the out keyword are not required to be initialized before the method call. Other than the initialization requirement, the out and ref keywords have the same behavior.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

R.E. Coker and Associates, Inc.

262-723-8104
108 W Court St.
Elkhorn, WI
http://www.recoker.com

Regional Articles
- .NET Tip: Returning Mulitple Objects from a Single Method Call Appleton WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Baraboo WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Beaver Dam WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Beloit WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Brookfield WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Burlington WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Cedarburg WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Chippewa Falls WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Cudahy WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call De Pere WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Delavan WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Eau Claire WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Elkhorn WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Fond Du Lac WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Fort Atkinson WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Franklin WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Green Bay WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Hartland WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Janesville WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Kaukauna WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Kenosha WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call La Crosse WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Lake Geneva WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Manitowoc WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Marinette WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Marshfield WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Menasha WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Menomonee Falls WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Menomonie WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Merrill WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Middleton WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Milwaukee WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Mosinee WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Mukwonago WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Muskego WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Neenah WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call New Berlin WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Oak Creek WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Oconomowoc WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Onalaska WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Oshkosh WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Pewaukee WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Racine WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Rhinelander WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Rice Lake WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call River Falls WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Schofield WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Shawano WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Sheboygan WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call South Milwaukee WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Stevens Point WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Sturgeon Bay WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Sun Prairie WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Superior WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Thiensville WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Two Rivers WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Watertown WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Waukesha WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Waupaca WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Wausau WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call West Bend WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Whitewater WI
- .NET Tip: Returning Mulitple Objects from a Single Method Call Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

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

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