• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

C# - Reducing code nested switch

Hello everyone,

How to reduce this code?

Save in my file settings a value for 24 hours in the 12 months of the year; not saved per day.

Then, this files has 12 months X 24 hours or 288 fields.
Use the following nomenclature: csv_01_01 or csv_(month)_(hour).

In my app has a method called getMonthHour().

 This method checks the month and the hour and then search in the settings file the value for the current month and time.

I'm using switch in this method;  well the code is very large.


*(2nd)How to reduce this code?

Thanks in advance,
ocaccy

        private void getMonthHour()
        {
            DateTime Xdt = DateTime.Now;
            Xthismonth = Xdt.Month;
            Xthishour = Xdt.Hour;

            if (File.Exists(Monitor.malyt.Folder_users + Monitor.malyt.ConfigINI))
            {
                IniFile ini = new IniFile(Monitor.malyt.Folder_users + Monitor.malyt.ConfigINI);

                #region Search values in Settings
                //var csvNow = "csv_" + Xmonth.ToString() + "_" + Xthishour.ToString();

                switch (Xthismonth)
                {
                    #region month 1
                    case 1:
                        switch (Xthishour)
                        {
                            #region hour 1
                            case 1:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_01);
                                break;
                            #endregion hour 1

                            #region hour 2
                            case 2:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_02);
                                break;
                            #endregion hour 2

                            #region hour 3
                            case 3:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_03);
                                break;
                            #endregion hour 3

                            #region hour 4
                            case 4:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_04);
                                break;
                            #endregion hour 4

                            #region hour 5
                            case 5:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_05);
                                break;
                            #endregion hour 5

                            #region hour 6
                            case 6:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_06);
                                break;
                            #endregion hour 6

                            #region hour 7
                            case 7:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_07);
                                break;
                            #endregion hour 7

                            #region hour 8
                            case 8:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_08);
                                break;
                            #endregion hour 8

                            #region hour 9
                            case 9:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_09);
                                break;
                            #endregion hour 9

                            #region hour 10
                            case 10:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_10);
                                break;
                            #endregion hour 10

                            #region hour 11
                            case 11:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_11);
                                break;
                            #endregion hour 11

                            #region hour 12
                            case 12:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_12);
                                break;
                            #endregion hour 12

                            #region hour 13
                            case 13:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_13);
                                break;
                            #endregion hour 13

                            #region hour 14
                            case 14:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_14);
                                break;
                            #endregion hour 14

                            #region hour 15
                            case 15:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_15);
                                break;
                            #endregion hour 15

                            #region hour 16
                            case 16:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_16);
                                break;
                            #endregion hour 16

                            #region hour 17
                            case 17:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_17);
                                break;
                            #endregion hour 17

                            #region hour 18
                            case 18:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_18);
                                break;
                            #endregion hour 18

                            #region hour 19
                            case 19:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_19);
                                break;
                            #endregion hour 19

                            #region hour 20
                            case 20:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_20);
                                break;
                            #endregion hour 20

                            #region hour 21
                            case 21:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_21);
                                break;
                            #endregion hour 21

                            #region hour 22
                            case 22:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_22);
                                break;
                            #endregion hour 22

                            #region hour 23
                            case 23:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_23);
                                break;
                            #endregion hour 23

                            #region hour 24
                            case 24:
                                Monitor.malyt.m3h = Convert.ToDouble(Settings.csv.csv_01_24);
                                break;
                            #endregion hour 24

                        }
                        break;
                    #endregion // month 1
.
.
repeat to month 2
repeat to month 3
repeat to month 4
..

Open in new window

0
ocaccy
Asked:
ocaccy
  • 4
  • 2
  • 2
  • +1
6 Solutions
 
aikimarkCommented:
My initial suggestion is to retrieve the setting by name, using the month and hour values to construct the name via concatenation.
Example:
Convert.ToDouble(Settings.csv["csv_" + Xthismonth.ToString + "_" + Xthishour.ToString];

Open in new window

You will probably need to fiddle with the month and hour values, formatting them to be two characters with left pad = zero.
0
 
ocaccyAuthor Commented:
Hi.

Returned these errors.

code page
error list
0
 
David Johnson, CD, MVPOwnerCommented:
you have 288 csv  files that are modified?? somewhere you seem to want a double Why don't you make a double array?
double [,] array = new double[12, 24};  

What is the definition for MonitorX  ?? and Monitor.settings??
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ocaccyAuthor Commented:
Sorry; I have only one file called settings.ini !

Monitor is my main Form.

Thank you.

ocaccy
0
 
David Johnson, CD, MVPOwnerCommented:
File.Exists(Monitor.malyt.Folder_users + Monitor.malyt.ConfigINI))
       
you still use an array and then use your gethour and getmonth
then double setting = array[month[[hour]
0
 
Utkarsh KulkarniCommented:
I will suggest you to create one XML file for your settings. Construct the string / value that you need to look-up in settings file.
You can then select the value from XML file using LINQ.
0
 
ocaccyAuthor Commented:
Aikimark, David, Utkarsh and other friends, using the instructions you gave me, the original code that had more than 2000 lines, now has 320 lines.

Aikimark thanks for the concatenation, Although not able to make it work; I believe that the concatenation is my solution.
David, thanks for the array.
Utkarsh thanks for the XML and LINQ, Could you help me with an example?

What is the correct way to concatenate these elements?
private void getMonthHour()
    {
        DateTime Xdt = DateTime.Now;
        int Xmonth = Xdt.Month;
        int Xhour = Xdt.Hour;

        if (File.Exists(confg.ini))
        {
            IniFile ini = new IniFile(confg.ini);

            m3h = Convert.ToDouble(confg.csv["csv_" + Xmonth.ToString + "_" + Xhour.ToString]);
        }
    }

Open in new window

0
 
Utkarsh KulkarniCommented:
I have written sample code as follows, you can add exception handler or conditions for validation etc as per your requirement

const string FILEINITIAL = "csv_";

        private void getMonthHour()
        {
            string _EntryToLookup = FILEINITIAL + DateTime .Now.Month.ToString () +"_"+ DateTime.Now.Hour.ToString();

            // Assuming you XML has two nodes one for lookup entry and another for its value
            // Load this XML in Dataset 
            string _XMLFileName = "";
            System.Data.DataSet _dsXMLValues = new System.Data.DataSet();
            _dsXMLValues.ReadXml(_XMLFileName);
            System.Data.DataRow[] _collValues = _dsXMLValues.Tables[0].Select("ColumName=" + _EntryToLookup);
            double _Value = Convert.ToDouble(_collValues[0][1]);
        }

Open in new window

0
 
aikimarkCommented:
This version of the ToString method in your posted code will cause the numeric value to be padded with leading zero.
m3h = Convert.ToDouble(confg.csv["csv_" + Xmonth.ToString("00") + "_" + Xhour.ToString("00")]);

Open in new window

0
 
ocaccyAuthor Commented:
Thank you guys.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now