Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 686849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:03:59+00:00 2026-05-14T02:03:59+00:00

I have two functions whose underlying logic is the same but in one case

  • 0

I have two functions whose underlying logic is the same but in one case it sets one property value on a class and in another case it sets a different one. How can I rewrite the following two functions to abstract away as much of the algorithm as possible so that I can make changes in logic in a single place?

SetBillingAddress

private void SetBillingAddress(OrderAddress newBillingAddress)
{
    BasketHelper basketHelper = new BasketHelper(SiteConstants.BasketName);
    OrderAddress oldBillingAddress = basketHelper.Basket.Addresses[basketHelper.BillingAddressID];

    bool NewBillingAddressIsNotOldBillingAddress = ((oldBillingAddress == null) || (newBillingAddress.OrderAddressId != oldBillingAddress.OrderAddressId));
    bool BillingAddressHasBeenPreviouslySet = (oldBillingAddress != null);
    bool BillingAddressIsNotSameAsShippingAddress = (basketHelper.ShippingAddressID != basketHelper.BillingAddressID);
    bool NewBillingAddressIsNotShippingAddress = (newBillingAddress.OrderAddressId != basketHelper.ShippingAddressID);

    if (NewBillingAddressIsNotOldBillingAddress && BillingAddressHasBeenPreviouslySet && BillingAddressIsNotSameAsShippingAddress)
    {
        basketHelper.Basket.Addresses.Remove(oldBillingAddress);
    }

    if (NewBillingAddressIsNotOldBillingAddress && NewBillingAddressIsNotShippingAddress)
    {
        basketHelper.Basket.Addresses.Add(newBillingAddress);
    }

    basketHelper.BillingAddressID = newBillingAddress.OrderAddressId;
    basketHelper.Basket.Save();
}

And here is the second one:

SetShippingAddress

private void SetShippingAddress(OrderAddress newShippingAddress)
{
    BasketHelper basketHelper = new BasketHelper(SiteConstants.BasketName);
    OrderAddress oldShippingAddress = basketHelper.Basket.Addresses[basketHelper.ShippingAddressID];

    bool NewShippingAddressIsNotOldShippingAddress = ((oldShippingAddress == null) || (newShippingAddress.OrderAddressId != oldShippingAddress.OrderAddressId));
    bool ShippingAddressHasBeenPreviouslySet = (oldShippingAddress != null);
    bool ShippingAddressIsNotSameAsBillingAddress = (basketHelper.ShippingAddressID != basketHelper.BillingAddressID);
    bool NewShippingAddressIsNotBillingAddress = (newShippingAddress.OrderAddressId != basketHelper.BillingAddressID);

    if (NewShippingAddressIsNotOldShippingAddress && ShippingAddressHasBeenPreviouslySet && ShippingAddressIsNotSameAsBillingAddress)
    {
        basketHelper.Basket.Addresses.Remove(oldShippingAddress);
    }

    if (NewShippingAddressIsNotOldShippingAddress && NewShippingAddressIsNotBillingAddress)
    {
        basketHelper.Basket.Addresses.Add(newShippingAddress);
    }

    basketHelper.ShippingAddressID = newShippingAddress.OrderAddressId;
    basketHelper.Basket.Save();
}

My initial thought was that if I could pass a class’s property by refernce then I could rewrite the previous functions into something like

private void SetPurchaseOrderAddress(OrderAddress newAddress, ref String CurrentChangingAddressIDProperty)

and then call this function and pass in either basketHelper.BillingAddressID or basketHelper.ShippingAddressID as CurrentChangingAddressIDProperty but since I can’t pass C# properties by reference I am not sure what to do with this code to be able to reuse the logic in both places.

Thanks for any insight you can give me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:03:59+00:00Added an answer on May 14, 2026 at 2:03 am

    You can pass lambda expressions:

    private void SetPurchaseOrderAddress(OrderAddress newAddress, Func<string> addressIDGetter, Action<string> addressIDSetter) 
    

    Call it like this:

    SetPurchaseOrderAddress(newAddress, () => basketHelper.BillingAddressID, a => basketHelper.BillingAddressID = a);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So let's say I have two different functions. One is a part of the
EDIT: I also have access to ESXLT functions. I have two node sets of
I have two functions whose execution time I would like to compare: function subject_one()
I have two functions to add and remove table rows that contain a form
I have a PHP file, Test.php, and it has two functions: <?php echo displayInfo();
Say I have the following code: function One() {} One.prototype.x = undefined; function Two()
I have a function which parses one string into two strings. In C# I
Ok I have two functions the first of which looks like so: let dlth
I have two constructor : function clsUsagerEmailUserName($nickName, $email) { $this->nickName = $nickName; $this->email =
I have two classes that each need an instance of each other to function.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.