Solved

C# - Reducing code nested switch

Posted on 2014-04-27
10
373 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
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 167 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 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 167 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
 

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 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 167 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Assisted Solution

by:Utkarsh Kulkarni
Utkarsh Kulkarni earned 166 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 166 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 45

Assisted Solution

by:aikimark
aikimark earned 167 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now