Click here to Skip to main content
15,896,207 members
Home / Discussions / C#
   

C#

 
GeneralRe: Application service with C#. Pin
OriginalGriff16-Oct-18 23:02
mveOriginalGriff16-Oct-18 23:02 
GeneralRe: Application service with C#. Pin
Pete O'Hanlon16-Oct-18 23:59
mvePete O'Hanlon16-Oct-18 23:59 
AnswerRe: Application service with C#. Pin
#realJSOP22-Oct-18 9:03
professional#realJSOP22-Oct-18 9:03 
QuestionHow can I make my code modular? Pin
George Bradley16-Oct-18 12:43
George Bradley16-Oct-18 12:43 
AnswerRe: How can I make my code modular? Pin
V.16-Oct-18 18:44
professionalV.16-Oct-18 18:44 
GeneralRe: How can I make my code modular? Pin
henrydevid127-Oct-18 11:56
henrydevid127-Oct-18 11:56 
AnswerRe: How can I make my code modular? Pin
#realJSOP24-Oct-18 1:42
professional#realJSOP24-Oct-18 1:42 
AnswerRe: How can I make my code modular? Pin
josda100026-Oct-18 18:29
josda100026-Oct-18 18:29 
I'm not sure how far you got with breaking this down into a single algorithm, but here's what I got so far.

public class UIForm
{
    public class TimeSet
    {
        private TextBox _txtAllottedTime;
        private TextBox _txtStartTime;
        private TextBox _txtEndTime;

        public TimeSet(TextBox txtAllottedTime, TextBox txtStartTime, TextBox txtEndTime)
        {
            _txtAllottedTime = txtAllottedTime;
            _txtStartTime = txtStartTime;
            _txtEndTime = txtEndTime;
        }

        private string AllottedTime =>
            _txtAllottedTime.Text;
        private string StartTime =>
            _txtStartTime.Text;
        private string EndTime =>
            _txtEndTime.Text;

        public bool TextWasEnteredByUser =>
            string.IsNullOrEmpty(AllottedTime) ||
            string.IsNullOrEmpty(StartTime) ||
            string.IsNullOrEmpty(EndTime);
    }

    public List<TimeSet> TimeSets
    {
        new TimeSet(txtTimeLimit1, txtEntryTime1, txtExitTime1),
        new TimeSet(txtTimeLimit2, txtEntryTime2, txtExitTime2),
        new TimeSet(txtTimeLimit3, txtEntryTime3, txtExitTime3)
    };

    public void park1()
    {
        park(TimeSets[0]);
    }

    public void park2()
    {
        park(TimeSets[1]);
    }

    public void park3()
    {
        park(TimeSets[2]);
    }

    public void park(TimeSet timeSet)
    {
        if (!timeSet.TextWasEnteredByUser)
        {
            MessageBox.Show("Enter all time values");
        }
        else
        {
            TimeSpan alloted = TimeSpan.Parse(timeSet.AllottedTime);
            DateTime start = DateTime.Parse(timeSet.StartTime);
            DateTime end = DateTime.Parse(timeSet.EndTime);

            if (start > end)
                end = end.AddDays(1);

            TimeSpan duration = end.Subtract(start);

            //If the start time is greater than end that means the diff is above 12 hours
            //So subtracting the hours from a day to get the number of hours used if (TimeSpan.Compare(alloted, duration) == -1)
            if (TimeSpan.Compare(alloted, duration) == -1)
            {
                overTime1++;
            }

            completeTime1++;
            lblOverTime1.Text = "" + overTime1;
            int percentage = (overTime1 * 100) / completeTime1;
            lblOverTimePercentage1.Text = "" + percentage;

            if (percentage > 50)
            {
                warnWarden();
            }
        }
    }
}


I had to do some guessing, and as a forewarning, I obviously couldn't compile this code. However, what I am attempting to show you is that I created a subclass (one which should be in a separate file at this point), and this is handling one park# case at a time. I can initialize each TimeSet in a List<TimeSet>, and access a single instance of a TimeSet in a given park# call. This gives me the arguments I need. I then just call a single method, park, with all of the parameters I need in one object at the beginning.

Also, due to this cleanup, I can clean up that if statement to ask the object passed in one question, instead of calling string.IsNullOrEmpty() multiple times in one line. This makes the code a bit more readable.

You can continue on refactoring, because I do see there were a few other controls that you could add to the class, and frankly the rest of the else block could potentially be in this new class, cleaning up your UIForm code.

I would need a bit more context in order to be more thorough, but hopefully you see what I'm trying to do. Good luck.

GeneralRe: How can I make my code modular? Pin
josda100027-Oct-18 2:48
josda100027-Oct-18 2:48 
QuestionImagelist in Tabcontrol - Image selection Pin
RAFENS15-Oct-18 23:47
RAFENS15-Oct-18 23:47 
AnswerRe: Imagelist in Tabcontrol - Image selection Pin
BillWoodruff19-Oct-18 2:49
professionalBillWoodruff19-Oct-18 2:49 
QuestionImage processing in c# Pin
Member 1401942314-Oct-18 10:51
Member 1401942314-Oct-18 10:51 
AnswerRe: Image processing in c# Pin
Dave Kreskowiak14-Oct-18 11:56
mveDave Kreskowiak14-Oct-18 11:56 
GeneralRe: Image processing in c# Pin
Member 1401942314-Oct-18 14:44
Member 1401942314-Oct-18 14:44 
GeneralRe: Image processing in c# Pin
Dave Kreskowiak14-Oct-18 18:28
mveDave Kreskowiak14-Oct-18 18:28 
QuestionUploading images to web hosting server (a particular folder like "Images") through C#.NET Pin
PKSubudhi13-Oct-18 17:47
PKSubudhi13-Oct-18 17:47 
AnswerRe: Uploading images to web hosting server (a particular folder like "Images") through C#.NET Pin
OriginalGriff13-Oct-18 19:56
mveOriginalGriff13-Oct-18 19:56 
AnswerRe: Uploading images to web hosting server (a particular folder like "Images") through C#.NET Pin
Mycroft Holmes14-Oct-18 13:04
professionalMycroft Holmes14-Oct-18 13:04 
QuestionHow can we send SMS in C#.NET to any mobile number without API Gateway? Pin
PKSubudhi13-Oct-18 19:53
PKSubudhi13-Oct-18 19:53 
AnswerRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
Richard MacCutchan13-Oct-18 21:24
mveRichard MacCutchan13-Oct-18 21:24 
AnswerRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
Dave Kreskowiak14-Oct-18 4:53
mveDave Kreskowiak14-Oct-18 4:53 
AnswerRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
MadMyche15-Oct-18 6:25
professionalMadMyche15-Oct-18 6:25 
GeneralRe: How can we send SMS in C#.NET to any mobile number without API Gateway? Pin
MadMyche1-Aug-19 5:35
professionalMadMyche1-Aug-19 5:35 
QuestionaForge.net question Pin
Member 1345929011-Oct-18 4:46
Member 1345929011-Oct-18 4:46 
AnswerRe: aForge.net question Pin
Bernhard Hiller11-Oct-18 21:39
Bernhard Hiller11-Oct-18 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.