I want to write an overloaded function. Each overload will take as parameter one of different command objects like MySqlCommand, SqlCommand, OledbCommand etc and different DataAdapters. The function has many lines of code and the command object passed as a parameter will be referenced many times in the code. However, most lines of code do not reference the passed parameter value. My question is, do i have to repeat the whole function (duplicate same code) for all the overloads? I feel there could be a better way of doing this for easy code maintenance .
Illustration: - - private void xx (MySqlCommand c, MySqlDataAdapter a)
-
{
-
c.CommandText = "statement1";
-
c.ExecuteNonQuery();
-
c.CommandText = "select * from sometable";
-
a.SelectCommand=c;
-
DataSet d = new DataSet();
-
a.Fill(d,"Table1");
-
foreach(DataRow r in d.Tables["Table1"])
-
{
-
int i=int.Parse(r["Table1"].ToString());
-
//do something with i
-
//other code some referencing c & a, others not
-
}
-
}
-
-
-
private void xx (OleDbCommand c, OleDbDataAdapter a)
-
{
-
c.CommandText = "statement1";
-
c.ExecuteNonQuery();
-
c.CommandText = "select * from sometable";
-
a.SelectCommand=c;
-
DataSet d = new DataSet();
-
a.Fill(d,"Table1");
-
foreach(DataRow r in d.Tables["Table1"])
-
{
-
int i=int.Parse(r["Table1"].ToString());
-
//do something with i
-
//other code some referencing c & a, others not
-
}
-
}
-
-
//Other overloads with other command/dataadapter parameters
2 1070
Have you tried using a class?
I solved it using interfaces as illustrated below. This way, i did not need to have any overloads. - private void xx (IDbCommand c, IDbDataAdapter a)
-
{
-
c.CommandText = "statement1";
-
c.ExecuteNonQuery();
-
c.CommandText = "select * from sometable";
-
a.SelectCommand=c;
-
DataSet d = new DataSet();
-
a.Fill(d,"Table1");
-
foreach(DataRow r in d.Tables["Table1"])
-
{
-
int i=int.Parse(r["Table1"].ToString());
-
//do something with i
-
//other code some referencing c & a, others not
-
}
-
}
Thank you very much.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Steve Jorgensen |
last post by:
A while back, I started boning up on Software Engineering best practices and
learning about Agile programming. In the process, I've become much more
committed to removing duplication in code at a much finer level. As such,
it's very frustrating to be working in VBA which lacks inheritance, one of the
more powerful tools for eliminating duplication at the level I'm talking
about.
I've recently come up with a technique to emulate one...
|
by: Martin |
last post by:
I already know some techniques for writing small footprint software,
but now I need to really squeeze the software I'm writing into a small
space (embedded software).
Is there a good web page that discsuesses techniques for this?
A good book?
/Martin
|
by: bahadir.balban |
last post by:
Hi,
What's the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case statement:
void print_structs(void * struct_argument, enum struct_type stype)
{
switch(stype) {
case STRUCT_TYPE_1:
|
by: Charles Law |
last post by:
Take a solution with a project hierarchy along the lines of an n-tier
system, so that we have a data layer, business layer and presentation layer.
The presentation layer is coupled to the business layer, and the business
layer is coupled to the data layer. So far so good.
Suppose the data layer raises an event, and it passes Me (the sender) as an
object, and e (MyEventArgs, a descendent of EventArgs) to the layer above
(the business...
|
by: Martin Jørgensen |
last post by:
Hi,
I don't understand these errors I get:
g++ Persort.cpp
Persort.cpp: In function 'int main()':
Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping
Persort.cpp:37: error: using obsolete binding at 'j'
| |
by: StephQ |
last post by:
This is from a thread that I posted on another forum some days ago.
I didn't get any response, so I'm proposing it in this ng in hope of
better luck :)
The standard explanation is that pointer to functions are hard to
inline for the compiler, and so you won't be able to avoid function
call overhead.
This is an important aspect when you are calling a function very
frequently for evaluation reason: think of the problem of performing...
|
by: David Mark |
last post by:
I see the "When is a function not a function" thread has flared up
again.
This is what I use to test parameters that can be Functions or
Objects. It does return the expected result for callable host objects
(at least the ones I tested.) None of the functions it supports are
expected to receive those as parameters. The second part of the test
is a little ambiguous in this regard (for browsers that do not support
call), so it is best...
|
by: dbarmer |
last post by:
I have a unix database system that hold three tables, 254, 255, 256, and all are linked together by WO, CUST NO, LOC NO. This is for Unix Program Purposes Only. Duplication here is by design. So Up Front I have duplicated data due to a sequence work'd performed. For example. WO 000001 has duplicate WO, CUST, LOC etc... four times because it has four different Work Descriptions.
SO, When I transfer these tables to access and create a...
|
by: arnuld |
last post by:
I have created a program which creates and renames files. I have
described everything in comments. All I have is the
cod-duplication. function like fopen, sprint and fwrite are being called
again and again.
I know to remove code-duplication I have to make functions and pass
arguments to them but I am not able to think of a way doing it. Can you
post some example for me, out of this code:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |