yeah..its very possible thanks to re-usability principle of OOP.
.NET has come up with the
ref
keyword which allows you to do this very thing..I'll put up a code sample in C# which will illustrate the usage of it in relation to your question..
.
.
static class DB_Conn
{
public static void Connection(ref SqlConnection conn, ref SqlCommand cmd)
{
conn = new SqlConnection();
conn.ConnectionString = "[Input Connection String here]";
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
}
}
Now using it in a different class without having to create everything again..
.
.
class UseData
{
SqlConnection conn;
SqlCommand cmd;
Public void deleteData(ref Conn, ref Cmd)
{
DB_Conn.Connection(Conn,Cmd);
cmd.CommandText="[Input stored Procedure/Query string here]";
conn.Open();
cmd.ExecuteNonQuery();
if(Conn.State == ConnectionState.Open)
{
Conn.Close();
}
cmd.Dispose();
}
}
And your program should run smoothly..
If you love the above, accept answer and give it a vote..:)