ref params

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

    ref params

    i'm new to C#...

    How can I create an out or ref param for a method without having to declare
    it in the caling code ? Like the DataAdapter can do on its Fill method.

    DataTable dt = new DataTable();
    SqlDataAdapter. Fill(dt);

    'dt' is passed by ref, but it is not required to pass it as such:
    SqlDataAdapter. Fill(ref dt);


    Thanks,
    Bill


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: ref params

    Bill,

    This is incorrect. dt is not passed by ref. Rather, DataTable is a
    reference type, meaning that when you assign it, you pass the reference. If
    you were passing the parameter by ref, then you would be able to change the
    reference that you are passing in.

    For example, if the Fill method did something like this:

    public void Fill(DataTable table)
    {
    table = new DataTable();
    }

    When you return, you will see that the parameter passed to table doesn't
    in fact change.

    Hope this helps.


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

    "Bill H" <bill@hostunkno wn.comwrote in message
    news:%23m3ZpHN4 GHA.4820@TK2MSF TNGP06.phx.gbl. ..
    i'm new to C#...
    >
    How can I create an out or ref param for a method without having to
    declare it in the caling code ? Like the DataAdapter can do on its Fill
    method.
    >
    DataTable dt = new DataTable();
    SqlDataAdapter. Fill(dt);
    >
    'dt' is passed by ref, but it is not required to pass it as such:
    SqlDataAdapter. Fill(ref dt);
    >
    >
    Thanks,
    Bill
    >

    Comment

    • Bill H

      #3
      Re: ref params

      Thanks for your response Nicholas .

      but I'm still a bit confused...

      When da.Fill returns, my DataTable param var is populated. That implies
      that it was passed by ref, correct ? But, I thought (and read, I think) in
      C#, in order to declare a parameter as ref, you need to declare it as such.
      You are right in your example, when I create a sub of my own, similiar to
      the da.Fill method, the param is not affected on the calling side. So why
      is the DataAdapter Fil method able to affect the DT param, and not mine ?

      Thanks for being patient. I'm a veteran VB programmer, so this is a
      humbling experience.

      Bill : )



      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
      message news:ei2AtMN4GH A.1300@TK2MSFTN GP05.phx.gbl...
      Bill,
      >
      This is incorrect. dt is not passed by ref. Rather, DataTable is a
      reference type, meaning that when you assign it, you pass the reference.
      If you were passing the parameter by ref, then you would be able to change
      the reference that you are passing in.
      >
      For example, if the Fill method did something like this:
      >
      public void Fill(DataTable table)
      {
      table = new DataTable();
      }
      >
      When you return, you will see that the parameter passed to table
      doesn't in fact change.
      >
      Hope this helps.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Bill H" <bill@hostunkno wn.comwrote in message
      news:%23m3ZpHN4 GHA.4820@TK2MSF TNGP06.phx.gbl. ..
      >i'm new to C#...
      >>
      >How can I create an out or ref param for a method without having to
      >declare it in the caling code ? Like the DataAdapter can do on its Fill
      >method.
      >>
      >DataTable dt = new DataTable();
      >SqlDataAdapter .Fill(dt);
      >>
      >'dt' is passed by ref, but it is not required to pass it as such:
      >SqlDataAdapter .Fill(ref dt);
      >>
      >>
      >Thanks,
      >Bill
      >>
      >
      >

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: ref params

        Bill,

        No, it was passed by value. The REFERENCE was passed by value. That
        means that the parameter can't be changed, but whatever the parameter points
        to can be changed.

        In VB, it's the same as passing a class without using ByRef. If you
        pass the class and change a property on the class, you will see it outside
        of the method.


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

        "Bill H" <bill@hostunkno wn.comwrote in message
        news:evErhsN4GH A.4164@TK2MSFTN GP05.phx.gbl...
        Thanks for your response Nicholas .
        >
        but I'm still a bit confused...
        >
        When da.Fill returns, my DataTable param var is populated. That implies
        that it was passed by ref, correct ? But, I thought (and read, I think)
        in C#, in order to declare a parameter as ref, you need to declare it as
        such. You are right in your example, when I create a sub of my own,
        similiar to the da.Fill method, the param is not affected on the calling
        side. So why is the DataAdapter Fil method able to affect the DT param,
        and not mine ?
        >
        Thanks for being patient. I'm a veteran VB programmer, so this is a
        humbling experience.
        >
        Bill : )
        >
        >
        >
        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
        in message news:ei2AtMN4GH A.1300@TK2MSFTN GP05.phx.gbl...
        >Bill,
        >>
        > This is incorrect. dt is not passed by ref. Rather, DataTable is a
        >reference type, meaning that when you assign it, you pass the reference.
        >If you were passing the parameter by ref, then you would be able to
        >change the reference that you are passing in.
        >>
        > For example, if the Fill method did something like this:
        >>
        >public void Fill(DataTable table)
        >{
        > table = new DataTable();
        >}
        >>
        > When you return, you will see that the parameter passed to table
        >doesn't in fact change.
        >>
        > Hope this helps.
        >>
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >>
        >"Bill H" <bill@hostunkno wn.comwrote in message
        >news:%23m3ZpHN 4GHA.4820@TK2MS FTNGP06.phx.gbl ...
        >>i'm new to C#...
        >>>
        >>How can I create an out or ref param for a method without having to
        >>declare it in the caling code ? Like the DataAdapter can do on its Fill
        >>method.
        >>>
        >>DataTable dt = new DataTable();
        >>SqlDataAdapte r.Fill(dt);
        >>>
        >>'dt' is passed by ref, but it is not required to pass it as such:
        >>SqlDataAdapte r.Fill(ref dt);
        >>>
        >>>
        >>Thanks,
        >>Bill
        >>>
        >>
        >>
        >
        >

        Comment

        • Noah Sham

          #5
          Re: ref params

          DataTable is a reference type in the CLR. Value types like int or bool need
          to be passed by reference if you intend to modify their value as a
          side-effect of the method invocation. In the case of the DataTable, Fill()
          requires an initialized instance of the DataTable class. Because the
          DataTable is a reference type in the CLR, the Fill() populates the DataTable
          instance by calling on the appropriate DataTable methods. The DataTable
          instance is modified and returned from the method. The following link
          discusses 'ref'


          and this link discusses value types





          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: ref params

            Bill H <bill@hostunkno wn.comwrote:
            Thanks for your response Nicholas .
            >
            but I'm still a bit confused...
            >
            When da.Fill returns, my DataTable param var is populated. That implies
            that it was passed by ref, correct ?
            Nope. It's a reference passed by value.

            See http://www.pobox.com/~skeet/csharp/parameters.html

            --
            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

            • Bill H

              #7
              Re: ref params

              thank you everyone. I'm sorry to have dragged you all into something that I
              probably would have realized sooner or later anyway.

              I now understand where I was going wrong. Your link, Jon, to the Params web
              page, helped me realize my err.
              I've been working with reference and value types forever now, and do truly
              understand the difference and how they work.
              You see, I've never encountered this situation before, because of my own
              work habits. I've always declared my params as Ref if I knew I wanted to
              affect that var. The Da.Fill method threw me off, because it modifies my DT
              without the 'ref' keyword. I realize now that it was manipulating the data
              within the param, as opposed to reassigning the reference pointer. I tend
              to reassign the pointer, and that's what I naturally was trying to do here,
              except without the 'ref' keyword.

              Your input was valuable. I thank you all again.

              Bill




              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:MPG.1f8251 131ecb871698d4c 4@msnews.micros oft.com...
              Bill H <bill@hostunkno wn.comwrote:
              >Thanks for your response Nicholas .
              >>
              >but I'm still a bit confused...
              >>
              >When da.Fill returns, my DataTable param var is populated. That implies
              >that it was passed by ref, correct ?
              >
              Nope. It's a reference passed by value.
              >
              See http://www.pobox.com/~skeet/csharp/parameters.html
              >
              --
              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

              • Jon Skeet [C# MVP]

                #8
                Re: ref params

                Bill H <bill@hostunkno wn.comwrote:
                thank you everyone. I'm sorry to have dragged you all into something that I
                probably would have realized sooner or later anyway.
                >
                I now understand where I was going wrong. Your link, Jon, to the Params web
                page, helped me realize my err.
                I've been working with reference and value types forever now, and do truly
                understand the difference and how they work.
                You see, I've never encountered this situation before, because of my own
                work habits. I've always declared my params as Ref if I knew I wanted to
                affect that var. The Da.Fill method threw me off, because it modifies my DT
                without the 'ref' keyword. I realize now that it was manipulating the data
                within the param, as opposed to reassigning the reference pointer. I tend
                to reassign the pointer, and that's what I naturally was trying to do here,
                except without the 'ref' keyword.
                >
                Your input was valuable. I thank you all again.
                I'm glad the page helped, but I'd urge you to *try* working without
                reference parameters wherever possible. They're sometimes useful, but
                they usually go against the idea of a method doing exactly one thing.
                You'll notice that very few framework methods take out/ref parameters.
                I'm not saying your code doesn't work, or that the larger design is bad
                or wrong, but it's worth at least thinking about how you'd code
                differently if you didn't use out/ref much.

                --
                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...