Oppinion wanted

G

Guest

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!";
}
}
}
--
(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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 
G

Guest

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.

--
(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
 
M

Mark Rae [MVP]

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?

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...
 
G

Guest

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)

--
(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
 
H

Hilton

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
 
G

Guest

non-taken at all. This is what i wanted!!
Thank you!
--
(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
 
G

Guest

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!
--
(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
 
H

Hilton

1. "Y as X" is cheaper than "(X) Y"
2. FindControl is (sometimes) called twice instead of once. That's
significant.

Hilton
 
J

Jon Skeet [C# MVP]

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
 
M

Mark Rae [MVP]

For a single operation, there's not a reproducible difference IIRC.

That's what I thought...
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.

Indeed.
 
M

marss

// 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
 
M

marss

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
 
G

Guest

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:How is this different than my example 2?


--
(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
 
G

Guest

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!!!
--
(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
 
J

Jon Skeet [C# MVP]

On Aug 31, 2:32 pm, WebBuilder451

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
 
G

Guest

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
--
(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
 
H

Hilton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top