ref assignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MRe

    ref assignment

    Hi,

    Why doesn't this work (it gives the error 'A ref or out argument must be
    an assignable variable').

    public void Caller()
    {
    int Value;
    Callee(ref Value = 5); // <-- Error here under Value = 5; it's
    assignable, look at me assign, woo!
    }
    public void Callee(ref int Value)
    {
    MessageBox.Show (Value.ToString ());
    }

    ..is ref evaluated before any operators (It also doesn't work with
    Callee(ref (Value = 5));)?

    I'm using VS2005; .NET2.0.

    Thank you,
    Eliott


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: ref assignment

    Elliot,

    I would think that 5 is not assignable.

    If anything, even if this syntax is legal (which I don't think it is),
    it isn't very clean. I would avoid it.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "MRe" <mreatdublindot iewrote in message
    news:OyctfBe0GH A.1268@TK2MSFTN GP02.phx.gbl...
    Hi,
    >
    Why doesn't this work (it gives the error 'A ref or out argument must be
    an assignable variable').
    >
    public void Caller()
    {
    int Value;
    Callee(ref Value = 5); // <-- Error here under Value = 5; it's
    assignable, look at me assign, woo!
    }
    public void Callee(ref int Value)
    {
    MessageBox.Show (Value.ToString ());
    }
    >
    ..is ref evaluated before any operators (It also doesn't work with
    Callee(ref (Value = 5));)?
    >
    I'm using VS2005; .NET2.0.
    >
    Thank you,
    Eliott
    >
    >

    Comment

    • Noah Sham

      #3
      Re: ref assignment

      The result of Value = 5 is not a reference to 'Value' it is a copy of
      'Value' and that copy cannot be passed by reference.

      Contrast this with this method
      public void Callee(int value)
      {
      }
      with this signature 'Callee(Value=5 )' will succeed because a copy of an int
      is expected.


      "MRe" <mreatdublindot iewrote in message
      news:OyctfBe0GH A.1268@TK2MSFTN GP02.phx.gbl...
      Hi,
      >
      Why doesn't this work (it gives the error 'A ref or out argument must be
      an assignable variable').
      >
      public void Caller()
      {
      int Value;
      Callee(ref Value = 5); // <-- Error here under Value = 5; it's
      assignable, look at me assign, woo!
      }
      public void Callee(ref int Value)
      {
      MessageBox.Show (Value.ToString ());
      }
      >
      ..is ref evaluated before any operators (It also doesn't work with
      Callee(ref (Value = 5));)?
      >
      I'm using VS2005; .NET2.0.
      >
      Thank you,
      Eliott
      >
      >

      Comment

      • MRe

        #4
        Re: ref assignment

        Thank you for the reply Noah,

        | The result of Value = 5 is not a reference to 'Value' it is a copy of
        | 'Value' and that copy cannot be passed by reference.

        Does that mean that every time an assignment is made to a simple type, it
        does the assignment to the variable on the stack (does C# use a stack?), and
        then, also creates a new copy to return as a result of the operator?

        Thanks again,
        Eliott


        "Noah Sham" <noahsham@veriz on.netwrote in message
        news:OJW3sYe0GH A.2072@TK2MSFTN GP06.phx.gbl...
        | The result of Value = 5 is not a reference to 'Value' it is a copy of
        | 'Value' and that copy cannot be passed by reference.
        |
        | Contrast this with this method
        | public void Callee(int value)
        | {
        | }
        | with this signature 'Callee(Value=5 )' will succeed because a copy of an
        int
        | is expected.
        |
        |
        | "MRe" <mreatdublindot iewrote in message
        | news:OyctfBe0GH A.1268@TK2MSFTN GP02.phx.gbl...
        | Hi,
        | >
        | Why doesn't this work (it gives the error 'A ref or out argument must
        be
        | an assignable variable').
        | >
        | public void Caller()
        | {
        | int Value;
        | Callee(ref Value = 5); // <-- Error here under Value = 5; it's
        | assignable, look at me assign, woo!
        | }
        | public void Callee(ref int Value)
        | {
        | MessageBox.Show (Value.ToString ());
        | }
        | >
        | ..is ref evaluated before any operators (It also doesn't work with
        | Callee(ref (Value = 5));)?
        | >
        | I'm using VS2005; .NET2.0.
        | >
        | Thank you,
        | Eliott
        | >
        | >
        |
        |


        Comment

        • Noah Sham

          #5
          Re: ref assignment

          C# has value types and reference types. The value types are stack based and
          the reference types are heap based with the reference pointer on the stack.
          double, byte, char, int32, int64 are value types as are structs constants
          and enumerations.

          In Callee(Value = 5) 'Value=5' is an expression that assigns 5 to Value and
          returns a copy of Value.
          int xyz = (Value = 5)
          The compiler realizes that it is an expression and not a variable reference.


          "MRe" <mreatdublindot iewrote in message
          news:eFUK3me0GH A.1288@TK2MSFTN GP03.phx.gbl...
          Thank you for the reply Noah,
          >
          | The result of Value = 5 is not a reference to 'Value' it is a copy of
          | 'Value' and that copy cannot be passed by reference.
          >
          Does that mean that every time an assignment is made to a simple type, it
          does the assignment to the variable on the stack (does C# use a stack?),
          and
          then, also creates a new copy to return as a result of the operator?
          >
          Thanks again,
          Eliott
          >
          >
          "Noah Sham" <noahsham@veriz on.netwrote in message
          news:OJW3sYe0GH A.2072@TK2MSFTN GP06.phx.gbl...
          | The result of Value = 5 is not a reference to 'Value' it is a copy of
          | 'Value' and that copy cannot be passed by reference.
          |
          | Contrast this with this method
          | public void Callee(int value)
          | {
          | }
          | with this signature 'Callee(Value=5 )' will succeed because a copy of an
          int
          | is expected.
          |
          |
          | "MRe" <mreatdublindot iewrote in message
          | news:OyctfBe0GH A.1268@TK2MSFTN GP02.phx.gbl...
          | Hi,
          | >
          | Why doesn't this work (it gives the error 'A ref or out argument must
          be
          | an assignable variable').
          | >
          | public void Caller()
          | {
          | int Value;
          | Callee(ref Value = 5); // <-- Error here under Value = 5; it's
          | assignable, look at me assign, woo!
          | }
          | public void Callee(ref int Value)
          | {
          | MessageBox.Show (Value.ToString ());
          | }
          | >
          | ..is ref evaluated before any operators (It also doesn't work with
          | Callee(ref (Value = 5));)?
          | >
          | I'm using VS2005; .NET2.0.
          | >
          | Thank you,
          | Eliott
          | >
          | >
          |
          |
          >
          >

          Comment

          • MRe

            #6
            Re: ref assignment

            Ah, okay - I was thinking I was doing like c++..

            void callee(int* Value);
            callee(&(Value = 5));

            ...buts no, I wasn't.. Hey! Did you say constants go on the stack - that's
            crazy! Well, I guess they got to go somewhere. Anyway..

            It looked like an annoying peculiarity, but it makes sense now, so I can
            live with it.
            Thank you very much for you help Noah :)
            Eliott


            "Noah Sham" <noahsham@veriz on.netwrote in message
            news:O52Ls5e0GH A.5100@TK2MSFTN GP05.phx.gbl...
            C# has value types and reference types. The value types are stack based
            and the reference types are heap based with the reference pointer on the
            stack. double, byte, char, int32, int64 are value types as are structs
            constants and enumerations.
            >
            In Callee(Value = 5) 'Value=5' is an expression that assigns 5 to Value
            and returns a copy of Value.
            int xyz = (Value = 5)
            The compiler realizes that it is an expression and not a variable
            reference.
            >
            >
            "MRe" <mreatdublindot iewrote in message
            news:eFUK3me0GH A.1288@TK2MSFTN GP03.phx.gbl...
            >Thank you for the reply Noah,
            >>
            >| The result of Value = 5 is not a reference to 'Value' it is a copy of
            >| 'Value' and that copy cannot be passed by reference.
            >>
            > Does that mean that every time an assignment is made to a simple type,
            >it
            >does the assignment to the variable on the stack (does C# use a stack?),
            >and
            >then, also creates a new copy to return as a result of the operator?
            >>
            >Thanks again,
            >Eliott
            >>
            >>
            >"Noah Sham" <noahsham@veriz on.netwrote in message
            >news:OJW3sYe0G HA.2072@TK2MSFT NGP06.phx.gbl.. .
            >| The result of Value = 5 is not a reference to 'Value' it is a copy of
            >| 'Value' and that copy cannot be passed by reference.
            >|
            >| Contrast this with this method
            >| public void Callee(int value)
            >| {
            >| }
            >| with this signature 'Callee(Value=5 )' will succeed because a copy of an
            >int
            >| is expected.
            >|
            >|
            >| "MRe" <mreatdublindot iewrote in message
            >| news:OyctfBe0GH A.1268@TK2MSFTN GP02.phx.gbl...
            >| Hi,
            >| >
            >| Why doesn't this work (it gives the error 'A ref or out argument
            >must
            >be
            >| an assignable variable').
            >| >
            >| public void Caller()
            >| {
            >| int Value;
            >| Callee(ref Value = 5); // <-- Error here under Value = 5; it's
            >| assignable, look at me assign, woo!
            >| }
            >| public void Callee(ref int Value)
            >| {
            >| MessageBox.Show (Value.ToString ());
            >| }
            >| >
            >| ..is ref evaluated before any operators (It also doesn't work with
            >| Callee(ref (Value = 5));)?
            >| >
            >| I'm using VS2005; .NET2.0.
            >| >
            >| Thank you,
            >| Eliott
            >| >
            >| >
            >|
            >|
            >>
            >>
            >
            >

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: ref assignment

              <"MRe" <mreatdublindot ie>wrote:
              Why doesn't this work (it gives the error 'A ref or out argument must be
              an assignable variable').
              The expression Value=5 is classified as a value (the result of the
              assignment) not a variable. Without getting "ref" involved, it's the
              same reason you can't do:

              int value;

              (value = 5) = 10;

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              Working...