passing objects using ref

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

    #1

    passing objects using ref

    For examples sake I have made up a very simple example.

    I have an object called foo1 which is of type foo. I want to be able to call
    a funtion called myfunc as follows:

    myfunc(ref foo1)

    here's the function :

    public void myfunc(ref object foo)
    {
    do some stuff
    }

    I get the following errors:
    The best overloaded method match for myfunc(ref object)' has some invalid
    arguments
    Argument '1' cannot convert from 'ref foo' to 'ref object'


  • William Ryan

    #2
    Re: passing objects using ref

    Take out the object reference in the function signature.
    "Robert Lario" <lario@nospam.i nherit.com> wrote in message
    news:%236lo0STf DHA.956@TK2MSFT NGP09.phx.gbl.. .[color=blue]
    > For examples sake I have made up a very simple example.
    >
    > I have an object called foo1 which is of type foo. I want to be able to[/color]
    call[color=blue]
    > a funtion called myfunc as follows:
    >
    > myfunc(ref foo1)
    >
    > here's the function :
    >
    > public void myfunc(ref object foo)
    > {
    > do some stuff
    > }
    >
    > I get the following errors:
    > The best overloaded method match for myfunc(ref object)' has some invalid
    > arguments
    > Argument '1' cannot convert from 'ref foo' to 'ref object'
    >
    >[/color]


    Comment

    • Robert Lario

      #3
      Re: passing objects using ref

      Thanks for the replay -- could you be more specific? I tired what I thought
      you meant -- this creates a syntax error..

      robert
      "William Ryan" <dotnetguru@com cast.nospam.net > wrote in message
      news:eNiFAVTfDH A.3024@tk2msftn gp13.phx.gbl...[color=blue]
      > Take out the object reference in the function signature.
      > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
      > news:%236lo0STf DHA.956@TK2MSFT NGP09.phx.gbl.. .[color=green]
      > > For examples sake I have made up a very simple example.
      > >
      > > I have an object called foo1 which is of type foo. I want to be able to[/color]
      > call[color=green]
      > > a funtion called myfunc as follows:
      > >
      > > myfunc(ref foo1)
      > >
      > > here's the function :
      > >
      > > public void myfunc(ref object foo)
      > > {
      > > do some stuff
      > > }
      > >
      > > I get the following errors:
      > > The best overloaded method match for myfunc(ref object)' has some[/color][/color]
      invalid[color=blue][color=green]
      > > arguments
      > > Argument '1' cannot convert from 'ref foo' to 'ref object'
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: passing objects using ref

        Robert,

        If you are passing a parameter by reference, then you have to make sure
        that the type of the variable passed in is the same as the type of the
        parameter. That is why you get that error. So, if your type is of type
        foo, and you are trying to pass it into a method that needs a "ref object"
        it will fail, unless you create a variable of type object first and pass
        that.

        Hope this helps.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nicholas.paldin o@exisconsultin g.com

        "Robert Lario" <lario@nospam.i nherit.com> wrote in message
        news:OnM%23kpTf DHA.1872@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Thanks for the replay -- could you be more specific? I tired what I[/color]
        thought[color=blue]
        > you meant -- this creates a syntax error..
        >
        > robert
        > "William Ryan" <dotnetguru@com cast.nospam.net > wrote in message
        > news:eNiFAVTfDH A.3024@tk2msftn gp13.phx.gbl...[color=green]
        > > Take out the object reference in the function signature.
        > > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
        > > news:%236lo0STf DHA.956@TK2MSFT NGP09.phx.gbl.. .[color=darkred]
        > > > For examples sake I have made up a very simple example.
        > > >
        > > > I have an object called foo1 which is of type foo. I want to be able[/color][/color][/color]
        to[color=blue][color=green]
        > > call[color=darkred]
        > > > a funtion called myfunc as follows:
        > > >
        > > > myfunc(ref foo1)
        > > >
        > > > here's the function :
        > > >
        > > > public void myfunc(ref object foo)
        > > > {
        > > > do some stuff
        > > > }
        > > >
        > > > I get the following errors:
        > > > The best overloaded method match for myfunc(ref object)' has some[/color][/color]
        > invalid[color=green][color=darkred]
        > > > arguments
        > > > Argument '1' cannot convert from 'ref foo' to 'ref object'
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Robert Lario

          #5
          Re: passing objects using ref

          Yes. That is what I feared. I was hoping that since foo is of type object
          that ploymorphism would take care of the up cast.


          "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
          in message news:Ok0o3yTfDH A.2984@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Robert,
          >
          > If you are passing a parameter by reference, then you have to make[/color]
          sure[color=blue]
          > that the type of the variable passed in is the same as the type of the
          > parameter. That is why you get that error. So, if your type is of type
          > foo, and you are trying to pass it into a method that needs a "ref object"
          > it will fail, unless you create a variable of type object first and pass
          > that.
          >
          > Hope this helps.
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - nicholas.paldin o@exisconsultin g.com
          >
          > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
          > news:OnM%23kpTf DHA.1872@TK2MSF TNGP09.phx.gbl. ..[color=green]
          > > Thanks for the replay -- could you be more specific? I tired what I[/color]
          > thought[color=green]
          > > you meant -- this creates a syntax error..
          > >
          > > robert
          > > "William Ryan" <dotnetguru@com cast.nospam.net > wrote in message
          > > news:eNiFAVTfDH A.3024@tk2msftn gp13.phx.gbl...[color=darkred]
          > > > Take out the object reference in the function signature.
          > > > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
          > > > news:%236lo0STf DHA.956@TK2MSFT NGP09.phx.gbl.. .
          > > > > For examples sake I have made up a very simple example.
          > > > >
          > > > > I have an object called foo1 which is of type foo. I want to be able[/color][/color]
          > to[color=green][color=darkred]
          > > > call
          > > > > a funtion called myfunc as follows:
          > > > >
          > > > > myfunc(ref foo1)
          > > > >
          > > > > here's the function :
          > > > >
          > > > > public void myfunc(ref object foo)
          > > > > {
          > > > > do some stuff
          > > > > }
          > > > >
          > > > > I get the following errors:
          > > > > The best overloaded method match for myfunc(ref object)' has some[/color]
          > > invalid[color=darkred]
          > > > > arguments
          > > > > Argument '1' cannot convert from 'ref foo' to 'ref object'
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: passing objects using ref

            Robert,

            That would be an error if it allowed it. Because of the "ref" keyword,
            that means that the method can modify the value of the parameter passed in.
            If it allowed you to pass in an instance of type foo, and the method took a
            parameter of type object, what if the method assigned a type other than foo
            to the parameter? Upon return, the variable passed to the parameter
            couldn't be accepted or accessed, since it points to a different type (and
            incorrect, on top of that).


            --
            - Nicholas Paldino [.NET/C# MVP]
            - nicholas.paldin o@exisconsultin g.com

            "Robert Lario" <lario@nospam.i nherit.com> wrote in message
            news:eal1AOUfDH A.1764@TK2MSFTN GP09.phx.gbl...[color=blue]
            > Yes. That is what I feared. I was hoping that since foo is of type object
            > that ploymorphism would take care of the up cast.
            >
            >
            > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
            wrote[color=blue]
            > in message news:Ok0o3yTfDH A.2984@TK2MSFTN GP11.phx.gbl...[color=green]
            > > Robert,
            > >
            > > If you are passing a parameter by reference, then you have to make[/color]
            > sure[color=green]
            > > that the type of the variable passed in is the same as the type of the
            > > parameter. That is why you get that error. So, if your type is of type
            > > foo, and you are trying to pass it into a method that needs a "ref[/color][/color]
            object"[color=blue][color=green]
            > > it will fail, unless you create a variable of type object first and pass
            > > that.
            > >
            > > Hope this helps.
            > >
            > >
            > > --
            > > - Nicholas Paldino [.NET/C# MVP]
            > > - nicholas.paldin o@exisconsultin g.com
            > >
            > > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
            > > news:OnM%23kpTf DHA.1872@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
            > > > Thanks for the replay -- could you be more specific? I tired what I[/color]
            > > thought[color=darkred]
            > > > you meant -- this creates a syntax error..
            > > >
            > > > robert
            > > > "William Ryan" <dotnetguru@com cast.nospam.net > wrote in message
            > > > news:eNiFAVTfDH A.3024@tk2msftn gp13.phx.gbl...
            > > > > Take out the object reference in the function signature.
            > > > > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
            > > > > news:%236lo0STf DHA.956@TK2MSFT NGP09.phx.gbl.. .
            > > > > > For examples sake I have made up a very simple example.
            > > > > >
            > > > > > I have an object called foo1 which is of type foo. I want to be[/color][/color][/color]
            able[color=blue][color=green]
            > > to[color=darkred]
            > > > > call
            > > > > > a funtion called myfunc as follows:
            > > > > >
            > > > > > myfunc(ref foo1)
            > > > > >
            > > > > > here's the function :
            > > > > >
            > > > > > public void myfunc(ref object foo)
            > > > > > {
            > > > > > do some stuff
            > > > > > }
            > > > > >
            > > > > > I get the following errors:
            > > > > > The best overloaded method match for myfunc(ref object)' has some
            > > > invalid
            > > > > > arguments
            > > > > > Argument '1' cannot convert from 'ref foo' to 'ref object'
            > > > > >
            > > > > >
            > > > >
            > > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Robert Lario

              #7
              Re: passing objects using ref

              Sure that could happen - And that is kind of what I want to do.

              Using reflection I examine the class structure of the object passed in.
              Then I build a new class in memory that extends the class type of the object
              passed in. Then I return an instance of the new extended class wrapped
              around the old object. So for example, if foo is passed and is of type Foo,
              I build a new class called __Foo that Extends Foo and return an instance of
              it.

              I was hoping that I could call my methods as such:

              myFunc(ref foo);

              but know I have to do this:

              foo = (Foo) myFunc(foo);

              I just want to hide the casting and assignment.

              Ideally I could just:

              myFunc(foo);

              But this does not appear to be supported, like in C++ with the &

              any suggestions would be greatly appreciated.

              Robert


              "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
              in message news:OK7zqGVfDH A.696@TK2MSFTNG P09.phx.gbl...[color=blue]
              > Robert,
              >
              > That would be an error if it allowed it. Because of the "ref"[/color]
              keyword,[color=blue]
              > that means that the method can modify the value of the parameter passed[/color]
              in.[color=blue]
              > If it allowed you to pass in an instance of type foo, and the method took[/color]
              a[color=blue]
              > parameter of type object, what if the method assigned a type other than[/color]
              foo[color=blue]
              > to the parameter? Upon return, the variable passed to the parameter
              > couldn't be accepted or accessed, since it points to a different type (and
              > incorrect, on top of that).
              >
              >
              > --
              > - Nicholas Paldino [.NET/C# MVP]
              > - nicholas.paldin o@exisconsultin g.com
              >
              > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
              > news:eal1AOUfDH A.1764@TK2MSFTN GP09.phx.gbl...[color=green]
              > > Yes. That is what I feared. I was hoping that since foo is of type[/color][/color]
              object[color=blue][color=green]
              > > that ploymorphism would take care of the up cast.
              > >
              > >
              > > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
              > wrote[color=green]
              > > in message news:Ok0o3yTfDH A.2984@TK2MSFTN GP11.phx.gbl...[color=darkred]
              > > > Robert,
              > > >
              > > > If you are passing a parameter by reference, then you have to make[/color]
              > > sure[color=darkred]
              > > > that the type of the variable passed in is the same as the type of the
              > > > parameter. That is why you get that error. So, if your type is of[/color][/color][/color]
              type[color=blue][color=green][color=darkred]
              > > > foo, and you are trying to pass it into a method that needs a "ref[/color][/color]
              > object"[color=green][color=darkred]
              > > > it will fail, unless you create a variable of type object first and[/color][/color][/color]
              pass[color=blue][color=green][color=darkred]
              > > > that.
              > > >
              > > > Hope this helps.
              > > >
              > > >
              > > > --
              > > > - Nicholas Paldino [.NET/C# MVP]
              > > > - nicholas.paldin o@exisconsultin g.com
              > > >
              > > > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
              > > > news:OnM%23kpTf DHA.1872@TK2MSF TNGP09.phx.gbl. ..
              > > > > Thanks for the replay -- could you be more specific? I tired what I
              > > > thought
              > > > > you meant -- this creates a syntax error..
              > > > >
              > > > > robert
              > > > > "William Ryan" <dotnetguru@com cast.nospam.net > wrote in message
              > > > > news:eNiFAVTfDH A.3024@tk2msftn gp13.phx.gbl...
              > > > > > Take out the object reference in the function signature.
              > > > > > "Robert Lario" <lario@nospam.i nherit.com> wrote in message
              > > > > > news:%236lo0STf DHA.956@TK2MSFT NGP09.phx.gbl.. .
              > > > > > > For examples sake I have made up a very simple example.
              > > > > > >
              > > > > > > I have an object called foo1 which is of type foo. I want to be[/color][/color]
              > able[color=green][color=darkred]
              > > > to
              > > > > > call
              > > > > > > a funtion called myfunc as follows:
              > > > > > >
              > > > > > > myfunc(ref foo1)
              > > > > > >
              > > > > > > here's the function :
              > > > > > >
              > > > > > > public void myfunc(ref object foo)
              > > > > > > {
              > > > > > > do some stuff
              > > > > > > }
              > > > > > >
              > > > > > > I get the following errors:
              > > > > > > The best overloaded method match for myfunc(ref object)' has[/color][/color][/color]
              some[color=blue][color=green][color=darkred]
              > > > > invalid
              > > > > > > arguments
              > > > > > > Argument '1' cannot convert from 'ref foo' to 'ref object'
              > > > > > >
              > > > > > >
              > > > > >
              > > > > >
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Jon Skeet

                #8
                Re: passing objects using ref

                Robert Lario <lario@nospam.i nherit.com> wrote:[color=blue]
                > Sure that could happen - And that is kind of what I want to do.
                >
                > Using reflection I examine the class structure of the object passed in.
                > Then I build a new class in memory that extends the class type of the object
                > passed in.[/color]

                But that's *not* the same as Nick was suggesting. Nick was suggesting
                the equivalent of:

                string x = "hello";
                Foo (ref x);


                void Foo (ref object y)
                {
                y = Encoding.ASCII;
                }

                then x would no longer hold a reference to a string, it holds a
                reference to an Encoding instance (well, an ASCIIEncoding instance to
                be precise). Bang - type safety is broken. So, what you wanted to do
                wouldn't break type safety (because you'd still be assigning a
                compatible reference) but the only way to express it is in a way which
                would allow type safety to be broken in other situations - so the
                compiler disallows it.
                [color=blue]
                > Then I return an instance of the new extended class wrapped
                > around the old object. So for example, if foo is passed and is of type Foo,
                > I build a new class called __Foo that Extends Foo and return an instance of
                > it.
                >
                > I was hoping that I could call my methods as such:
                >
                > myFunc(ref foo);
                >
                > but know I have to do this:
                >
                > foo = (Foo) myFunc(foo);
                >
                > I just want to hide the casting and assignment.[/color]

                You can't, for precisely the reason above. If the method doesn't know
                in advance what kind of type it's going to get, it can't assure the
                compiler that it will give something compatible back. I'm not sure
                whether or not generics would let you do what you want - maybe...
                [color=blue]
                > Ideally I could just:
                >
                > myFunc(foo);
                >
                > But this does not appear to be supported, like in C++ with the &
                >
                > any suggestions would be greatly appreciated.[/color]

                Stick with the assignment and cast.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                Working...