none
C#: Need expert advice. Common Class RRS feed

  • Question

  • Inside program, I have a repeated Method with different value in same class and also same for other classes based on model criteria.
    What is the best way for me to use common class for all model?

    *Parameter value is call from SQL DB

    Below is current source code

    class ParameterSetting_ModelAA
        {
            public void ParameterSetting01(out HTuple hv_DarkThreshold, out HTuple hv_MinDarkThreshold,
                out HTuple hv_MaxDarkThreshold)
            {
                hv_DarkThreshold = 30;
                hv_MinDarkThreshold = 10;
                hv_MaxDarkThreshold = 140;
                return;
            }
            public void ParameterSetting02(out HTuple hv_DarkThreshold, out HTuple hv_MinDarkThreshold,
                out HTuple hv_MaxDarkThreshold,)
            {
    
                hv_DarkThreshold = 10;
                hv_MinDarkThreshold = 0;
                hv_MaxDarkThreshold = 110;
                return;
            }
            public void ParameterSetting03(out HTuple hv_DarkThreshold, out HTuple hv_MinDarkThreshold,
                out HTuple hv_MaxDarkThreshold)
            {
                hv_DarkThreshold = 20;
                hv_MinDarkThreshold = 0;
                hv_MaxDarkThreshold = 150;
                return;
            }
    }
    
    class ParameterSetting_ModelBB
        {
            public void ParameterSetting01(out HTuple hv_DarkThreshold, out HTuple hv_MinDarkThreshold,
                out HTuple hv_MaxDarkThreshold)
            {
                hv_DarkThreshold = 0;
                hv_MinDarkThreshold = 10;
                hv_MaxDarkThreshold = 200;
                return;
            }
            public void ParameterSetting02(out HTuple hv_DarkThreshold, out HTuple hv_MinDarkThreshold,
                out HTuple hv_MaxDarkThreshold,)
            {
    
                hv_DarkThreshold = 80;
                hv_MinDarkThreshold = 50;
                hv_MaxDarkThreshold = 160;
                return;
            }
            public void ParameterSetting03(out HTuple hv_DarkThreshold, out HTuple hv_MinDarkThreshold,
                out HTuple hv_MaxDarkThreshold)
            {
                hv_DarkThreshold = 70;
                hv_MinDarkThreshold = 80;
                hv_MaxDarkThreshold = 120;
                return;
            }
    }

    Please help and advice.

    Thanks in advance

    Friday, September 21, 2018 12:30 AM

All replies

  • ( Notice that you are using classes as namespaces )

    It depends on some factors.

    Considering that those classes provide just constant values:

    1) If the code that use them, do use the constant values from just 1 class, then I suggest simple enums.

    2) If the code that use them, do use the constant values from all classes, then I suggest nested enums.

    3) ...

    Friday, September 21, 2018 3:50 AM
  • I would create settings factory class. I think there are some differences between settings. So you can create ModelAASettingsFactory which could have CreateSettings01/02/03 methods to return setting object. You can create abstract class above this class and inherit it for ModelBB setting factory class.

    Please comment each method to know what this setting means. Commens can you provide useful naming for methods.

    Create your Setting object as immutable. It means values cannot be changed.

    public class ModelSetting
    {
      public int hvDarkThreshold{get;}
      public int hvMinDarkThreshold{get;}
      public int hvMaxDarkThreshold{get;}
      public ModelSetting(int hvDarkThreshold, int hvMinDarkThreshold, int hvMaxDarkThreshold)
      {
        this.hvDarkThreshold=hvDarkThreshold;     
        this.hvMinDarkThreshold=hvMinDarkThreshold;
        this.hvMaxDarkThreshold=hvMaxDarkThreshold;
      }
    }

    And factory method will be:

    public ModelSetting GetSetting01()
    {
      return new ModelSetting(0, 10, 3);
    }

    Friday, September 21, 2018 9:05 AM