Each method can only return 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 value. 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.
Article: Leveraging Your Flash Development with Silverlight
You're not giving up Flash any time soon (and we don't blame you.) But if you could get your Flash application working in Silverlight, why wouldn't you? We show you the tools and techniques required to have your rockin' Flash application rolled for Silverlight. Learn more here.
»
Article: What Does it Take to Build the Best RIA?
With the proliferation of Rich Interactive Application (RIA) platform choices out there, you no longer have to take a one-size-fits-all approach to developing your next RIA application. Knowing the strengths (and weaknesses) of each platform can help you to decide the best RIA for your next application.
»
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.
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 an 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.
Tools:
Add www.codeguru.com to your favorites Add www.codeguru.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed