Class Instantiation

G

Guest

I have aVB.NET application that uses an SQL Server wrapper class to connect
and perform any and all ADO.NET database activities. This is an MDI child
based application where the child forms are in an MDI tabbed environment. So
when a new MDI child form is displayed, a new tab is created much like VS.NET
2005 development environment.

Is it best to dimension a new SQL wrapper class at the module level of a
child form, or should I be creating a new class in every function that
accesses the database? If done at the function level, in theory, the class is
instantiated, used and then released for garbage collection when the function
terminates. As opposed to creating a new wrapper class at the module level
and using that variable in all the functions that access the database. This
would keep the class in memory until the child form is closed.

There may be many functions (10-15) within a given child form that access
the database. So should there be a Dim Myconn as New SQLWrapperClass in
every function or is it better to create the class once at the module level?

Thanks,
 
G

Guest

I have a similiar application and instantiate the Database access class in a
module so I can use it in all classes. I probalby will get "Flamed" for this
and if you don't want to do this, you can make all properties and methods
shared in the class. This way, you won't have instantiate the class as a
global.
 
G

Guest

So I could create a global reference to the SQL wrapper class, something like
this:

Module Generic
Public MyConn as New SQLWrapper
End Module

And then use Myconn as a global variable throughout the application. Sounds
like a interesting idea. Is this generally a good practice?
 
G

Guest

Yes that works. If you want to use the shared technique then;

public Class SQLWrapper
Shared property ....
Shared method ....
end Class

Then when used in your other classes, etc;

SqlWrapper.property = ....

SqlWrapper does not need to be instantiated.
 

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