0

Is there a way to turn these two lines of code into a single line of

TextBox tempTextBox= grdvwEncroachmentsID.Rows[e.RowIndex].FindControl("txtbxBillTo") as TextBox;
string billToTemp = tempTextBox.Text;
kevorski
  • 816
  • 1
  • 11
  • 29
  • 1
    Hmm, a programmer that has not debugged a NullReferenceException often enough would ask this. The result is not "better", it is not more diagnosable, not more readable, not more efficient. And it is wrong, never use `as` when you meant to use a `(cast)`. – Hans Passant Nov 09 '17 at 18:00

3 Answers3

2

Just delete the Line Break!

Just kidding. I assume you mean single statement.

Here you go:

string billToTemp = (grdvwEncroachmentsID.Rows[e.RowIndex].FindControl("txtbxBillTo") as TextBox).Text;

Not sure why you would do this, though. It's ugly.

Clay07g
  • 1,105
  • 7
  • 23
  • What do you mean by ugly? The length of the line of code? – kevorski Nov 09 '17 at 17:23
  • 1
    Yes. It makes it harder to read. And it gives you less room to declare your intentions. Also, if you ever want to re-use that TextBox variable, you will either have to declare a new one or put it into a variable (like you have in your question). – Clay07g Nov 09 '17 at 17:24
  • Thank you. That is what I was thinking. – kevorski Nov 09 '17 at 17:25
  • 1
    +1 for sense of humour. The Elvis operator ?. can be used to avoid NullReference nasties. Fluent alignment can assist with readability. – StuartLC Nov 09 '17 at 18:21
1

Use parentheses:

string billToTemp = (grdvwEncroachmentsID.Rows[e.RowIndex].FindControl("txtbxBillTo") as TextBox).Text;
itsme86
  • 19,266
  • 4
  • 41
  • 57
0

Try this

string billToTemp = (grdvwEncroachmentsID.Rows[e.RowIndex].FindControl("txtbxBillTo") as TextBox).Text;
kevorski
  • 816
  • 1
  • 11
  • 29
Munna
  • 1
  • 1