C# Methods - Passing enum as Parameter

Hello everyone,

currently I am refactoring my existing code and I realized, that I have almost two identical methods. One method checks values in a matrix along the row, the other one does excatly the same along the columns.

I remember the function AddForce() in Unity. At the end, you can chose an enum for the force mode. For example ForceMode.Force, ForceMode.Impulse etc.

Example from UNITY DOCS:

using UnityEngine;

public class Example : MonoBehaviour
{
    public float thrust = 1.0f;
    public Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.AddForce(0, 0, thrust, ForceMode.Impulse);
    }
}

In the end, I would like to achieve a method call which looks like this:

    private void Start()
    {
        numberCollector(ExecutionMode.Row);
        numberCollector(ExecutionMode.Column);
    }

Now I would like to do the same with the following two methods, combining them to one.
Later, when calling the method, I like to chose something like ExecutionMode.AsRow or ExecutionMode.AsColumn.

Does anyone has an idea how to solve this issue in an elegant way?

Current Code for Columns:

    public List<int> numberCollectorColumn(int column)
    {
        int[] numberCollector = new int[gridSize];
        int numberCollectorIndex = 0;
        int numberCount = 0;

        for (int k = 0; k < gridSize; k++)
        {
            if (tileInfo[column, k])
            {
                numberCount++;
                numberCollector[numberCollectorIndex] = numberCount;
            }


            if (k < gridSize - 1)
            {
                if (tileInfo[column, k] && !tileInfo[column, k + 1])
                {
                    numberCollectorIndex++;
                    numberCount = 0;
                }
            }
        }

        List<int> numberCollectorList = new List<int>(numberCollector);
        return numberCollectorList;

    }

Current Code for Rows:

    public List<int> numberCollectorRow(int row)
    {
        int[] numberCollector = new int[gridSize];
        int numberCollectorIndex = 0;
        int numberCount = 0;
        for (int k = 0; k < gridSize; k++)
        {
            // Case No.1 - Current entry and next entry are both false.

            if (tileInfo[k, row])
            {
                numberCount++;
                numberCollector[numberCollectorIndex] = numberCount;
            }

            if (k < gridSize-1)
            {
                if (tileInfo[k, row] && !tileInfo[k + 1, row])
                {
                    numberCollectorIndex++;
                    numberCount = 0;
                }
            }
        }

        List<int> numberCollectorList = new List<int>(numberCollector);
        return numberCollectorList;
    }

just add it to the parameter definition of your function^^

public List<int> numberCollector(int index, ExecutionMode mode)//index being the row or collumn index
{
   if(mode == ExecutionMode.Row)
   {
      //whatever you wanna do if its row
   }
   else
   {
      //whatever you wanna do if its not row
   }
   //whatever the shared code is both row and collumn execute
}
1 Like

Many thank! Now it’s working fine.
Sometimes it’s easier than you think. ^^

As follows the complete solution, in case someone else will have a similiar issue in the future:

1. First declare the enum you’d like to use later:

    public enum ExecutionMode
    {
        row,
        column,
    }

2. Then, implement the enum as shown in the previous post of TheNightglow:

 public List<int> numberCollector(int index, ExecutionMode mode)
    {

        if (mode == ExecutionMode.row)
        {
            // do something for ExecutionMode.row
        }

        if (mode == ExecutionMode.column)
        {
            // do something for ExecutionMode.column
        }

  // do common stuff, which should be done in any case, e.g. returning value

        List<int> numberCollectorList = new List<int>(numberCollector);
        return numberCollectorList;

    }