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,

        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);
                            #endregion hour 1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                    #endregion // month 1
repeat to month 2
repeat to month 3
repeat to month 4

Open in new window

Who is Participating?
aikimarkConnect With a Mentor Commented:
My initial suggestion is to retrieve the setting by name, using the month and hour values to construct the name via concatenation.
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.
ocaccyAuthor Commented:

Returned these errors.

code page
error list
David Johnson, CD, MVPConnect With a Mentor OwnerCommented:
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??
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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

Monitor is my main Form.

Thank you.

David Johnson, CD, MVPConnect With a Mentor OwnerCommented:
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]
Utkarsh KulkarniConnect With a Mentor Sr. Sw EngCommented:
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.
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

Utkarsh KulkarniConnect With a Mentor Sr. Sw EngCommented:
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();
            System.Data.DataRow[] _collValues = _dsXMLValues.Tables[0].Select("ColumName=" + _EntryToLookup);
            double _Value = Convert.ToDouble(_collValues[0][1]);

Open in new window

aikimarkConnect With a Mentor Commented:
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

ocaccyAuthor Commented:
Thank you guys.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.