Writing a method to serve slightly different scenarios C#

mattc19

I struggled with what to title this as but hopefully I can explain a little better here. I am trying to write a program that will track an assembly through a 6 station assembly line. At each station, the operator will hit a button (such as station1start, station1stop, station2start, etc) and the button press event will send the timestamp to a database and also update the form visually by moving the traveling id number to the next station. I have this all working for the first couple of stations but I'm wondering if there is a way to use the same method for each station. For example have a method such as

void updateStart(int station_num)

where the station ID would be an argument but otherwise the method could be used for all of the stations. I know that variables in C# cannot be dynamically changed but am curious if there is another way to make this code cleaner. It seems like bad programming to have 6 methods almost identical. Especially if we were to add another 6 stations. See the screenshot of the form and my example code below of the button that the operator would hit when they started at station 2. Any help would be greatly appreciated!

http://i.stack.imgur.com/Ddxww.png

 private void Station2Start_Click(object sender, EventArgs e)
    {
        Station2Label.Text = Station1Label.Text;
        Station1Label.Text = "";
        Station1Status.Text = "";
        Station2Status.Text = "IN PROGRESS";
        addTimeToDb(2);
    }
Eric Lippert

The question is somewhat unclear but I believe it is:

I have the following code:

private void Station2Start_Click(object sender, EventArgs e)
{
    Station2Label.Text = Station1Label.Text;
    Station1Label.Text = "";
    Station1Status.Text = "";
    Station2Status.Text = "IN PROGRESS";
    addTimeToDb(2);
}
private void Station3Start_Click(object sender, EventArgs e)
{
    Station3Label.Text = Station2Label.Text;
    Station2Label.Text = "";
    Station2Status.Text = "";
    Station3Status.Text = "IN PROGRESS";
    addTimeToDb(2);
}

And so on, repeated many times with minor substitutions. How do I "DRY out" this code? (That is Don't Repeat Yourself.)

When you create the labels and status boxes put them in an array:

private Label[] stationLabels;
private Label[] statusLabels;
... 
// in your form initialization after the creation of the labels:
stationLabels = new [] { Station1Label, Station2Label, Station3Label, ... 
// and similarly for status labels.

Now write

private void StationClick(int station)
{
    stationLabels[station-1].Text = stationLabels[station-2].Text;
    ... and so on

And then each method becomes

private void Station2Start_Click(object sender, EventArgs e)
{
   StationClick(2);
}

private void Station3Start_Click(object sender, EventArgs e)
{
   StationClick(3);
}

And so on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"subset" and "[" on dataframe give slightly different results, why?

From Dev

Function giving slightly different answer than expected

From Dev

two slightly different header files in c++ used (one .lib file though) causing lots of errors but no warning generated. how to prevent it?

From Dev

Filter "base query" for slightly different results

From Dev

Color grouped bars slightly different in HighCharts

From Dev

Java Style: Catching a bunch of slightly different errors

From Dev

Different methods to square a value result in slightly different output - which method is most accurate / reliable?

From Dev

Generate slightly different APKs from the same code

From Dev

Slightly different floating point math results (C to golang)

From Dev

Same use case, different actors, slightly different scenarios

From Dev

Different behaviour in different scenarios of "warning C4800: 'int' : forcing value to bool 'true' or 'false'"

From Dev

Using xts with slightly different date structures

From Dev

Trying to process different scenarios in Service

From Dev

Why so many different types of writing main() in C

From Dev

How to merge xts objects with slightly different columns?

From Dev

Slightly different answers when changing associativity

From Dev

JJWT parse dont fail if SigningKey is slightly different

From Dev

Sort List of JSONObjects on different scenarios

From Dev

preg_match() delimiter error (slightly different)

From Dev

writing to different file names using for loop in C++ visual studio

From Dev

two slightly different header files in c++ used (one .lib file though) causing lots of errors but no warning generated. how to prevent it?

From Dev

Color grouped bars slightly different in HighCharts

From Dev

Transposing / Reshaping based on variable but slightly different

From Dev

Oracle : Generate rows with slightly different values in a column

From Dev

How to reuse a code which is slightly different

From Dev

Diff: slightly different filenames, same file

From Dev

Text over image css (slightly different)

From Dev

Navigation displaying slightly different in Firefox

From Dev

Same Method for Client and Server slightly different behavior

Related Related

  1. 1

    "subset" and "[" on dataframe give slightly different results, why?

  2. 2

    Function giving slightly different answer than expected

  3. 3

    two slightly different header files in c++ used (one .lib file though) causing lots of errors but no warning generated. how to prevent it?

  4. 4

    Filter "base query" for slightly different results

  5. 5

    Color grouped bars slightly different in HighCharts

  6. 6

    Java Style: Catching a bunch of slightly different errors

  7. 7

    Different methods to square a value result in slightly different output - which method is most accurate / reliable?

  8. 8

    Generate slightly different APKs from the same code

  9. 9

    Slightly different floating point math results (C to golang)

  10. 10

    Same use case, different actors, slightly different scenarios

  11. 11

    Different behaviour in different scenarios of "warning C4800: 'int' : forcing value to bool 'true' or 'false'"

  12. 12

    Using xts with slightly different date structures

  13. 13

    Trying to process different scenarios in Service

  14. 14

    Why so many different types of writing main() in C

  15. 15

    How to merge xts objects with slightly different columns?

  16. 16

    Slightly different answers when changing associativity

  17. 17

    JJWT parse dont fail if SigningKey is slightly different

  18. 18

    Sort List of JSONObjects on different scenarios

  19. 19

    preg_match() delimiter error (slightly different)

  20. 20

    writing to different file names using for loop in C++ visual studio

  21. 21

    two slightly different header files in c++ used (one .lib file though) causing lots of errors but no warning generated. how to prevent it?

  22. 22

    Color grouped bars slightly different in HighCharts

  23. 23

    Transposing / Reshaping based on variable but slightly different

  24. 24

    Oracle : Generate rows with slightly different values in a column

  25. 25

    How to reuse a code which is slightly different

  26. 26

    Diff: slightly different filenames, same file

  27. 27

    Text over image css (slightly different)

  28. 28

    Navigation displaying slightly different in Firefox

  29. 29

    Same Method for Client and Server slightly different behavior

HotTag

Archive