0

Hi guys, probably a simple one. Using C# .Net 4.0 and Visual Studio 2012 Ultimate.

Got the following code:

string part = "";
part = txtIOpart.Text;
txtBatchCV.Text = txtBatchIO.Text;
txtPartCV.Text = part;
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg);
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal();
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS();
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal();

txtBarCV.Text = "*" + Sqlrunclass.SplitInfo_ASno(part, pg) + "*";
txtBarNumCV.Text = txtBarCV.Text;
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location();
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc();
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height();
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter();
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit();

picTypeCV.Image = ftpclass.Download("CVspecType" + Sqlrunclass.SplitSpec_TypeCV() + ".jpg", "ftp.shaftec.com/Images/TypeJpg", "0095845|shafteccom0", "4ccc7365d4");

if (txtBatchCV.Text == null || txtBatchCV.Text == "")
{
    txtBatchCV.Text = "ALL";
}

As you can see at the bottom I'm checking the batch, but I need to check all of the data thats being set by a bunch of methods. Each one will have a different txt output if it sees a null or blank txt. Is there anyway to shorten this code?

Maurice Reeves
  • 1,572
  • 13
  • 19
lemunk
  • 2,616
  • 12
  • 57
  • 87

6 Answers6

3

Try, txtBatchCV.Text For example

//Just for null
txtBatchCV.Text = (txtBatchCV.Text ?? "ALL").ToString(); 

//for both null and empty string
txtBatchCV.Text = string.IsNullOrEmpty(txtBatchCV.Text) ? "ALL": txtBatchCV.Text; 
Kaf
  • 33,101
  • 7
  • 58
  • 78
3

You could iterate through all the textboxes

foreach (var txt in form.Controls.OfType<TextBox>())
{
    switch(txt.Id){
        case "txtBatchCV":
        // Do whatever you want for txtBatchCV e.g. check string.IsNullOrEmpy(txt.Text)
        break;
    }
}

I borrowed the above from here:

How do I loop through all textboxes and make them run corresponding actions from action dictionary?

In response to the comment I got from Tim, I've added a bit more code to explain what you could do. My code example was never meant to be a full solution.

Community
  • 1
  • 1
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
1

For starters you could use string.IsNullOrEmpty(txtBatchCV.Text), it's a convevience method that basically does what you do in the if check.

Destrictor
  • 752
  • 1
  • 4
  • 15
1

I would try something like this:

void SetDefaultIfNull(TextBox txt, string defaultVal)
{
    if (string.IsNullOrWhitespace(txt.Text))
        txt.Text = defaultVal;
}

Then pass each textbox and the default to the method.

Maurice Reeves
  • 1,572
  • 13
  • 19
1

You can atleast use one of these methods:

string.IsNullOrEmpty(txtBatchCV.Text)

or

string.IsNullOrWhitespace(txtBatchCV.Text)

Marius
  • 1,053
  • 2
  • 7
  • 16
1

TextBox.Text is never null, it will return "" then. If your methods return null you could use the null-coalescing operator:

string nullRepl = "ALL";
txtExternalCV.Text = Sqlrunclass.SplitSpec_External(part, pg) ?? nullRepl;
txtInternalCV.Text = Sqlrunclass.SplitSpec_Internal() ?? nullRepl;
txtABSCV.Text = Sqlrunclass.SplitSpec_cvABS() ?? nullRepl;
txtOilCV.Text = Sqlrunclass.SplitSpec_OilSeal() ?? nullRepl;
txtLocnCV.Text = Sqlrunclass.SplitInfo_Location() ?? nullRepl;
txtFitsCV.Text = Sqlrunclass.SplitInfo_Desc() ?? nullRepl;
txtHeightCV.Text = Sqlrunclass.SplitSpec_Height() ?? nullRepl;
txtDiameterCV.Text = Sqlrunclass.SplitSpec_Diameter() ?? nullRepl;
txtCirclitCV.Text = Sqlrunclass.SplitSpec_Circlit() ?? nullRepl;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thats pretty nice never used that operator before, ill give it a try mate thanks! – lemunk Dec 14 '12 at 14:28
  • what if the method return "" – lemunk Dec 14 '12 at 15:18
  • @StevenSmith: Then you can't use the `??`-operator. – Tim Schmelter Dec 14 '12 at 15:22
  • yeh just checked whats being returned, since its a string thats being returned it will always be "", hmm seemed nice aswell instead of having to then check the text boxes. im wandering if it would be better to do the check in the method itself in the other class then pass something like "0" through#? – lemunk Dec 14 '12 at 15:26