473,797 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Do I Avoid Code Duplication in Overloaded Functions?

22 New Member
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: -



Expand|Select|Wrap|Line Numbers
  1. private void xx (MySqlCommand c, MySqlDataAdapter a)
  2. {
  3.    c.CommandText = "statement1";
  4.    c.ExecuteNonQuery();
  5.    c.CommandText = "select * from sometable";
  6.    a.SelectCommand=c;
  7.    DataSet d = new DataSet();
  8.    a.Fill(d,"Table1");
  9.    foreach(DataRow r in d.Tables["Table1"])
  10.    {
  11.       int i=int.Parse(r["Table1"].ToString());
  12.       //do something with i
  13.       //other code some referencing c & a, others not
  14.    }
  15. }
  16.  
  17.  
  18. private void xx (OleDbCommand c, OleDbDataAdapter a)
  19. {
  20.    c.CommandText = "statement1";
  21.    c.ExecuteNonQuery();
  22.    c.CommandText = "select * from sometable";
  23.    a.SelectCommand=c;
  24.    DataSet d = new DataSet();
  25.    a.Fill(d,"Table1");
  26.    foreach(DataRow r in d.Tables["Table1"])
  27.    {
  28.       int i=int.Parse(r["Table1"].ToString());
  29.       //do something with i
  30.       //other code some referencing c & a, others not
  31.    }
  32. }
  33.  
  34. //Other overloads with other command/dataadapter parameters
Jan 23 '08 #1
2 1070
kenobewan
4,871 Recognized Expert Specialist
Have you tried using a class?
Jan 23 '08 #2
gkinu
22 New Member
I solved it using interfaces as illustrated below. This way, i did not need to have any overloads.

Expand|Select|Wrap|Line Numbers
  1. private void xx (IDbCommand c, IDbDataAdapter a)
  2. {
  3.    c.CommandText = "statement1";
  4.    c.ExecuteNonQuery();
  5.    c.CommandText = "select * from sometable";
  6.    a.SelectCommand=c;
  7.    DataSet d = new DataSet();
  8.    a.Fill(d,"Table1");
  9.    foreach(DataRow r in d.Tables["Table1"])
  10.    {
  11.       int i=int.Parse(r["Table1"].ToString());
  12.       //do something with i
  13.       //other code some referencing c & a, others not
  14.    }
  15. }
Thank you very much.
Jan 24 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
23114
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...
2
13393
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
44
2438
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:
19
2900
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...
26
2892
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'
5
2271
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...
13
8080
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...
1
1569
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...
61
3295
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:
0
9685
marktang
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...
1
10205
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,...
0
10021
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...
1
7559
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
6802
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
5458
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...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
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...

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.