none
passing object from form1.cs to form2.cs? RRS feed

  • Question

  • hi

    thanks for the help. here is my question.

    in form1 I made an object  --> cars

    then I open form2  from a button on form1 using the showdialog method


    in form2 all I want todo is set the properties of cars but I see that they are not there.

    do have have any easy to understand example of how to do this?

    Thanks




    • Moved by OmegaMan Friday, March 5, 2010 2:38 AM (From:Visual C# General)
    Tuesday, March 2, 2010 6:44 PM

Answers

  • Take the Cars object as a parameter by reference to the constructor of Form2:
    In Form1

    Form2 myForm2 = new Form2(ref car);
    myForm2.ShowDialog();

    Form2:

    public Form2(ref Object car)
    {
         car.wheels = 4;
         car.fuel = "expensive";
         etc.....
    }

    This will update the car instance properties and persist when you get back to Form1
    Bob - www.crowcoder.com
    • Marked as answer by Rudedog2 Wednesday, March 3, 2010 6:01 PM
    Tuesday, March 2, 2010 7:19 PM
  • Take the Cars object as a parameter by reference to the constructor of Form2:
    In Form1

    Form2 myForm2 = new Form2(ref car);
    myForm2.ShowDialog();

    Form2:

    public Form2(ref Object car)
    {
     
    there is no  need  for the ref keyword  because  all  objects  in  c#  are  a reference  type ....!
    A man's dreams are an index to his greatness
    Classes are reference types while structs are value types. However, what Molku was referring to was passing by ref which means it affects the original, here is an example you can evaluate:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Int32 number = 1;
    
                //pass by ref, it affects the original
                PassByRef(ref number);
                Console.WriteLine(number);
    
                //Doesn't affect the original
                PassByValue(number);
                Console.WriteLine(number);
    
                Console.ReadLine();
            }
    
            static void PassByRef(ref Int32 number)
            {
                number++;
            }
    
            static void PassByValue(Int32 number)
            {
                number++;
            }
        }
    }
    


    That being said, it is not the "best" wa y to handle this. The proper way to handle this is by understanding the difference between parent to child, sibling to sibling relationships between forms.

    See Passing Values Between Forms
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Rudedog2 Wednesday, March 3, 2010 6:00 PM
    Wednesday, March 3, 2010 2:26 PM

  • http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

    Passing value types by reference, as demonstrated earlier in this topic, is useful, but ref is also useful for passing reference types. This allows called methods to modify the object to which the reference refers because the reference itself is being passed by reference. The following sample shows that when a reference type is passed as a ref parameter, the object itself can be changed. For more information, see Passing Reference-Type Parameters (C# Programming Guide).
    Bob - www.crowcoder.com
    • Marked as answer by Rudedog2 Wednesday, March 3, 2010 6:00 PM
    Wednesday, March 3, 2010 5:52 PM

All replies

  • Check this recent thread.

    http://social.msdn.microsoft.com/Forums/en-ZA/winforms/thread/e8f9745c-5e24-46dc-a939-cc486e71e21e

    For questions related to WinForms and any WinForm control, including DataGridView use the Windows Forms General or Windows Forms Data Controls and Databinding forums.

    You should more, better, and faster answers on the other forum.

    Rudy  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by JohnGrove Tuesday, March 2, 2010 8:54 PM
    Tuesday, March 2, 2010 6:50 PM
  • Take the Cars object as a parameter by reference to the constructor of Form2:
    In Form1

    Form2 myForm2 = new Form2(ref car);
    myForm2.ShowDialog();

    Form2:

    public Form2(ref Object car)
    {
         car.wheels = 4;
         car.fuel = "expensive";
         etc.....
    }

    This will update the car instance properties and persist when you get back to Form1
    Bob - www.crowcoder.com
    • Marked as answer by Rudedog2 Wednesday, March 3, 2010 6:01 PM
    Tuesday, March 2, 2010 7:19 PM
  • hi

    thanks for the help. here is my question.

    in form1 I made an object  --> cars

    then I open form2  from a button on form1 using the showdialog method


    in form2 all I want todo is set the properties of cars but I see that they are not there.

    do have have any easy to understand example of how to do this?

    Thanks





    We just answered a similar question here

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, March 2, 2010 11:02 PM
  • hello santa,

    Do you have any good news for us?

    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 3, 2010 1:48 PM
  • Take the Cars object as a parameter by reference to the constructor of Form2:
    In Form1

    Form2 myForm2 = new Form2(ref car);
    myForm2.ShowDialog();

    Form2:

    public Form2(ref Object car)
    {
     
    there is no  need  for the ref keyword  because  all  objects  in  c#  are  a reference  type ....!
    A man's dreams are an index to his greatness
    Wednesday, March 3, 2010 1:55 PM
  • Take the Cars object as a parameter by reference to the constructor of Form2:
    In Form1

    Form2 myForm2 = new Form2(ref car);
    myForm2.ShowDialog();

    Form2:

    public Form2(ref Object car)
    {
     
    there is no  need  for the ref keyword  because  all  objects  in  c#  are  a reference  type ....!
    A man's dreams are an index to his greatness
    Classes are reference types while structs are value types. However, what Molku was referring to was passing by ref which means it affects the original, here is an example you can evaluate:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Int32 number = 1;
    
                //pass by ref, it affects the original
                PassByRef(ref number);
                Console.WriteLine(number);
    
                //Doesn't affect the original
                PassByValue(number);
                Console.WriteLine(number);
    
                Console.ReadLine();
            }
    
            static void PassByRef(ref Int32 number)
            {
                number++;
            }
    
            static void PassByValue(Int32 number)
            {
                number++;
            }
        }
    }
    


    That being said, it is not the "best" wa y to handle this. The proper way to handle this is by understanding the difference between parent to child, sibling to sibling relationships between forms.

    See Passing Values Between Forms
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Rudedog2 Wednesday, March 3, 2010 6:00 PM
    Wednesday, March 3, 2010 2:26 PM
  • hi

        base on requirement you can create multiple overload in you another form  & pass require value in next from  or you can create public method & pass value in that method also 

    thanks

    Amit kundalwal

    Wednesday, March 3, 2010 2:41 PM
  • Here's some food for thought.  Stuff that makes you say "hmmm".  Methods 3 and 4.


        class Program
        {
            static void Main(string[] args)
            {
                TestClass testObject;
                //
                testObject = new TestClass("Default");
                Console.WriteLine(testObject.Data);
                Console.WriteLine((Method_01(ref testObject)).Data);
                Console.WriteLine(testObject.Data);
                Console.ReadLine();
                //
                testObject = new TestClass("Default");
                Console.WriteLine(testObject.Data);
                Console.WriteLine((Method_02(testObject)).Data);
                Console.WriteLine(testObject.Data);
                Console.ReadLine();
                //
                testObject = new TestClass("Default");
                Console.WriteLine(testObject.Data);
                Console.WriteLine((Method_03(ref testObject)).Data);
                Console.WriteLine(testObject.Data);
                Console.ReadLine();
                //
                testObject = new TestClass("Default");
                Console.WriteLine(testObject.Data);
                Console.WriteLine((Method_04(testObject)).Data);
                Console.WriteLine(testObject.Data);
                Console.ReadLine();
            }

            static TestClass Method_01(ref TestClass obj)
            {
                TestClass objNew = obj;
                objNew.Data = "Modified";
                return objNew;
            }
            static TestClass Method_02(TestClass obj)
            {
                TestClass objNew = obj;
                objNew.Data = "Modified";
                return objNew;
            }
            static TestClass Method_03(ref TestClass obj)
            {
                obj = new TestClass("Modified");
                return obj;
            }
            static TestClass Method_04(TestClass obj)
            {
                obj = new TestClass("Modified");
                return obj;
            }
        }

    /************************************************/


        class TestClass
        {
            public string Data
            {
                get;
                set;
            }
            public TestClass(string data)
            {
                this.Data = data;
            }
        }

    /************************************************

    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 3, 2010 2:53 PM
  • hi

        base on requirement you can create multiple overload in you another form  & pass require value in next from  or you can create public method & pass value in that method also 

    thanks

    Amit kundalwal


    Good advice!

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, March 3, 2010 5:34 PM

  • http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

    Passing value types by reference, as demonstrated earlier in this topic, is useful, but ref is also useful for passing reference types. This allows called methods to modify the object to which the reference refers because the reference itself is being passed by reference. The following sample shows that when a reference type is passed as a ref parameter, the object itself can be changed. For more information, see Passing Reference-Type Parameters (C# Programming Guide).
    Bob - www.crowcoder.com
    • Marked as answer by Rudedog2 Wednesday, March 3, 2010 6:00 PM
    Wednesday, March 3, 2010 5:52 PM
  • I'll mark your posts as the answer for your links and explanation.
    I also think your solution is the best match for the OP's apparent level of skill.

    I had already posted a sample that demonstrates these subtle differences.

    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 3, 2010 6:00 PM
  • Classes are reference types while structs are value types. However, what Molku was referring to was passing by ref which means it affects the original, here is an example you can evaluate:
    what  I   mean here if the  car  it's an  object and  not  a struct  he can do  something  like  this  
    public  class Class1
        {
           public string Prop1
           {
               get;
               set; 
           }
        }
    //form2 
    public partial class Form2 : Form
        {
            public Form2( Class1 clas)
            {
                InitializeComponent();
                clas.Prop1 = "3";   
            }
        }
    //form1 
     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Class1 clas1  =  new Class1()  ;    
                clas1.Prop1= "12";  
                using (Form2 frm  =  new Form2(clas1))
                {
                    frm.ShowDialog();   
    
                }
                MessageBox.Show(clas1.Prop1);  
            }
    

    A man's dreams are an index to his greatness
    Thursday, March 4, 2010 7:44 AM
  • Take the Cars object as a parameter by reference to the constructor of Form2:
    In Form1

    Form2 myForm2 = new Form2(ref car);
    myForm2.ShowDialog();

    Form2:

    public Form2(ref Object car)
    {
     
    there is no  need  for the ref keyword  because  all  objects  in  c#  are  a reference  type . ...!
    A man's dreams are an index to his greatness

    Since no one else has disagreed with you I will.  Your assertion is outright false.  .NET defines Value Types, too.  And I agree with the point of view that says the ref keyword should be used in this case.  The sample code that you just posted is based upon a flawed assumption.  The snippet that I posted exposes the flaw, why using 'ref' makes a difference.

    Happy Coding.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, March 4, 2010 1:20 PM

  • Since no one else has disagreed with you I will.  Your assertion is outright false.  .NET defines Value Types, too.  And I agree with the point of view that says the ref keyword should be used in this case.  The sample code that you just posted is based upon a flawed assumption.  The snippet that I posted exposes the flaw, why using 'ref' makes a difference.

    Happy Coding

    my assertion it's not outright false . because here molku don't  need to change the value of the reference itself . I know that  the 
    behaviour method4 it's not expected  by  a beginner programmer  but  as c#  specifiction 

     "However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block"

    So please  the next time try to omit a bad words from the discussion 
    A man's dreams are an index to his greatness
    Friday, March 5, 2010 8:05 AM
  • < all  objects  in  c#  are  a reference  type . ...! >

    Now I see why no one challenged you on the above false assertion.

    I'm not here to argue, but that statement is as false as they come. 
    It is far off base that it is totally indefensible. 
    I guess that you have never heard of Value Types.
    Besides, C# does really not define anything.  The .NET FCL defines the types.

    Your citing of the C# specification is being used out of context.  You should at least cite the section, $3.2.1.
    According to your use of the specification, then the my Method_03 shouldn't work the way it does.
    The quote explains the behavior seen in Method_04, and does not apply to the 'ref' keyword.
    The 'ref' keyword, the whole point of the discussion.

    Rudy  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Edited by Rudedog2 Monday, March 8, 2010 4:24 PM
    Friday, March 5, 2010 1:39 PM
  • Relax boys.......go easy....
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, March 5, 2010 5:42 PM
  • to  rudedog2 "Warrior"  would  like to explain  why " you cannot use the same reference to allocate memory for a new class and have it persist outside the block"  if  you really  understand  what's  gone under the hood ...?


    A man's dreams are an index to his greatness
    Monday, March 8, 2010 7:57 AM
  • I am not here to argue. 
    Sorry if you feel I offended you with clarification of Reference Types and Value Types.
    Yours is not an uncommon reaction to the name of my avatar.

    When you quote the C# Specification, you should at least cite the section that you are quoting; i.e. $3.2.1.
    Without a contextual reference, your ideas and opinions may carry less significance.

    Method_03 uses "the same reference to allocate memory to a new class instance, and it persists outside of the block. "

    Happy Coding.

    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    • Edited by Rudedog2 Monday, March 8, 2010 5:09 PM
    Monday, March 8, 2010 4:35 PM
  • Don't let the name fool you, "Rudedog" is a misnomer. Rudy is actually one of the most patient, kindest answerer in these forums.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Monday, March 8, 2010 4:49 PM
  • Hi rudedog2 ,
    me too I'm not here to argue but we are  here to learn  from  each others  and from  our mistakes. So as response to my question 
    "why  you cannot use the same reference to allocate memory for a new class and have it persist outside the block" ..?  
    the answer is quite easy as if we say "clear as mud " the fact that we cannot do this reside in the  memory  management of the .net  and how  the heap  and stack  are  allocated  here  is a link  to  understand more what's  gone under the hood :) .
    A man's dreams are an index to his greatness
    Tuesday, March 9, 2010 4:10 PM
  • Well, in that case, my posted Method_03 has pulled the "ole switcharoo" on your referenced link.  My method instantiates a new object, that survives outside of the method block where it was instantiated, which is contrary to your claim as I understand it.  Run the code and see for yourself.

    Seriously, your facts are correct, but taken just a bit out of context and misapplied.  There is a subtle difference at work here.  Your example at link declares the variable inside of the method.  My example declares the variable outside of the method.  Both examples instantiate an new instance inside of a method.

    Please start a new thread to continue this discussion in the C# General Forum

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, March 9, 2010 4:39 PM
  • Hi  Rudedog2 , 
    ok  let's  forget who  has  right  and  who has outright . 
    "There is a subtle difference at work here.  Your example at link declares the variable inside of the method.  My example declares the variable outside of the method"
    the aim of link given above  it's to keep in mind  that the base of this  kind of beahaviour it's basically the heap and stack allocation and upon to you to conclude why  ...?   
    I can't find  a  link  that  can  explain   how  it's  work  but  I  will  do  later if you want  ..   :)


    A man's dreams are an index to his greatness
    Thursday, March 11, 2010 11:08 AM