[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C# - Reducing code nested switch

Posted on 2014-04-27
10
Medium Priority
?
384 Views
Last Modified: 2014-04-29
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
Comment
Question by:ocaccy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 46

Accepted Solution

by:
aikimark earned 668 total points
ID: 40026345
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
 

Author Comment

by:ocaccy
ID: 40026469
Hi.

Returned these errors.

code page
error list
0
 
LVL 83

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 668 total points
ID: 40026526
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:ocaccy
ID: 40026540
Sorry; I have only one file called settings.ini !

Monitor is my main Form.

Thank you.

ocaccy
0
 
LVL 83

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 668 total points
ID: 40026717
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
 
LVL 7

Assisted Solution

by:Utkarsh Kulkarni
Utkarsh Kulkarni earned 664 total points
ID: 40027173
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
 

Author Comment

by:ocaccy
ID: 40029181
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
 
LVL 7

Assisted Solution

by:Utkarsh Kulkarni
Utkarsh Kulkarni earned 664 total points
ID: 40029262
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
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 668 total points
ID: 40029388
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
 

Author Closing Comment

by:ocaccy
ID: 40029594
Thank you guys.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question