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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ocaccyAuthor Commented:

Returned these errors.

code page
error list
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??
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Monitor is my main Form.

Thank you.

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]
Utkarsh KulkarniSr. 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 KulkarniSr. 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

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.