473,804 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to: encapsulate code via classes

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)
{
DisplaySQLError s(ex);
}

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

Thanks,
Nov 16 '05 #1
3 1735
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/articl...1373&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/arch...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.
--
"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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)
{
DisplaySQLError s(ex);
}

... where the method DisplaySQLError s is centralized in a separate .cs file?
Thanks,

Nov 16 '05 #2
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!
"Nick Malik [Microsoft]" wrote:
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/articl...1373&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/arch...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.
--
"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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)
{
DisplaySQLError s(ex);
}

... where the method DisplaySQLError s is centralized in a separate .cs

file?

Thanks,


Nov 16 '05 #3
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.
--
"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:65******** *************** ***********@mic rosoft.com...
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!
"Nick Malik [Microsoft]" wrote:
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/articl...1373&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/arch...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.
--
"charliewes t" <ch*********@di scussions.micro soft.com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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)
{
DisplaySQLError s(ex);
}

... where the method DisplaySQLError s is centralized in a separate .cs

file?

Thanks,


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1330
by: Bob Garbados | last post by:
I'm developing some reservation forms for a variety of events that all contain common contact and billing info fields. There are also several fields pertaining to each specific event. My initial thought was to use include files to handle the common fields... one for the contact fields, one for the contact field validation, one for the billing fields, and one for the billing field validation. Would it be better to implement this using...
0
2492
by: beyond | last post by:
does anyone knows, how to encapsulate pl/sql-packages into DAO-object (java)? we are considering to separate logic from data-structure in oracle. what are the technical problems /risks doing this? are there other options?
2
1189
by: Matt | last post by:
Hiding Method. How to Hide tx.carry() I want tx.carry not to be reached on test(); any ideas? public abstract class Car { public abstract void run(); public abstract void carry(); } public class Taxi : Car {
171
7806
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
5
1675
by: Thorbjørn Jørgensen | last post by:
Hi How can I encapsulate a specific method from a specific object? I have some different object with different methods, and I would like that the object could put a reference to one of its methods into a common arraylist. A control element should then at a later time, go through the arraylist and execute the methods, but I do not know how to encapsulate the methods to put then into the arraylist? Hope that my question is somehos...
6
1849
by: TBass | last post by:
Hi, I've been using C for years, but I'm new to C++. For the most part it's been smooth, but here's my current snag: COPCClassFactor.h =============== class COPCClassFactory {
1
1779
by: Jason Huang | last post by:
Hi, In my C# windows form, is it possible to encapsulate a Label to an Image? If possible, how to do that? Thanks for help. Jason
2
1848
by: kabradley | last post by:
Hello everyone. I have three reports that I would like to put into one main report. I tried creating the main report and then putting three seperate subreports in the detail section of the parent report, but the page headers in the subreports do not print. After searching, I learned that the reason for this is because subreports do not have anything except group headers/footers and report headers/footers. I then set the record source of the...
4
2573
by: nw | last post by:
Hi All, I currently have a vector of objects (lets call them MyObject). I want to perform various operations regularly on the whole vector, for example find the maximum, average, or operations not dissimilar to that. So as I see it I have 3 options: 1. Implement these operations as functions 2. Derive a class from vector (from googling people seem to think this is a bad idea)
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.