Global class

  • Thread starter Thread starter Gav
  • Start date Start date
G

Gav

Hi all,

This may not be the best way to do things I'm not sure... however I have
created a class that handles all the database calls, I didn't want to keep
having to create connections to the database or instances of the same class
so I wanted to make an instance of the class in the top level window form
and be able to access it from the child windows. Is this possible to do? if
so how do you access the instance of the class at the top level?

Gav
 
A few thoughts regarding your question:

1~ take a look at the EnterpriseLibrary and its Data Access Block,
since they seem to do what your data access class does; relevant links:

http://olorinblog.blogspot.com/2005/02/el-intro.html
a post from my blog about the E.L. in general; it starts with a list of
links you may want to check out for more info as well;

2~ anyway, if you have your data access in a MyDataAccess class,
and your main form has an instance of this class (let's say this
instance is named 'dataLayer'), then you can pass it to your child form
as an argument of the child form constructor, like this:

MyChildForm childForm = new MyChildForm(dataLayer);

of course, this requires you to modify the constructor of the child
form to accept this argument. Most likely, you'll also want to set up a
private field of the child form of MydataAccess type, and assign to it
the value received in the constructor. For instance:

public class MyChildForm : Form
{
private MyDataAccess dataLayer;
public MyChildForm(MyDataAccess inDataLayer)
{
this.dataLayer = inDataLayer;
}
}

then you can use the this.dataLayer from anywhere in your child form.

3~ an alternative is to make your data access class abstract, so that
it is not instantiated, and have it expose only static methods. Even if
you are not instantiating an object of this type, you can do some
set-up work from its static constructor, like this:

public abstract class MyDataAccess
{
MyConnection conn;

static MyDataAccess()
{
MyDataAccess.conn = new MyConnection(/*any parameter needed*/);
//anything else you need to do to set the connection up
}
public static DataSet Select(string sqlStatement)
{
//Run the statement using the MyDataAccess.conn connection and
return the DataSet
}
}

This might or might not make sense, depending on the type of
application you're working in.

HTH,
F.O.R.
 
Thanks for the reply

I just have a question about the third part, apart from the 'abstract' and
'static' comments it is basically what I already have... but how would I
make calls to the class from my forms?

thanks again
Gav
 
Gav said:
This may not be the best way to do things I'm not sure... however I have
created a class that handles all the database calls, I didn't want to keep
having to create connections to the database or instances of the same class
so I wanted to make an instance of the class in the top level window form
and be able to access it from the child windows. Is this possible to do? if
so how do you access the instance of the class at the top level?

Just as an aside, the best thing to do *is* to keep creating
connections to the database, and close the connection when you've
finished. It won't close the real underlying connection - the
connection pool will handle that for you.
 
My class looks similar to how you described in section 3 and I think I'll
pass it on like in section 2 (I like to make my own code)... how would you
suggest I pass the parameters to the class (considering I have no idea how
many there could be depending on the call)?

thanks
Gav
 
Back
Top