You might want to use the generic Result<T> pattern shown here:

http://www.codeproject.com/useritems/alternative_2_out_keyword.asp

On 7/12/06, Eddie Lascu <[EMAIL PROTECTED]> wrote:
Well,

My sample method is a strip-down version of what I have in reality. My real
method does more than just promoting the state (the application implements a
parser). The method returns a boolean to signal whether everything went
smoothly inside. Reference parameters, including a StringBuilder that
constructs an error message in case something goes bad, are passed in.
I had 'ref' in mind all along. For some reason, my fingers typed 'out' and
since I recently switched to VS2005 I thought that this might be a new
VS2005 behavior with regard to 'ref' parameters.

Thanks everybody for waking me up!!!

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Mike Andrews
Sent: Wednesday, July 12, 2006 11:54 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Passing value parameters in VS2005 / .NET
2.0


Yes, as all the others have said, ref is the way to go.

However, I'm wondering why you've not done something similar to this:

static MyStateEnum ChangeToState1(MyStateEnum enState){
 if ( enState == MyStateEnum.START_STATE){
   enState = MyStateEnum.STATE1;
 }

 return enState;

}

just a thought.


On 7/12/06, Eddie Lascu <[EMAIL PROTECTED]> 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
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com



--
"You hide yourself with sanctimony, I hide myself with narcissism."
--T-Bone Burnett

Marc C. Brooks
http://musingmarc.blogspot.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to