Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved C# Methods - Passing enum as Parameter

Discussion in 'Scripting' started by DonPuno, Apr 24, 2021.

  1. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    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:
    https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Example : MonoBehaviour
    4. {
    5.     public float thrust = 1.0f;
    6.     public Rigidbody rb;
    7.  
    8.     void Start()
    9.     {
    10.         rb = GetComponent<Rigidbody>();
    11.         rb.AddForce(0, 0, thrust, ForceMode.Impulse);
    12.     }
    13. }
    In the end, I would like to achieve a method call which looks like this:

    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         numberCollector(ExecutionMode.Row);
    4.         numberCollector(ExecutionMode.Column);
    5.     }
    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:
    Code (CSharp):
    1.     public List<int> numberCollectorColumn(int column)
    2.     {
    3.         int[] numberCollector = new int[gridSize];
    4.         int numberCollectorIndex = 0;
    5.         int numberCount = 0;
    6.  
    7.         for (int k = 0; k < gridSize; k++)
    8.         {
    9.             if (tileInfo[column, k])
    10.             {
    11.                 numberCount++;
    12.                 numberCollector[numberCollectorIndex] = numberCount;
    13.             }
    14.  
    15.  
    16.             if (k < gridSize - 1)
    17.             {
    18.                 if (tileInfo[column, k] && !tileInfo[column, k + 1])
    19.                 {
    20.                     numberCollectorIndex++;
    21.                     numberCount = 0;
    22.                 }
    23.             }
    24.         }
    25.  
    26.         List<int> numberCollectorList = new List<int>(numberCollector);
    27.         return numberCollectorList;
    28.  
    29.     }
    Current Code for Rows:
    Code (CSharp):
    1.     public List<int> numberCollectorRow(int row)
    2.     {
    3.         int[] numberCollector = new int[gridSize];
    4.         int numberCollectorIndex = 0;
    5.         int numberCount = 0;
    6.         for (int k = 0; k < gridSize; k++)
    7.         {
    8.             // Case No.1 - Current entry and next entry are both false.
    9.  
    10.             if (tileInfo[k, row])
    11.             {
    12.                 numberCount++;
    13.                 numberCollector[numberCollectorIndex] = numberCount;
    14.             }
    15.  
    16.             if (k < gridSize-1)
    17.             {
    18.                 if (tileInfo[k, row] && !tileInfo[k + 1, row])
    19.                 {
    20.                     numberCollectorIndex++;
    21.                     numberCount = 0;
    22.                 }
    23.             }
    24.         }
    25.  
    26.         List<int> numberCollectorList = new List<int>(numberCollector);
    27.         return numberCollectorList;
    28.     }
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    just add it to the parameter definition of your function^^

    Code (CSharp):
    1.  
    2. public List<int> numberCollector(int index, ExecutionMode mode)//index being the row or collumn index
    3. {
    4.    if(mode == ExecutionMode.Row)
    5.    {
    6.       //whatever you wanna do if its row
    7.    }
    8.    else
    9.    {
    10.       //whatever you wanna do if its not row
    11.    }
    12.    //whatever the shared code is both row and collumn execute
    13. }
    14.  
     
    DonPuno likes this.
  3. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    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:

    Code (CSharp):
    1.     public enum ExecutionMode
    2.     {
    3.         row,
    4.         column,
    5.     }

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


    Code (CSharp):
    1.  public List<int> numberCollector(int index, ExecutionMode mode)
    2.     {
    3.  
    4.         if (mode == ExecutionMode.row)
    5.         {
    6.             // do something for ExecutionMode.row
    7.         }
    8.  
    9.         if (mode == ExecutionMode.column)
    10.         {
    11.             // do something for ExecutionMode.column
    12.         }
    13.  
    14.   // do common stuff, which should be done in any case, e.g. returning value
    15.  
    16.         List<int> numberCollectorList = new List<int>(numberCollector);
    17.         return numberCollectorList;
    18.  
    19.     }