Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# parameter passing help

Status
Not open for further replies.

ukemike

Programmer
Aug 14, 2003
8
US
Hi i got a question for you guys.

I am trying to write a program in C# that communicates with a database. I have:

1) main_prog class
2) Object_reader class

main_prog is the main program, and object_reader is a class which has all the function to communicate with the database(collect_to, close, etc..)

My main form has a bunch of buttons. One of the buttons is a "load_record".
On the click of that button i instanciate a new object_reader object, open the connection and load the first record. Here is where my question is:

After the use clicks that button i want to keep the connection open and just pass the instance of that object to another button's click event. This way I dont have to open a new connection, do whatever, and close the connection for every button clicked.

Do you guys understand my question. Here is another example:

class my_class
{
bla bla blah
}

class main_prog
{

button1_click(.....)
{
my_class thisclass = new my_class
}
button2_click(i want to pass thisclass to here)

}


ok guys. any help is greatly appreciated.. thanx a lot



 
You'll need a copy of your connection object stored in a private variable in your main_prog class. Don't instantiate it - it's just a place to keep a reference to it.
Code:
class main_prog
{
   private SqlConnection m_MyConnection;

   main_prog
   {
       // initialize it in your constructor
       m_MyConnection = null;
   }

   button1_click(e,,)
   {
      my_class thisclass = new my_class();
      thisclass.GetData(m_MyConnection,,,,);
   }

   button2_click(e,,)
   {
      my_class thisclass = new my_class();
      thisclass.GetMoreData(m_MyConnection,,,,);
   }
}
[code]
A better approach would be to keep the connection in your data class, and simply keep a reference to it, instead of your connection:
[code]
class main_prog
{
   private my_class m_thisclass;

   main_prog
   {
       // initialize it in your constructor
       m_thisclass = new my_class(connectstring,,,);
   }

   button1_click(e,,)
   {
      m_thisclass.GetData(,,,,);
   }

   button2_click(e,,)
   {
      m_thisclass.GetData(,,,,,);
   }
}
[code]

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top