Jump to content
  • Advertisement
Sign in to follow this  
the-beast

[.net] New to C#, need to pass data to function in another class

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, first off I'm very new to C# and I need to ask a very basic question (More of a general query that a games related one). I have a class called Form1() that contains the main function and a function that writes to a rich edit box (richTextBox2). This class creates an instances of another class (MySQL()) and then calls a function in that class. This other function has a loop in it that needs to write to the richTextBox2 control (through a function) in the original class. I hope your still with me. Here is some example code so that you can see what i am doing.
namespace Chris_SQL
{
	public class Form1 : System.Windows.Forms.Form
	{
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button1;

		public Form1()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		//Create a new instance of the MySQL class
		MySQL SQLconnection = new MySQL();

		private void button1_Click(object sender, System.EventArgs e)
		{
			SQLconnection.MySQLRead(null);
		}

		public void  SQLOutput(String text)
		{
			richTextBox2.Text = text;
		}
}

	public class MySQL
	{
		static private MySQLConnection con;

		public MySQL()
		{}

		public void MySQLRead2()
		{
			try
			{
				MySQLConnection con;
				con = new MySQLConnection( new MySQLConnectionString("localhost",
					"database",
					"user",
					"pass").AsString );
				con.Open();

				string sql = "SELECT * FROM colour";

				MySQLCommand cmd = new MySQLCommand(sql,con);  
  
				MySQLDataReader reader = cmd.ExecuteReaderEx();

				while(reader.Read())
				{
					//This needs to talk to the SQLOutput() function from the Form1() class
					//	Chris_SQL.Form1.SQLOutput(reader[0].ToString());
				}

				reader.Close();        
				con.Close();
			}
			catch(Exception ee)
			{
				MessageBox.Show( ee.ToString() );
			}
		}
}
}
So to put it simple, how do I get the MySQLRead2() to be able to talk to the SQLOutput() from the calling class? Thanks for any help you can shed on this.

Share this post


Link to post
Share on other sites
Advertisement
There are a few ways to accomplish this. The simplest was is to have a Form1 parameter in the constructor of MySQL and just pass "this" to it when you create the SQLconnection object. You then have access to all the public members of Form1.

A more elegant way, though, is to use events. First, create the following class and delegate:

public class MyCustomEventArgs : System.EventArgs
{
public string OutputText = "";

public MyCustomEventArgs(string outputText)
{
OutputText = outputText;
}
}

public delegate void MyCustomEventHandler(object sender, MyCustomEventArgs e);




Next, add the following public member to your MySQL class and call it in the appropriate place:

public event MyCustomEventHandler OnOutputReady;

//Later inside your reader loop
OnOutputReady(this, new MyCustomEventArgs( reader[0].ToString() ) );




Finally, attach to the event in Form1

//After the SQLconnection object is created
SQLconnection.OnOutputReady += new MyCustomEventHandler(SQLconnection_OnOutputReady);

//The SQLconnection_OnOutputReady will replace the SQLOutput function
private SQLconnection_OnOutputReady(object sender, MyCustomEventArgs e)
{
//Append the output text to the rich text box
richTextBox1.AppendText(e.OutputText);
}




A few things to note:
1. This code is not threadsafe. Look into calling richTextBox1.Invoke to properly append text in the right thread.

2. You don't need to recreate your MySQLConnection object for each read. Create it in the constructor of the MySQL object and call upon it in your read functions.

3. No need to use reader[0].ToString(). Just call reader.GetString(0).

Hope all this helps; let me know if you have any more questions.

[edit] Fixed some code [/edit]

[Edited by - DaWanderer on May 22, 2005 4:12:23 PM]

Share this post


Link to post
Share on other sites
I think I missing something obvious here, but I get this error:


Invalid token '+=' in class, struct, or interface member declaration


on this line:


SQLconnection.OnOutputReady += new MyCustomEventHandler(SQLconnection_OnOutputReady);





Any idea whats wrong?

Thanks.


This is my code


namespace Chris_SQL
{
public class MyCustomEventArgs : System.EventArgs
{
public string OutputText = "";

public MyCustomEventArgs(string outputText)
{
OutputText = outputText;
}
}

public delegate void MyCustomEventHandler(object sender, MyCustomEventArgs e);

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button1;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

//Create a new instance of the MySQL class
MySQL SQLconnection = new MySQL();

//After the SQLconnection object is created
SQLconnection.OnOutputReady += new MyCustomEventHandler(SQLconnection_OnOutputReady);

//The SQLconnection_OnOutputReady will replace the SQLOutput function
private SQLconnection_OnOutputReady(object sender, MyCustomEventArgs e)
{
//Append the output text to the rich text box
richTextBox1.AppendText(e.OutputText);
}

private void button1_Click(object sender, System.EventArgs e)
{
SQLconnection.MySQLRead(null);
}

/*public void SQLOutput(String text)
{
richTextBox2.Text = text;
}*/

}

public class MySQL
{
static private MySQLConnection con;

public event MyCustomEventHandler OnOutputReady;

public MySQL()
{}

public void MySQLRead2()
{
try
{
//This connection will be moved later
MySQLConnection con;
con = new MySQLConnection( new MySQLConnectionString("localhost",
"database",
"user",
"pass").AsString );
con.Open();

string sql = "SELECT * FROM colour";

MySQLCommand cmd = new MySQLCommand(sql,con);

MySQLDataReader reader = cmd.ExecuteReaderEx();

while(reader.Read())
{
OnOutputReady( new MyCustomEventArgs( reader[0].ToString() ) );


}

reader.Close();
con.Close();
}
catch(Exception ee)
{
MessageBox.Show( ee.ToString() );
}
}
}
}





[Edited by - the-beast on May 22, 2005 8:25:56 AM]

Share this post


Link to post
Share on other sites
You have to do the "SQLconnection.OnOutputReady += ..." inside of a function. Just do it inside your form's constructor like this:

public Form1()
{
SQLconnection.OnOutputReady += new MyCustomEventHandler(SQLconnection_OnOutputReady);
}

Share this post


Link to post
Share on other sites
Thanks for the help, works like a charm now.

Oh and if anyone else is interested in the code I had to change this line


OnOutputReady( new MyCustomEventArgs( reader[0].ToString() ) );



to


OnOutputReady( this, new MyCustomEventArgs( reader[0].ToString() ) );




to get the code to compile.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!