Oppinion wanted

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

    #1

    Oppinion wanted

    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_RowCr eated(object sender, GridViewRowEven tArgs e)
    {
    if (e.Row.RowType == DataControlRowT ype.DataRow)
    {
    // type 1 is more "C" Like
    if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
    ((Label)((GridV iewRow)e.Row).F indControl("lbl _One1")).Text =
    "YES!";
    // type 2 more VB like
    if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
    {
    Label lbl =
    (Label)((GridVi ewRow)e.Row).Fi ndControl("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
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Oppinion wanted

    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.



    "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
    news:E4A372BA-99E5-43B7-98B0-0CC149F49E28@mi crosoft.com...
    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_RowCr eated(object sender, GridViewRowEven tArgs e)
    {
    if (e.Row.RowType == DataControlRowT ype.DataRow)
    {
    // type 1 is more "C" Like
    if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
    ((Label)((GridV iewRow)e.Row).F indControl("lbl _One1")).Text =
    "YES!";
    // type 2 more VB like
    if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
    {
    Label lbl =
    (Label)((GridVi ewRow)e.Row).Fi ndControl("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

    Comment

    • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

      #3
      Re: Oppinion wanted

      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


      "Ignacio Machin ( .NET/ C# MVP )" wrote:
      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.
      >
      >
      >
      "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
      news:E4A372BA-99E5-43B7-98B0-0CC149F49E28@mi crosoft.com...
      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_RowCr eated(object sender, GridViewRowEven tArgs e)
      {
      if (e.Row.RowType == DataControlRowT ype.DataRow)
      {
      // type 1 is more "C" Like
      if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
      ((Label)((GridV iewRow)e.Row).F indControl("lbl _One1")).Text =
      "YES!";
      // type 2 more VB like
      if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
      {
      Label lbl =
      (Label)((GridVi ewRow)e.Row).Fi ndControl("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
      >
      >
      >

      Comment

      • Mark Rae [MVP]

        #4
        Re: Oppinion wanted

        "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
        news:B0608A4A-F502-48B5-89D9-0ECB111417FF@mi crosoft.com...
        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...


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

          #5
          Re: Oppinion wanted

          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_mas ter)Master).Fin dControl("TextB ox1")).Text =
          "YES";
          vs VB
          DirectCast(Dire ctCast(Master,
          ASP.master_mp2_ master).FindCon trol("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


          "Mark Rae [MVP]" wrote:
          "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
          news:B0608A4A-F502-48B5-89D9-0ECB111417FF@mi crosoft.com...
          >
          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...
          >
          >
          --
          Mark Rae
          ASP.NET MVP

          >
          >

          Comment

          • Hilton

            #6
            Re: Oppinion wanted

            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).Fi ndControl (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

            "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
            news:E4A372BA-99E5-43B7-98B0-0CC149F49E28@mi crosoft.com...
            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_RowCr eated(object sender, GridViewRowEven tArgs e)
            {
            if (e.Row.RowType == DataControlRowT ype.DataRow)
            {
            // type 1 is more "C" Like
            if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
            ((Label)((GridV iewRow)e.Row).F indControl("lbl _One1")).Text =
            "YES!";
            // type 2 more VB like
            if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
            {
            Label lbl =
            (Label)((GridVi ewRow)e.Row).Fi ndControl("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

            Comment

            • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

              #7
              Re: Oppinion wanted

              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


              "Hilton" wrote:
              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).Fi ndControl (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
              >
              "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
              news:E4A372BA-99E5-43B7-98B0-0CC149F49E28@mi crosoft.com...
              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_RowCr eated(object sender, GridViewRowEven tArgs e)
              {
              if (e.Row.RowType == DataControlRowT ype.DataRow)
              {
              // type 1 is more "C" Like
              if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
              ((Label)((GridV iewRow)e.Row).F indControl("lbl _One1")).Text =
              "YES!";
              // type 2 more VB like
              if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
              {
              Label lbl =
              (Label)((GridVi ewRow)e.Row).Fi ndControl("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
              >
              >
              >

              Comment

              • Mark Rae [MVP]

                #8
                Re: Oppinion wanted

                "Hilton" <nospam@nospam. comwrote in message
                news:89HBi.1265 3$3x.2785@newss vr25.news.prodi gy.net...
                it is also very inefficient.
                How so...?


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

                  #9
                  Re: Oppinion wanted

                  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


                  "Hilton" wrote:
                  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).Fi ndControl (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
                  >
                  "WebBuilder 451" <WebBuilder451@ discussions.mic rosoft.comwrote in message
                  news:E4A372BA-99E5-43B7-98B0-0CC149F49E28@mi crosoft.com...
                  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_RowCr eated(object sender, GridViewRowEven tArgs e)
                  {
                  if (e.Row.RowType == DataControlRowT ype.DataRow)
                  {
                  // type 1 is more "C" Like
                  if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
                  ((Label)((GridV iewRow)e.Row).F indControl("lbl _One1")).Text =
                  "YES!";
                  // type 2 more VB like
                  if (((GridViewRow) e.Row).FindCont rol("lbl_One1") != null)
                  {
                  Label lbl =
                  (Label)((GridVi ewRow)e.Row).Fi ndControl("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
                  >
                  >
                  >

                  Comment

                  • Hilton

                    #10
                    Re: Oppinion wanted

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

                    Hilton

                    "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                    news:u8tRsO16HH A.4348@TK2MSFTN GP06.phx.gbl...
                    "Hilton" <nospam@nospam. comwrote in message
                    news:89HBi.1265 3$3x.2785@newss vr25.news.prodi gy.net...
                    >
                    >it is also very inefficient.
                    >
                    How so...?
                    >
                    >
                    --
                    Mark Rae
                    ASP.NET MVP
                    http://www.markrae.net

                    Comment

                    • Mark Rae [MVP]

                      #11
                      Re: Oppinion wanted

                      "Hilton" <nospam@nospam. comwrote in message
                      news:0jOBi.3116 4$RX.17690@news svr11.news.prod igy.net...
                      >>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...


                      --
                      Mark Rae
                      ASP.NET MVP


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Oppinion wanted

                        On Aug 31, 9:07 am, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                        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

                        Comment

                        • Mark Rae [MVP]

                          #13
                          Re: Oppinion wanted

                          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                          news:1188548998 .774679.190070@ r23g2000prd.goo glegroups.com.. .
                          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:
                          >
                          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.


                          --
                          Mark Rae
                          ASP.NET MVP


                          Comment

                          • marss

                            #14
                            Re: Oppinion wanted

                            On 31 , 01:03, "Hilton" <nos...@nospam. comwrote:
                            // The label to show <insert comment>
                            Label label = (e.Row as GridViewRow).Fi ndControl (LABEL_BLOB) as Label;
                            >
                            A small addition. There is no need to cast e.Row to GridViewRow.

                            Label label = e.Row.FindContr ol (LABEL_BLOB) as Label;

                            Regards,
                            Mykola


                            Comment

                            • marss

                              #15
                              Re: Oppinion wanted


                              One more suggestion.
                              For a long time I did as suggested by Hilton:
                              Label label = e.Row.FindContr ol (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.FindContr ol (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,


                              Comment

                              Working...