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 7847341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:51:51+00:00 2026-06-02T17:51:51+00:00

I have a function of an class in C# that compares one object to

  • 0

I have a function of an class in C# that compares one object to another. More or less a Equals function but with some added code. My object has quite a few variables and I would like to compare each and if any are different add them to a data table. Right now my function looks something like:

    public bool compareSysInfo(SystemInfo expected, SystemInfo comp)
    {
        ComparePopup popup = new ComparePopup();
        bool finalResult = true;//initial assumption that they are equal
        //compares branding
        if (expected.branding != comp.branding)
        {
            finalResult = false;
            popup.addDataToTable("Branding", comp.getBranding() + "", expected.getBranding() + "");
        }
        //compares pro #
        if (expected.getPro() != comp.getPro())
        {
            finalResult = false;
            popup.addDataToTable("Pro Number", comp.getPro() + "", expected.getPro() + "");
        }

It continues like that for all the variables. I would like to avoid excessive if statements, is there a way to create a abstract method and use a loop? I looked into delegates but I’m not sure how to use them in this context. Maybe I’m going about this wrong and should do something completely different like adding them to a collections and just using a for loop to compare the objects. I feel like I’m missing something obvious. I know any place that has repetitive code like this you can implement better, it’s just I don’t know how. Any advice here would be great.
Thanks for the help.

  • 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-06-02T17:51:53+00:00Added an answer on June 2, 2026 at 5:51 pm

    Create a strategy class, inherit from it for each rule, and provide your class with a list containing these:

    public MyClass {
     readonly IEnumerable<Rule> _rules;
     public MyClass(IEnumerable<Rule> rules) {
        _rules
     }
    
     public bool CompareSysInfo(SystemInfo expected, SystemInfo comp) {
       // i prefer linq over loops
       var result = from r in _rules
                    where !r.CheckRule(expected, comp)
                    select false;
       return result.Count() > 0; // only returns true if no rule checks return false
     }
    }
    

    Then for each rule check you implement an instance of the Rule class (or interface):

    public abstract class Rule {
       protected ComparePopup Popup { get; private set; }
       protected Rule(ComparePopup popup) {
         Popup = popup;
       }
       public abstract bool CheckRule(SystemInfo expected, SystemInfo comp);
    }
    
    public class BrandingRule : Rule {
    
       public BrandingRule(ComparePopup popup) : base(popup) { }
    
       public override bool CheckRule(SystemInfo expected, SystemInfo comp) {
         var result = expected.branding == comp.branding;
         if(!result)
           Popup.addDataToTable("Branding", comp.getBranding() + "", expected.getBranding() + "");
       }
    }
    

    This strategy affords you the ability to add, remove, or modify rules and what they do independently of the code that needs to use them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function (class) that takes a function reference as one paremter.
I have a member function in a class that has a callback, but the
I have a function inside a class that returns a reference to a member
I have this class that have a function to load other classes and create
Im making a program for class that manages a Hotel. I have a function
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that accepts a class (not an instance) and, depending on
I have a function that accepts a variable number of parameters: foo (Class... types);
Does anyone have a working class or function to create the hashed email that
class SomeClass { //some members MemberClass one_of_the_mem_; } I have a function foo( SomeClass

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.