.NET Forum / Languages / C# / August 2007
Oppinion wanted
|
|
Thread rating:  |
WebBuilder451 - 30 Aug 2007 21:30 GMT two ways to find a label control in a gridview and set its text property. Which is the perfered way I perfer the first one, but i'm new to cs. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // type 1 is more "C" Like if (((GridViewRow)e.Row).FindControl("lbl_One1") != null) ((Label)((GridViewRow)e.Row).FindControl("lbl_One1")).Text = "YES!"; // type 2 more VB like if (((GridViewRow)e.Row).FindControl("lbl_One1") != null) { Label lbl = (Label)((GridViewRow)e.Row).FindControl("lbl_Two2"); lbl.Text = "AND Yes!"; } } }
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
Ignacio Machin ( .NET/ C# MVP ) - 30 Aug 2007 21:42 GMT Hi,
Maybe I got lost in so many () , but what is the difference between both variants?
They look the same to me, just that in the 2dn version you use a temp variable.
> two ways to find a label control in a gridview and set its text property. > Which is the perfered way I perfer the first one, but i'm new to cs. [quoted text clipped - 15 lines] > } > } WebBuilder451 - 30 Aug 2007 22:24 GMT that is the idea, they do the samething, but are they samething? Are they both way too complicated?, how would you do it? Also, is one way safer or better? given that i created a temp variable in the second does this use more memory, albeit verry little more, than the first because it's creating a temp; Or is the temp created anyway by using the cast because CLR just does it that way?
It's not C++ so i'm trying to learn what's best for this langage.
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
> Hi, > [quoted text clipped - 23 lines] > > } > > } Mark Rae [MVP] - 30 Aug 2007 22:30 GMT > that is the idea, they do the samething, but are they samething? Are they > both way too complicated?, how would you do it? [quoted text clipped - 4 lines] > does > it that way? I think it comes down to personal preference, and I don't believe that either method is appreciably more efficient than the other...
I have a personal preference for the first option...
I'm sure that other people would say that the second option is more readable...
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
WebBuilder451 - 30 Aug 2007 22:56 GMT Appreciate your response (from both of you). Upon a little work i discovered that i can do the same thing in vb.net but this style is frowned upon for it's wordiness consider: C# (please ignore the lack of error checking)
((TextBox)((ASP.master_mp2_master)Master).FindControl("TextBox1")).Text = "YES"; vs VB DirectCast(DirectCast(Master, ASP.master_mp2_master).FindControl("TextBox1"), TextBox).Text = "YES"
Most would break this up into steps, where as i would not unless i had to.
My concern is that my statements are getting too wordy, they are not too for me because i did C and C++ 15 years ago, but this is C# not C++ and i want to learn good coding practices.
Thanks!!! (I owe you both a favor)
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
> > that is the idea, they do the samething, but are they samething? Are they > > both way too complicated?, how would you do it? [quoted text clipped - 12 lines] > I'm sure that other people would say that the second option is more > readable... Hilton - 30 Aug 2007 23:03 GMT Hi,
No offense, but not only is that stuff unreadable, but it is also very inefficient. How about:
// The label to show <insert comment> Label label = (e.Row as GridViewRow).FindControl (LABEL_BLOB) as Label;
// The label was not found, we are therefore showing a non-BLOB page if (label != null) { label.Text = "Yes"; }
I would also see if the first line could be improved by looking at the details of how it is setup etc, but for now, this would be a good start.
Hilton
> two ways to find a label control in a gridview and set its text property. > Which is the perfered way I perfer the first one, but i'm new to cs. [quoted text clipped - 15 lines] > } > } WebBuilder451 - 30 Aug 2007 23:14 GMT non-taken at all. This is what i wanted!! Thank you!
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
> Hi, > [quoted text clipped - 34 lines] > > } > > } Mark Rae [MVP] - 30 Aug 2007 23:17 GMT > it is also very inefficient. How so...?
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
Hilton - 31 Aug 2007 07:12 GMT 1. "Y as X" is cheaper than "(X) Y" 2. FindControl is (sometimes) called twice instead of once. That's significant.
Hilton
>> it is also very inefficient. > > How so...? Mark Rae [MVP] - 31 Aug 2007 09:07 GMT >>> it is also very inefficient. >> >> How so...?
> 1. "Y as X" is cheaper than "(X) Y" Is it...?
> 2. FindControl is (sometimes) called twice instead of once. Maybe for something like nested repeaters, but not in this particular instance...
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
Jon Skeet [C# MVP] - 31 Aug 2007 09:29 GMT > > 1. "Y as X" is cheaper than "(X) Y" > > Is it...? For a single operation, there's not a reproducible difference IIRC.
However, the comparison is normally this:
if (foo is Bar) { Bar b = (Bar) foo; // Use b }
vs
Bar b = foo as Bar; if (b != null) { // Use b }
The latter case only needs to look at the type information once, so is quicker. The difference is very small unless you're doing this *lots* of times though.
Jon
Mark Rae [MVP] - 31 Aug 2007 10:20 GMT >> > 1. "Y as X" is cheaper than "(X) Y" >> >> Is it...? > > For a single operation, there's not a reproducible difference IIRC. That's what I thought...
> However, the comparison is normally this: > [quoted text clipped - 14 lines] > The latter case only needs to look at the type information once, so is > quicker. Indeed.
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
WebBuilder451 - 31 Aug 2007 14:32 GMT This was my point also. I'm doing a lot of single operation setting, mostly on text only. if i were setting more than one i would create an object and assign it a value.
However, my comparision
> if (((GridViewRow)e.Row).FindControl("lbl_One1") != null) is also doing a find control.
also you noted:
>>if (foo is Bar) >>{ >> Bar b = (Bar) foo; >> // Use b >>} How is this different than my example 2?
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
> > > 1. "Y as X" is cheaper than "(X) Y" > > [quoted text clipped - 23 lines] > > Jon Jon Skeet [C# MVP] - 31 Aug 2007 14:58 GMT On Aug 31, 2:32 pm, WebBuilder451 <WebBuilder...@discussions.microsoft.com> wrote:
<snip>
> How is this different than my example 2? What do you mean by "example 2"?
All I was trying to show is where "as" is faster than "is"+cast. Not a big deal though. Go for the most readable code.
Jon
WebBuilder451 - 31 Aug 2007 15:34 GMT origional posting: example 2 if (((GridViewRow)e.Row).FindControl("lbl_One1") != null) { Label lbl = (Label)((GridViewRow)e.Row).FindControl("lbl_Two2"); lbl.Text = "AND Yes!"; } But i think you may have answered the question
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
> On Aug 31, 2:32 pm, WebBuilder451 > <WebBuilder...@discussions.microsoft.com> wrote: [quoted text clipped - 9 lines] > > Jon Hilton - 31 Aug 2007 18:40 GMT >> 2. FindControl is (sometimes) called twice instead of once. > > Maybe for something like nested repeaters, but not in this particular > instance... I was referring to the OP's code:
// type 2 more VB like if (((GridViewRow)e.Row).FindControl("lbl_One1") != null) { Label lbl = (Label)((GridViewRow)e.Row).FindControl("lbl_Two2"); lbl.Text = "AND Yes!"; }
Hilton
Mark Rae [MVP] - 31 Aug 2007 19:29 GMT > I was referring to the OP's code: Apologies - I thought you were referring to the FindControl bug with nested repeaters...
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
WebBuilder451 - 30 Aug 2007 23:22 GMT followup: How is it inefficient?
if i read it correctly my way is way less readable, no question, but it's not creating an object for compairson or assignment. I'm not saying you're wrong at all, just would like it explained.
Your answer was very appreciated!
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
> Hi, > [quoted text clipped - 34 lines] > > } > > } marss - 31 Aug 2007 10:57 GMT > // The label to show <insert comment> > Label label = (e.Row as GridViewRow).FindControl (LABEL_BLOB) as Label; A small addition. There is no need to cast e.Row to GridViewRow.
Label label = e.Row.FindControl (LABEL_BLOB) as Label;
Regards, Mykola http://marss.co.ua
marss - 31 Aug 2007 13:04 GMT One more suggestion. For a long time I did as suggested by Hilton: Label label = e.Row.FindControl (LABEL_BLOB) as Label; if (label != null) label.Text = "Yes";
But recently, I refused that way and now I am writing so: Label label = e.Row.FindControl (LABEL_BLOB) as Label; label.Text = "Yes";
I think the first way is meaningful only when the code is written in a base class. In a situation where there is a certain aspx/ascx file and the corresponding code the second variant is more preferable. Why? There are two situations when the label can be equal to null: 1. LABEL_BLOB does not correspond to control id. 2. Incorrect item template in aspx file (missing label)
The end user can not cause such error. Both situations are caused by errors in the design and must be corrected at the design stage.
When we write if (label != null) ... we just hide a design error while application is executed.
Of course, my assertions may be somewhat debatable :)
Mykola, http://marss.co.ua
WebBuilder451 - 31 Aug 2007 14:38 GMT I agree and did coding this way in vb for years without a problem. In cs i started to do it just because it was suggested it was good form. However, to date i've never had an error that just poped in one day because the coutrol could not be found. It eather works the first time or i need to change it.
If i were using some dynamic controls and passing them in a objects i've found it to be necessary to do a check, to handle issues where data caused no control to be created.
Thanks!!!
 Signature (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always)
kes
> One more suggestion. > For a long time I did as suggested by Hilton: [quoted text clipped - 25 lines] > Mykola, > http://marss.co.ua Hilton - 31 Aug 2007 18:25 GMT The control might not be a label, then you get an exception. BTW: I rewrote the code without knowing too much about it, i just wanted to refine the 'obvious'.
Thanks,
Hilton
> One more suggestion. > For a long time I did as suggested by Hilton: [quoted text clipped - 25 lines] > Mykola, > http://marss.co.ua
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|