Avatar of paulpp
paulppFlag for United States of America

asked on 

Duplicate Code Driving me Crazy

Hello,

I am trying to improve my coding practices, and have an area I could use help getting some advice on.  This is the code in a static Class in a WCF project.  It checks to see if an item is in the database already and active, in the database and inactive, or not in the database. del in the database is a bit type so will return true or false.
 public static bool? CheckForComplaintCode(IndComplaint ind)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM comcodes where comcode = @ComCode";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@ComCode", ind.ComCode));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }

        public static bool? CheckForDepartmentCode(IndDepartment ind)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM deptcode where deptcode = @DeptCode";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@DeptCode", ind.DeptCode));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }//end def 'CheckForDepartmentCode'

Open in new window


I have 15 of these, each taking a differnt object.  I understand duplicated code is not ideal, and want to know another way of doing this, that uses some aspect of C# I am not fully educated in.

 I thought about using inheritance and making all the objects Derive from a Parent calss, then do the following:
 if (object is IndDepartment) { do stuff.. }
 but that seems like it will be the same amount of code, but only in one function.

I would like to use Delegates, Func<>  or Action<> to solve this, but while I understande the syntax, I cant wrap my head around how to apply them (of if they even apply in this situation).  

Any help would be appreciated.  Thank you.
C#WCF.NET Programming

Avatar of undefined
Last Comment
ogm
ASKER CERTIFIED SOLUTION
Avatar of ogm
ogm
Flag of Spain image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo