You need to use "ref" rather than "out" when you want the called routine to be
able to see the value passed in. When you specify "out" that indicates that
the called routine must set the value.
"ref" is sort of a shortcut for "both in and out".
At 11:14 AM 7/12/2006, Eddie Lascu wrote
>Consider this:
>
>using System;
>using System.Collections.Generic;
>using System.Text;
>
>namespace TestOutParameterBehavior
>{
> public enum MyStateEnum
> {
> START_STATE = 0,
> STATE1 = 1,
> STATE2 = 2,
> END_STATE
> }
>
> class Program
> {
> static void Main(string[] args)
> {
> MyStateEnum enState = MyStateEnum.START_STATE;
>
> ChangeToState1(out enState);
>
> if (enState == MyStateEnum.START_STATE)
> Console.WriteLine("The state didn't change!!!");
> else
> Console.WriteLine("The state did change!!!");
>
> Console.WriteLine("Press <Enter> to Exit...");
> Console.ReadLine();
> }
>
> static void ChangeToState1(out MyStateEnum enState)
> {
> if( enState == MyStateEnum.START_STATE )
> enState = MyStateEnum.STATE1;
> }
> }
>}
>
>Why is it that I cannot test the value of my enumeration variable before I
>assign to it? This small test app doesn't compile and the errors I am
>getting are:
>1. Use of unassigned out parameter 'enState';
>2. The out parameter 'enState' must be assigned to before control leaves the
>current method;
>
>Obviously, I need to use 'out' parameters, because enumerations are value
>types and I need to preserve the changes done in the ChangeToState1 method.
>Am I missing something? Clearly this wasn't the case in VS2003 / .NET 1.1. I
>would understand the environment generating a warning in my case, but an
>error? It's like I need to assign the out parameter no matter what. Well, in
>some cases (decided by the 'if' test) I don't want to do the assignment.
>
>Regards,
>Eddie
J. Merrill / Analytical Software Corp
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com