Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ref typecast between form derived from Form class and Form class

ref typecast between form derived from Form class and Form class

Scheduled Pinned Locked Moved C#
questionhelpcareer
7 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karismatic
    wrote on last edited by
    #1

    Hi, I have a function like foo(ref System.Windows.Form objForm) and I need to pass a custom form which is derived from System.Windows.Form that is Custom form :- FormX : System.Windows.Form So, my question is how can I call function foo with object of FormX as ref argument that is something like FormX obj; foo (ref obj); // it is giving compilation error Please let me know the solution

    Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job

    D T 2 Replies Last reply
    0
    • K Karismatic

      Hi, I have a function like foo(ref System.Windows.Form objForm) and I need to pass a custom form which is derived from System.Windows.Form that is Custom form :- FormX : System.Windows.Form So, my question is how can I call function foo with object of FormX as ref argument that is something like FormX obj; foo (ref obj); // it is giving compilation error Please let me know the solution

      Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      You haven't stated the error but I guess it's because you haven't assigned obj to either null or a FormX instance. When passing by reference the object is passed into the function so it must exist (or be assignd null if a reference type). If you want the function to pass out the FormX instance and not provide it to the method yourself, you should use out instead of ref. By the way, unless you've messed with the System.Windows namespace, it should be System.Windows.Forms.Form.

      Dave
      Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      1 Reply Last reply
      0
      • K Karismatic

        Hi, I have a function like foo(ref System.Windows.Form objForm) and I need to pass a custom form which is derived from System.Windows.Form that is Custom form :- FormX : System.Windows.Form So, my question is how can I call function foo with object of FormX as ref argument that is something like FormX obj; foo (ref obj); // it is giving compilation error Please let me know the solution

        Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job

        T Offline
        T Offline
        Tim Weckx
        wrote on last edited by
        #3

        Pass-by-reference parameters must match type exactly. The workaround is to do:

        FormX objX = new FormX();

        // cast down
        Form obj = objX;

        // now you can call the foo method
        foo(ref obj);

        However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.

        foo(System.Windows.Forms.Form obj)
        {
        // obj is passed as a reference by default since it's a reference type
        }

        I K 2 Replies Last reply
        0
        • T Tim Weckx

          Pass-by-reference parameters must match type exactly. The workaround is to do:

          FormX objX = new FormX();

          // cast down
          Form obj = objX;

          // now you can call the foo method
          foo(ref obj);

          However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.

          foo(System.Windows.Forms.Form obj)
          {
          // obj is passed as a reference by default since it's a reference type
          }

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          Member 2463256 wrote:

          However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.

          Not quite the same thing. The difference is this:

          foo(ref Form obj)
          {
          // The caller's variable is changed to a new
          // Form instance.
          obj = new Form();
          }

          foo(Form obj)
          {
          // Our LOCAL variable 'obj' is set to a new form, but
          // the calling function will see no change.
          obj = new Form();
          }

          The "ref" keyword is similar to the "out" keyword, except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed. Of course, in either case, using "ref" or not, changing a property of 'obj' will affect the caller's instance.

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          A 1 Reply Last reply
          0
          • I Ian Shlasko

            Member 2463256 wrote:

            However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.

            Not quite the same thing. The difference is this:

            foo(ref Form obj)
            {
            // The caller's variable is changed to a new
            // Form instance.
            obj = new Form();
            }

            foo(Form obj)
            {
            // Our LOCAL variable 'obj' is set to a new form, but
            // the calling function will see no change.
            obj = new Form();
            }

            The "ref" keyword is similar to the "out" keyword, except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed. Of course, in either case, using "ref" or not, changing a property of 'obj' will affect the caller's instance.

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #5

            Ian Shlasko wrote:

            except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed.

            Not quite. The only difference between ref and out is that out ensures the value will be set before you return from the method. When ref is used, it does not ensure the variable will be set... it merely allows it. This allows for one to do something like this:

            // Notice myForm was not initialized.
            Form myForm;
            SomeMethod(out myForm);
            // This will work.
            myForm.ShowDialog();

            Notice how this differs from ref:

            // Notice myForm was not initialized.
            Form myForm;
            SomeMethod(ref myForm);
            // This will NOT work (compile time error), because myForm may not have been initialized.
            myForm.ShowDialog();

            [Forum Guidelines]

            I 1 Reply Last reply
            0
            • A AspDotNetDev

              Ian Shlasko wrote:

              except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed.

              Not quite. The only difference between ref and out is that out ensures the value will be set before you return from the method. When ref is used, it does not ensure the variable will be set... it merely allows it. This allows for one to do something like this:

              // Notice myForm was not initialized.
              Form myForm;
              SomeMethod(out myForm);
              // This will work.
              myForm.ShowDialog();

              Notice how this differs from ref:

              // Notice myForm was not initialized.
              Form myForm;
              SomeMethod(ref myForm);
              // This will NOT work (compile time error), because myForm may not have been initialized.
              myForm.ShowDialog();

              [Forum Guidelines]

              I Offline
              I Offline
              Ian Shlasko
              wrote on last edited by
              #6

              Well, your example illustrates the inverse, but still, I stand corrected. Forgot about the requirement that "out" variables are set. But basically... Normal: Values in, unmodified (Aside from setters/methods of object types) Ref: Values in, possibly different values out Out: Nothing in, values out

              Proud to have finally moved to the A-Ark. Which one are you in?
              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

              1 Reply Last reply
              0
              • T Tim Weckx

                Pass-by-reference parameters must match type exactly. The workaround is to do:

                FormX objX = new FormX();

                // cast down
                Form obj = objX;

                // now you can call the foo method
                foo(ref obj);

                However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.

                foo(System.Windows.Forms.Form obj)
                {
                // obj is passed as a reference by default since it's a reference type
                }

                K Offline
                K Offline
                Karismatic
                wrote on last edited by
                #7

                Thank you everybody but in my case Member 2463256 reply was useful. Problem resolved!

                Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups