-1

I'm having a problem when trying to pass object from one Form to another.

object y = new object(textBox1.Text, textBox2.Text);
                Form2 newWin = new Form2();
                newWin.Show();

I just want to send the object y to form2 from form 1

EDIT:

this is what i've write in form 1:

Preformer y = new Preformer(artName.Text, hitSong.Text);
                preformerForm newPreformerWin = new preformerForm(y);
                newPreformerWin.Show();

and this is what i've write in form2 (this called preformerForm):

    public partial class preformerForm : Form
    {
        Preformer z;
    
        public preformerForm()
        {
            InitializeComponent();
            
        }
        preformerForm(Preformer y)
        {
            this.z = y;

        } 
   }

I just want to use the object 'y' in Form 2 and it's not working. I have a error in form 1 :

Gilad Levy
  • 19
  • 2
  • Just make a parameter in your Form2 constructor. `public Form2(object passedObject)` There are better options than `object`. – LarsTech May 16 '22 at 18:27
  • Yes I've did it and i doesn't work. I have error : CS0051. – Gilad Levy May 16 '22 at 18:34
  • “Doesn’t work” is a poor description. Do you get an error? The object is not passed? … What specifically doesn’t work? Can you show the code that “doesn’t work?” – JohnG May 16 '22 at 18:41
  • this is Form1: Preformer y = new Preformer(artName.Text, hitSong.Text); preformerForm newPreformerWin = new preformerForm(y); newPreformerWin.Show(); – Gilad Levy May 16 '22 at 18:49
  • this is Form2(newPreformerWin) : Preformer z; public preformerForm() { InitializeComponent(); } preformerForm(Preformer y) { this.z = y; } – Gilad Levy May 16 '22 at 18:50
  • CS0122 this is my error. – Gilad Levy May 16 '22 at 18:51
  • Please [edit] your question and add the additional code to your question and not as a comment. The error indicates a possible wrong type being passed and or a public/private type variable. I am guessing a type mismatch but there isn’t enough info to be sure. – JohnG May 16 '22 at 19:03
  • Just did that. Thank you very much! – Gilad Levy May 16 '22 at 19:09
  • You still need the `InitializeComponent();` in the new constructor. That's where all your form information is being called to put the form together. – LarsTech May 16 '22 at 19:16
  • Just put that func and it's still the same error. – Gilad Levy May 16 '22 at 19:20
  • The constructor needs to be `public`... Like... `public preformerForm(Preformer y) { ...` – JohnG May 16 '22 at 19:23
  • Make sure it's `public preformerForm(Preformer y)` Which means your `Performer` class needs to be public, too. – LarsTech May 16 '22 at 19:24
  • Yes the Preformer class is already public. When i try to put public in the new consructor i have CS0051 error. – Gilad Levy May 16 '22 at 19:27
  • The error indicates that the `Preformer` class in not `public`… you should add/show this class to your question… not as a comment. – JohnG May 16 '22 at 19:31
  • It's work now. Thank you very much, the class was internal before automaticlly, what this type mean? How does it effect? – Gilad Levy May 16 '22 at 19:39

0 Answers0