How can I make my code modular?
-
I need help with breaking my algorithm into modules. The reason why is because the following code at the moment is essentially duplicated three times for each calculate button on my GUI Windows Forms Application. I was wondering if I break it down in to sub-routines this can be done? Can anyone help me with this? public void park1() { string allotedTime = txtTimeLimit1.Text; string startTime = txtEntryTime1.Text; string endTime = txtExitTime1.Text; if (String.IsNullOrEmpty(allotedTime) || String.IsNullOrEmpty(startTime) || String.IsNullOrEmpty(endTime)) { MessageBox.Show("Enter all time values"); } else { TimeSpan alloted = TimeSpan.Parse(allotedTime); DateTime start = DateTime.Parse(startTime); DateTime end = DateTime.Parse(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 need help with breaking my algorithm into modules. The reason why is because the following code at the moment is essentially duplicated three times for each calculate button on my GUI Windows Forms Application. I was wondering if I break it down in to sub-routines this can be done? Can anyone help me with this? public void park1() { string allotedTime = txtTimeLimit1.Text; string startTime = txtEntryTime1.Text; string endTime = txtExitTime1.Text; if (String.IsNullOrEmpty(allotedTime) || String.IsNullOrEmpty(startTime) || String.IsNullOrEmpty(endTime)) { MessageBox.Show("Enter all time values"); } else { TimeSpan alloted = TimeSpan.Parse(allotedTime); DateTime start = DateTime.Parse(startTime); DateTime end = DateTime.Parse(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(); } } }
Start by creating a seperate class (seperate from your GUI class) and pass the necessary data for the algortihm via or the constructor or via properties (or both). The GUI should handle data validation input. Perhaps make the calculation buttons only available if the data is valid. When the algorithm is finished it returns the result to the GUI who can display it. Small pseudocode example.
//GUI class
function calculate(){
int n1, n2;
bool parse1 = int.TryParse(txt1.Text, out n1);
bool parse2 = int.TryParse(txt2.Text, out n2);
if(parse1 && parse2){
Calculator calc = new Calculator();
txt_sum.Text = calc.Add(n1, n2);
txt_substract.Text = calc.Substract(n1, n2);
}
}//Calculator class
function Add(int n1, int n2){
return n1 + n2;
}function Substract(int n1, int n2){
return n1 - n2;
}Of course this is a very quick and dirty pseudo code example, you'll need to work the rest out from there. PS: If you want to do it properly move the calculations to some form of a business layer instead of just a seperate class. Hope this helps.
V.
-
I need help with breaking my algorithm into modules. The reason why is because the following code at the moment is essentially duplicated three times for each calculate button on my GUI Windows Forms Application. I was wondering if I break it down in to sub-routines this can be done? Can anyone help me with this? public void park1() { string allotedTime = txtTimeLimit1.Text; string startTime = txtEntryTime1.Text; string endTime = txtExitTime1.Text; if (String.IsNullOrEmpty(allotedTime) || String.IsNullOrEmpty(startTime) || String.IsNullOrEmpty(endTime)) { MessageBox.Show("Enter all time values"); } else { TimeSpan alloted = TimeSpan.Parse(allotedTime); DateTime start = DateTime.Parse(startTime); DateTime end = DateTime.Parse(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(); } } }
- Set the Tag property on each calc button to an appropriate (unique) integer index value, such as 1, 2, 3. etc 1) Set each calc button to use the same Click event handler 2) In the one-and-only Click event handler do something like this:
private void Calc_Click(object sender,...)
{
switch (Convert.ToInt32((Button)sender.Tag))
{
case 1 : MyCalcRoutine(1, txtTimeLimit1, txtEntryTime1, txtExitTime1); break;
case 2 : MyCalcRoutine(2, txtTimeLimit2, txtEntryTime2, txtExitTime2); break;
case 3 : MyCalcRoutine(3, txtTimeLimit3, txtEntryTime3, txtExitTime3); break;
}
}And finally, your generic calculation routine.
private void MyCalcRoutine(int index, TextBox timeLimit, TextBox entryTime, TextBox exitTime)
{
// I included the button index as the fist parameter in case you develop a need to do
// something that is button-specific in this method.string allotedTime = timeLimit.Text; string startTime = entryTime.Text; string endTime = exitTime; if (String.IsNullOrEmpty(allotedTime) || String.IsNullOrEmpty(startTime) || String.IsNullOrEmpty(endTime)) { MessageBox.Show("Enter all time values"); } else { TimeSpan alloted = TimeSpan.Parse(allotedTime); DateTime start = DateTime.Parse(startTime); DateTime end = DateTime.Parse(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) { overTime1++; } completeTime1++; lblOverTime1.Text = "" + overTime1; int percentage = (overTime1 \* 100) / completeTime1; lblOverTimePercentage1.Text = "" + percentage; if (percentage > 50) { warnWarden(); } }
}
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSO -
I need help with breaking my algorithm into modules. The reason why is because the following code at the moment is essentially duplicated three times for each calculate button on my GUI Windows Forms Application. I was wondering if I break it down in to sub-routines this can be done? Can anyone help me with this? public void park1() { string allotedTime = txtTimeLimit1.Text; string startTime = txtEntryTime1.Text; string endTime = txtExitTime1.Text; if (String.IsNullOrEmpty(allotedTime) || String.IsNullOrEmpty(startTime) || String.IsNullOrEmpty(endTime)) { MessageBox.Show("Enter all time values"); } else { TimeSpan alloted = TimeSpan.Parse(allotedTime); DateTime start = DateTime.Parse(startTime); DateTime end = DateTime.Parse(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'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 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'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 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();
It was late last night when I was coding that up, but I noticed a lot that could be broken down into that subclass. I also agree with earlier responses... some of this calculation logic could really be in another project altogether, separating the algorithm from the UI wiring. However, the original question asked how to make it modular... I believe that question has to do with the repetition in control logic. I assume the controls look like they're in a grid, having three columns or rows, each of which containing StartTimes, EndTimes, and the like. That's what the subclass was for. I worked a lot more on the logic this morning, and this is what I have now:
public class UIForm { public class TimeSet { private TextBox \_txtAllottedTime; private TextBox \_txtStartTime; private TextBox \_txtEndTime; private Label \_lblOverTime; private Label \_lblOverTimePercentage; private int \_overTime; private int \_completeTime; public TimeSpan AllottedTime => TimeSpan.Parse(\_txtAllottedTime.Text); private DateTime StartTime => DateTime.Parse(\_txtStartTime.Text); private DateTime EndTime => DateTime.Parse(\_txtEndTime.Text); public TimeSet( TextBox txtAllottedTime, TextBox txtStartTime, TextBox txtEndTime, Label lblOverTime, Label lblOverTimePercentage, int overTime, int completeTime) { \_txtAllottedTime = txtAllottedTime; \_txtStartTime = txtStartTime; \_txtEndTime = txtEndTime; \_lblOverTime = lblOverTime; \_lblOverTimePercentage = lblOverTimePercentage; OverTime = overTime; \_completeTime = completeTime; } public int OverTime { get => \_overTime; private set { \_overTime = value; \_lblOverTime.Text = value.ToString(); } } public bool TextWasEnteredByUser => string.IsNullOrEmpty(AllottedTime.Text) || string.IsNullOrEmpty(StartTime.Text) || string.IsNullOrEmpty(EndTime.Text); public TimeSpan Duration
-
Start by creating a seperate class (seperate from your GUI class) and pass the necessary data for the algortihm via or the constructor or via properties (or both). The GUI should handle data validation input. Perhaps make the calculation buttons only available if the data is valid. When the algorithm is finished it returns the result to the GUI who can display it. Small pseudocode example.
//GUI class
function calculate(){
int n1, n2;
bool parse1 = int.TryParse(txt1.Text, out n1);
bool parse2 = int.TryParse(txt2.Text, out n2);
if(parse1 && parse2){
Calculator calc = new Calculator();
txt_sum.Text = calc.Add(n1, n2);
txt_substract.Text = calc.Substract(n1, n2);
}
}//Calculator class
function Add(int n1, int n2){
return n1 + n2;
}function Substract(int n1, int n2){
return n1 - n2;
}Of course this is a very quick and dirty pseudo code example, you'll need to work the rest out from there. PS: If you want to do it properly move the calculations to some form of a business layer instead of just a seperate class. Hope this helps.
V.
Hey, I am also a programmer, so for that, I have some different thought, before taking any modulator you just take some class or integer and after you step by step you will do the code. For further about it[
epson error code 0xf1
](https://babasupport.org/printer/epson-error-code-0xf1/)just visit with it.