skip to Main Content

This is my object

public class Totals {
        public int Total1 { get; set; }
        public int Total2 { get; set; }
        public int Total3 { get; set; }
        public int Total4 { get; set; }
    }

Incrementing the values of Total1 and Total2 using calculateTotals method

private Totals calculateTotals(Totals t) {
            if (//condition) {
                t.Total1 += 1;
            } else {
                 t.Total2 += 1;
            }
            return t;
        }

**Incrementing value of Total3 and Total4 of the same object with same conditions at a different location using different method calculateOtherTotals, at this point I only need to update Total3 and Total4 **

private Totals calculateOtherTotals(Totals t) {
            if (//condition) {
                t.Total3 += 1;
            } else {
                 t.Total4 += 1;
            }
            return t;
        }

I am new to c# , I need to increment the values Total1,Total2 and Total3,Total4 separately and the code which I have is working fine

Is there a way to improve my code?, how can I avoid creating two different methods which pretty much does the same logic on different properties? is there a way to create only 1 method to achieve my functionality?

3

Answers


  1. You could do it this way, but essentially the amount of code doesn’t change.

    This adds a judgment:

    Totals calculateTotals(Totals t, bool Flag)
    {
        //function1:
        if (Flag)
        {
            if (true)
            { //(condition) {
                t.Total1++;
            }
            else
            {
                t.Total2++;
            }
        }
        //function2:
        else
        {
            if (true)
            { //(condition) {
                t.Total3++;
            }
            else
            {
                t.Total4++;
            }
        }
        return t;
    }
    

    Call it like this:

    Totals totals = new Totals();
    totals.Total1=0;
    totals.Total2=0;
    totals.Total3=0;
    totals.Total4=0;
    
    calculateTotals(totals,true);//function1:
    calculateTotals(totals,false);//function2:
    
    Login or Signup to reply.
  2. Is there a way to improve my code?, how can I avoid creating two different methods which pretty much does the same logic on different properties? is there a way to create only 1 method to achieve my functionality?

    Then it depends on how you want your method (function) to be. (E.g., how you define what your function will do and how your class and properties are characteristic—which, currently, many who want to help you still wonder about.)

    Let me give another clear example.

    Assume that you answer your additional requirement are:

    • My object has only 4 properties of "Total"
    • I want these new function to increment value only 1 when call, no need to add more than 1
    • This function is called from another class to modify my object value
    • I want my cool function name calculateOtherTotals being private, because of some unexplained reason such as “I don’t like others knowing it exists”.

    Then

    public OtherClass{
        Public Totals ExposeThePrivateCalculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4)
        {
             calculateOtherTotals(t, IncrementT1 , IncrementT2 , IncrementT3,  IncrementT4);
        }
        Private Totals calculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4) {
            
            if( IncrementT1 )  t.Total1 += 1; //choose your style
            if( IncrementT2==true )  ++t.Total2;//choose your style
            if( IncrementT3!=false )  t.Total3++; //choose your style
              t.Total4 +=  IncrementT4==true?1:0;//choose your style
            return t;
        }
    
    }
    
    //In main (how to use)
    
    Totals t= new Totals();
    
    OtherClass doMyFunc = new OtherClass();
    t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, true, false,false,false); // result of operation => t.total1 += 1;
    t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, false, true,false,false); // result of operation => t.total2 += 1;
         
    
    Login or Signup to reply.
  3. Reflection is one way, though its slow and not a Domain Specific Language:

    Type totalsType = typeof(Totals);
    var totalToIncrement = condition;
    PropertyInfo prop = totalsType.GetProperty("Total" + totalToIncrement);
    prop.SetValue(null, 76);
    

    Or perhaps you want to abstract the properties you’re incrementing:

    private Totals calculateTotals(Totals t)
    {
        bool condition = true;
        AbstractAdds(ref t.Total1, ref t.Total2, condition);
        return t;
    }
    
    private void AbstractAdds(ref int a, ref int b, bool condition = false)
    {
        if (condition)
        { 
            a++;
        }
        else
        { 
            b++;
        }
    }
    
    }
    
    public class Totals
    {
    public int Total1;//{ get; set; }
    public int Total2;//{ get; set; }
    public int Total3;//{ get; set; }
    public int Total4;//{ get; set; }
    }
    

    I’d personally have a List<int> or int[3] and make the condition calculate the index 0-3:

    var index = calcCondition;
    Totals[index]++;
    

    This way its extensible for more totals and you get inbuilt functions like LINQ, eg Totals.Sum().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search