Pass values b/w forms using properties

R

Ryan

I am trying to pass a value from a texbox in Form1 to a textbox in Form2
using properties in VS2005 but it doesn't work; please help (project is
attached).

Code for Game Class:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyProject
{
class Game
{
private string strName;
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}

private string strMfg;
public string Mfg
{
get
{
return strMfg;
}
set
{
strMfg = value;
}
}


public Game()
{ }

public Game(string strName, string Mfg)
{
this.Name = strName;
this.Mfg = strMfg;
}
}
}



Code for Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Game clssGame = new Game();
clssGame.Name = txtName.Text;
clssGame.Mfg = txtMfg.Text;

Form2 frm = new Form2();
frm.ShowDialog();
}
}
}


Code for Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form2_Load(object sender, EventArgs e)
{
Game clssGame = new Game();
txtName.Text = clssGame.Name;
txtMfg.Text = clssGame.Mfg;
}
}
}
 
R

Ryan

I've provided the code just to show you my approach which isn't working.
I need someone to help me fix my code or provide aother sample code to pass
values between forms using properties driven by a class.
 
M

michel.desangles

Peter, he's a beginner and doesn't understand all the fancy words
you're using, like "mediator", "instance", "values", "code"...

In Form1 :

private void button1_Click(object sender, EventArgs e)
{
Game clssGame = new Game();
clssGame.Name = txtName.Text;
clssGame.Mfg = txtMfg.Text;


Form2 frm = new Form2(clssGame);
frm.ShowDialog();
}

In Form2 :
Remove Form2_Load, then :

public Form2(Game clssGame)
{
InitializeComponent();
txtName.Text = clssGame.Name;
txtMfg.Text = clssGame.Mfg;
}

And as Peter said, it's hard to guess your intent. You're passing a
reference of an object to a client class, that might not be desirable.
You'd probably be better off passing the values, but your question is
ambiguous, as you don't said why you insist on "using properties
driven by a class".

Michel
 
M

Mr. Arnold

Look at the code that has been changed.

Code for Game Class:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyProject
{
class Game
{
private string strName;
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}

private string strMfg;
public string Mfg
{
get
{
return strMfg;
}
set
{
strMfg = value;
}
}


public Game()
{ }

public Game(string strName, string Mfg)
{
this.Name = strName;
this.Mfg = strMfg;
}
}
}



Code for Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form1 : Form
{
private Game clssGame; '>>>>> declare it here
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
clssGame = new Game(); '>>>>>>>> set it NEW here
clssGame.Name = txtName.Text;
clssGame.Mfg = txtMfg.Text;

Form2 frm = new Form2(clssGame); ''>>>>>>>>>> pass it in the
form's constructor
frm.ShowDialog();
}
}
}


Code for Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form2 : Form
{
private Game clssgame; '>>>>>>> declare it here
public Form2(Object passclssGame) '>>>>>> passclssGame is just an
Object in the form being declared
{
clssgame = (Game)passclssGame; '>>>>>>> CAST passclssGame to
the Game class
 
R

Ryan

Thanks to all of you.

Mr. Arnold said:
Look at the code that has been changed.


private Game clssGame; '>>>>> declare it here


private Game clssgame; '>>>>>>> declare it here

clssgame = (Game)passclssGame; '>>>>>>> CAST passclssGame to
the Game class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top