troubleshooting Question

How can I avoid repeating the same parameters in 2 different methods

Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada asked on
C#* Parameters
10 Comments1 Solution22 ViewsLast Modified:
The 2 Methods below use exactly the same parameters
The first method-1 users the method to generate a list from database
records using a stored procedure

The second Method-2  uses the same parameters but this time to write or
delete a record

Method-1
 []public static List<LocationModel> GetUsers_Filtered(string locationid,string  locationdes,string thecompany,string dummyidx ,string  dowhat)
        {
            var p = new
            {
                locationidx = locationid,
                locdescriptionx = locationdes,
                companycodex = thecompany,
                idx = Convert.ToInt32(dummyidx),
                dowhatx = dowhat
            };
            return NewDataAccess.ReadData<LocationModel>("public.splocationcode_search", p);
        }[/code]

Method-2       
   []   public static void CreateLocation(string locationvalue, string locdescriptionvalue, string companycodevalue, int idvalue, string dowhatvalue)
        {
            // Note This parameter definition is good for inserts and updates and delete into/From the database
            var p = new
            {
                locationidx = locationvalue,
                locdescriptionx = locdescriptionvalue,
                companycodex = companycodevalue,
                idx = idvalue,
                dowhatx = dowhatvalue   // Can be UI DM or DP ie UpdateInsert, DeleteMarked or DeletePermanent
            };


            switch (LoginDetails.staticinsertupdatedeletetag)
            {
                case "IU":                                  // Update and Inserts Stored Procedure
                    NewDataAccess.WriteData("public.splocationcode_updateinsert", p);
                    break;
                case "DM":                                  //  Delete Marked
                    NewDataAccess.WriteData("public.splocationcode_delete", p);
                    break;
                default:
                    break;
            }

        }[/code]

The 2 methods work as required but i would if possible not like to
repeat the same set of parameters twice.

How can I refactor this code to ensure that the portion below is not repeated twice
 var p = new
            {
                locationidx = locationid,
                locdescriptionx = locationdes,
                companycodex = thecompany,
                idx = Convert.ToInt32(dummyidx),
                dowhatx = dowhat
            };


Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros