Picturebox

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

    #1

    Picturebox

    What's picturebox?

    Reading MSDN, there is a function call defined as:

    FunctionName(re f count)

    What does "ref" here indicate?


  • Alvin Bruney

    #2
    Re: Picturebox

    ref is passing by reference


    "cs" <cs@adv.edu> wrote in message
    news:eVOZ6iftDH A.3496@TK2MSFTN GP11.phx.gbl...[color=blue]
    > What's picturebox?
    >
    > Reading MSDN, there is a function call defined as:
    >
    > FunctionName(re f count)
    >
    > What does "ref" here indicate?
    >
    >[/color]


    Comment

    • Frank Oquendo

      #3
      Re: Picturebox

      cs wrote:
      [color=blue]
      > FunctionName(re f count)
      >
      > What does "ref" here indicate?[/color]

      The ref keyword indicates that you need to your parameter By Reference
      instead of By Value. In the case of reference types, the difference is
      academic.

      However, with value types such as structs and primitives, it's possible
      that the method you're calling will change the value of the parameter.
      If so, that change will be reflected in the calling method.

      --
      There are 10 kinds of people. Those who understand binary and those who
      don't.


      (Pull the pin to reply)


      Comment

      • Jeff Louie

        #4
        Re: Picturebox

        Frank... Hmm. If you write a swap routine, by ref works and by value
        does not. So I am not sure the difference is just academic.

        Regards,
        Jeff[color=blue]
        >The ref keyword indicates that you need to your parameter By[/color]
        Reference instead of By Value. In the case of reference types, the
        difference is academic.<


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Frank Oquendo

          #5
          Re: Picturebox

          Jeff Louie wrote:[color=blue]
          > Frank... Hmm. If you write a swap routine, by ref works and by value
          > does not. So I am not sure the difference is just academic.[/color]

          You're going to have to provide an example as I do not understand what
          you are referring to. When passing a reference type ByVal, only the
          handle to the object is duplicated and passed. You're still manipulating
          the original object. That sounds pretty academic to me.

          ByRef and ByVal only have meaningful semantics when passing value types.

          --
          There are 10 kinds of people. Those who understand binary and those who
          don't.


          (Pull the pin to reply)


          Comment

          • Jeff Louie

            #6
            Re: Picturebox

            Frank... This is pretty obscure behaviour so don't sweat it.

            using System;

            namespace TestSwap
            {
            abstract class Drawable
            {
            abstract public void DrawMe();
            }
            class Circle : Drawable
            {
            public override void DrawMe()
            {
            System.Console. WriteLine("Circ le");
            }
            }
            class Square : Drawable
            {
            public override void DrawMe()
            {
            System.Console. WriteLine("Squa re");
            }
            }
            /// <summary>
            /// Summary description for Class1.
            /// </summary>
            class Class1
            {
            static public void SwapByValue(Dra wable d1, Drawable d2)
            {
            Drawable temp= d1;
            d1 = d2;
            d2= temp;
            }
            static public void SwapByRef(ref Drawable d1, ref Drawable d2)
            {
            Drawable temp= d1;
            d1 = d2;
            d2= temp;
            }
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
            //
            // TODO: Add code to start application here
            //
            Drawable wasCircle= new Circle();
            Drawable wasSquare= new Square();
            wasCircle.DrawM e(); // Circle
            wasSquare.DrawM e(); // Square
            SwapByValue(was Circle, wasSquare); // fails
            wasCircle.DrawM e(); // Circle
            wasSquare.DrawM e(); // Square
            SwapByRef(ref wasCircle, ref wasSquare); // succeeds
            wasCircle.DrawM e(); // Square
            wasSquare.DrawM e(); // Circle
            System.Console. ReadLine();
            }
            }
            }


            Regards,
            Jeff[color=blue]
            >You're going to have to provide an example as I do not understand what[/color]
            you are referring to.<

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Picturebox

              Frank Oquendo <frankopin@acad x.com> wrote:[color=blue]
              > Jeff Louie wrote:[color=green]
              > > Frank... Hmm. If you write a swap routine, by ref works and by value
              > > does not. So I am not sure the difference is just academic.[/color]
              >
              > You're going to have to provide an example as I do not understand what
              > you are referring to. When passing a reference type ByVal, only the
              > handle to the object is duplicated and passed. You're still manipulating
              > the original object. That sounds pretty academic to me.
              >
              > ByRef and ByVal only have meaningful semantics when passing value types.[/color]

              Not true at all. Simple example:

              Foo f = new Foo();
              SomeMethod (ref f);

              ...

              void SomeMethod (ref Foo x)
              {
              x = null;
              }

              If the parameter were not passed by reference, there would be no change
              to the value of f. As it is, f is changed to null.

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

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

              • Frank Oquendo

                #8
                Re: Picturebox

                Jon Skeet [C# MVP] wrote:
                [color=blue]
                > Not true at all. Simple example:[/color]

                A conveniently simple example, at that. What you've demonstrated is that
                you can chang what the copy of an object handle points to without
                effecting the original object. However, that behavior changes as soon as
                you manipulate an object's properties, quite in contradiction to what
                quite a few people would expect. Here's an example.

                using System;

                namespace ConsoleApplicat ion6
                {
                class Class1
                {
                [STAThread]
                static void Main(string[] args)
                {
                Foo foo = new Foo();
                foo.Text = "Old text";
                Bar(foo);
                Console.WriteLi ne(foo.Text);
                Console.ReadLin e();
                }

                public class Foo
                {
                private string _text;

                public string Text
                {
                get { return _text; }
                set { _text = value; }
                }
                }

                static void Bar(Foo foo)
                {
                foo.Text = "New Text";
                }
                }
                }

                --
                There are 10 kinds of people. Those who understand binary and those who
                don't.


                (Pull the pin to reply)


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Picturebox

                  Frank Oquendo <frankopin@acad x.com> wrote:[color=blue]
                  > Jon Skeet [C# MVP] wrote:
                  >[color=green]
                  > > Not true at all. Simple example:[/color]
                  >
                  > A conveniently simple example, at that. What you've demonstrated is that
                  > you can chang what the copy of an object handle points to without
                  > effecting the original object.[/color]

                  It doesn't change the "object handle" at all - it changes what value
                  the variable has.
                  [color=blue]
                  > However, that behavior changes as soon as
                  > you manipulate an object's properties, quite in contradiction to what
                  > quite a few people would expect.[/color]

                  Well, only if those people hadn't actually bothered to find out what
                  reference types are...

                  The point, however, is that passing a reference variable *by reference*
                  does something different to passing it by value, and the difference is
                  far from academic.

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