patterncsharpModerate
Managing part types and counts
Viewed 0 times
managingparttypesandcounts
Problem
Both functions look fairly similar to each other, but they are different in the type of string. I have 6 functions like this and all differ only in the "string cmd". Any ideas on how I can put it all into one?
```
public void getFrmSingProgTbl(int flg)
{
if (flg == 1)
{
object[] astatus = new object[1];
astatus = td.CheckOk(td.t1snd);
string status = (string)astatus[0];
if (status == "true")
{
string cmd = getCurrTime() + "|" + "part-type" + "|" + t1prtNameTxt.Text + "\n";
cmd += getCurrTime() + "|" + "operation-type" + "|" + t1oprNameTxt.Text + "\n";
string showCmd = "Part Type - " + t1prtNameTxt.Text + "\nOperation - " + t1oprNameTxt.Text;
if (MessageBox.Show(showCmd, "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
HandleAfterSend(td.t1snd, td.t1stp, flg, td.t1btn);
sndData(cmd,1);
}
}
else
{
this.ActiveControl = (TextBox)astatus[1];
MessageBox.Show(status, "Error");
}
}
else if (flg == 2)
{
object[] astatus = new object[1];
astatus = td.CheckOk(td.t1stp);
string status = (string)astatus[0];
if (status == "true")
{
string cmd = getCurrTime() + "|" + "part-count-good" + "|" + t1gpTxt.Text + "\n";
cmd += getCurrTime() + "|" + "part-count-bad" + "|" + t1bpTxt.Text + "\n";
string showCmd = "Good Parts - " + t1gpTxt.Text + "\nBad Parts - " + t1bpTxt.Text;
if (td.CheckNumeric(td.t1stp))
{
if (MessageBox.Show(showCmd, "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
HandleAfterSend(td.t1stp, td.t1snd, flg, td.t1bt
```
public void getFrmSingProgTbl(int flg)
{
if (flg == 1)
{
object[] astatus = new object[1];
astatus = td.CheckOk(td.t1snd);
string status = (string)astatus[0];
if (status == "true")
{
string cmd = getCurrTime() + "|" + "part-type" + "|" + t1prtNameTxt.Text + "\n";
cmd += getCurrTime() + "|" + "operation-type" + "|" + t1oprNameTxt.Text + "\n";
string showCmd = "Part Type - " + t1prtNameTxt.Text + "\nOperation - " + t1oprNameTxt.Text;
if (MessageBox.Show(showCmd, "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
HandleAfterSend(td.t1snd, td.t1stp, flg, td.t1btn);
sndData(cmd,1);
}
}
else
{
this.ActiveControl = (TextBox)astatus[1];
MessageBox.Show(status, "Error");
}
}
else if (flg == 2)
{
object[] astatus = new object[1];
astatus = td.CheckOk(td.t1stp);
string status = (string)astatus[0];
if (status == "true")
{
string cmd = getCurrTime() + "|" + "part-count-good" + "|" + t1gpTxt.Text + "\n";
cmd += getCurrTime() + "|" + "part-count-bad" + "|" + t1bpTxt.Text + "\n";
string showCmd = "Good Parts - " + t1gpTxt.Text + "\nBad Parts - " + t1bpTxt.Text;
if (td.CheckNumeric(td.t1stp))
{
if (MessageBox.Show(showCmd, "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
HandleAfterSend(td.t1stp, td.t1snd, flg, td.t1bt
Solution
Quick thoughts off the top of my head.
-
Seems like you could replace each function with (ignoring all the changes above):
And then call it with:
- Use the standard .Net naming convention and don't abbreviate
- Don't change the whole processing of a method by a flag
- If you control td.CheckOk(...) why does it return an array when it isn't used that way? Also, it should return a bool rather than a string "true". This should return a specific type rather than a bunch of data in various array indexes.
- Why are you creating a new array astatus = new object[1]; and then immediately replacing it with the value from td.CheckOk?
- Use StringBuilder rather than lots of string concats
-
Seems like you could replace each function with (ignoring all the changes above):
public void GetResult(Func generateCommand, Func generateSendData) {
object[] astatus = td.CheckOk(td.t1snd);
if (astatus != null && astatus.Length > 0 && (string)astatus[0] == "true") {
string cmd = generateCommand();
string data = generateSendData();
if (MessageBox.Show(showCmd, "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) {
HandleAfterSend(td.t1snd, td.t1stp, flg, td.t1btn);
sndData(cmd,1);
}
} else {
this.ActiveControl = (TextBox)astatus[1];
MessageBox.Show(astatus[0].ToString(), "Error");
}
}And then call it with:
GetResult(
() => getCurrTime() + "|" + "part-type" + "|" + t1prtNameTxt.Text + "\n" + getCurrTime() + "|" + "operation-type" + "|" + t1oprNameTxt.Text + "\n",
() => "Part Type - " + t1prtNameTxt.Text + "\nOperation - " + t1oprNameTxt.Text
);
GetResult(
() => getCurrTime() + "|" + "part-count-good" + "|" + t1gpTxt.Text + "\n" + getCurrTime() + "|" + "part-count-bad" + "|" + t1bpTxt.Text + "\n",
() => "Good Parts - " + t1gpTxt.Text + "\nBad Parts - " + t1bpTxt.Text
);
//etcCode Snippets
public void GetResult(Func<string> generateCommand, Func<string> generateSendData) {
object[] astatus = td.CheckOk(td.t1snd);
if (astatus != null && astatus.Length > 0 && (string)astatus[0] == "true") {
string cmd = generateCommand();
string data = generateSendData();
if (MessageBox.Show(showCmd, "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) {
HandleAfterSend(td.t1snd, td.t1stp, flg, td.t1btn);
sndData(cmd,1);
}
} else {
this.ActiveControl = (TextBox)astatus[1];
MessageBox.Show(astatus[0].ToString(), "Error");
}
}GetResult(
() => getCurrTime() + "|" + "part-type" + "|" + t1prtNameTxt.Text + "\n" + getCurrTime() + "|" + "operation-type" + "|" + t1oprNameTxt.Text + "\n",
() => "Part Type - " + t1prtNameTxt.Text + "\nOperation - " + t1oprNameTxt.Text
);
GetResult(
() => getCurrTime() + "|" + "part-count-good" + "|" + t1gpTxt.Text + "\n" + getCurrTime() + "|" + "part-count-bad" + "|" + t1bpTxt.Text + "\n",
() => "Good Parts - " + t1gpTxt.Text + "\nBad Parts - " + t1bpTxt.Text
);
//etcContext
StackExchange Code Review Q#2156, answer score: 15
Revisions (0)
No revisions yet.