how to: encapsulate code via classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

this is a very newbie question re: oop - pls excuse my ignorance:

i have inserted in each of my asp.net web forms written using C# error
handing code that i would like to encapsulate in one, centralized class.
However, all of the examples i find regarding the usage of classes deal w/
inheritance.

my question is: is there a way to simply refer to methods in separate .cs
files, w/out having to use inheritance? it seems inefficient to write classes
w/ "virtual" methods and then "override" them if i never intent to use the
original methods?

For example...

try
{
// code to execute
}
catch (SqlException ex)
{
DisplaySQLErrors(ex);
}

.... where the method DisplaySQLErrors is centralized in a separate .cs file?

Thanks,
 
sure, you can create utility functions in a class...
check out the use of the 'static' keyword on a method. This article may
help
http://www.samspublishing.com/articles/article.asp?p=101373&seqNum=11

In general, you may also benefit from digging in to the 'design patterns'
literature a bit. All this stuff will make a lot more sense.
http://blogs.msdn.com/nickmalik/archive/2004/12/21/328727.aspx

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Thanks Nick. This is very straight forward and i've got it already working.
This leads me to another question: Would you recommend that i use a utility
file as you have suggested to centralize database connections?

For example...

using (SqlConnection cn = DBConn())
{
// do something w/ the open db connection
}

.... I would guess this is not a good idea b/c it might cause
conflicts/errors as multiple connections would be open at the same time???
Assuming i'm right, what's the best way to centralize the code to open
database connections?

Thanks again!
 
Actually, the suggestion you made is the preferred model for ASP.NET: create
the connection, use it, and close it.

a) each connection is only in use for a brief period of time, so your app
won't have more than a dozen or so actual connections to the database at any
one time. That is not a problem for SQL.

b) connection pooling allows the connections to be re-used, which means that
you have a very low overhead for creating the connection. You can leverage
the system to manage this resource for you, making your code far simpler to
write and use.

I would recommend against trying to manage the connection yourself. You
WILL end up with problems on multiple threads if you use a singleton to
manage a database connection. It isn't worth it.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Back
Top