GetType()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ron

    #1

    GetType()

    Hi,
    I am currently using the base class GetType() in the
    following way and was woundering if there was a different
    way of looking at types and doing a comparison for
    equality.

    Thanks Ron

    foreach(System. Web.UI.Control ctl in this.Controls)
    {
    if(ctl.GetType( ).ToString()
    == "System.Web.UI. WebControls.Dro pDownList")
    {
    DropDownList ddl = (DropDownList)c tl.FindControl
    ("ddlAddNewLoca tion");
    if(ddl != null)
    {
    locationId = int.Parse(ddl.S electedItem.Val ue);
    }
    }
    }
  • Greg Ewing [MVP]

    #2
    Re: GetType()

    ron, you have a couple of options. To keep a similar flow you could use the
    typeof() operator instead of .GetType().

    I would recommend you check out the is keyword though. With is you can do
    something like this:

    foreach (...)
    {
    if (ctl is System.Web.UI.W ebControls.Drop DownList)
    {
    DropDownList ddl = (DropDownList)c tl;
    //do your work on the dropdown list
    }
    }

    --
    Greg Ewing [MVP]
    IT support built exclusively for nonprofits. 25 years of managed IT, help desk, and cybersecurity - so your team can focus on your mission.



    "ron" <anonymous@disc ussions.microso ft.com> wrote in message
    news:0a4b01c39e ed$b90c61a0$a50 1280a@phx.gbl.. .[color=blue]
    > Hi,
    > I am currently using the base class GetType() in the
    > following way and was woundering if there was a different
    > way of looking at types and doing a comparison for
    > equality.
    >
    > Thanks Ron
    >
    > foreach(System. Web.UI.Control ctl in this.Controls)
    > {
    > if(ctl.GetType( ).ToString()
    > == "System.Web.UI. WebControls.Dro pDownList")
    > {
    > DropDownList ddl = (DropDownList)c tl.FindControl
    > ("ddlAddNewLoca tion");
    > if(ddl != null)
    > {
    > locationId = int.Parse(ddl.S electedItem.Val ue);
    > }
    > }
    > }[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: GetType()

      ron,

      You should use this instead:

      // Check to see if it is a drop down list.
      if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))

      The typeof operator will get the type defined in parenthesis for you.
      This is good because it doesn't force you to have the full name of the type
      (version, etc, etc).

      Hope this helps.

      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "ron" <anonymous@disc ussions.microso ft.com> wrote in message
      news:0a4b01c39e ed$b90c61a0$a50 1280a@phx.gbl.. .[color=blue]
      > Hi,
      > I am currently using the base class GetType() in the
      > following way and was woundering if there was a different
      > way of looking at types and doing a comparison for
      > equality.
      >
      > Thanks Ron
      >
      > foreach(System. Web.UI.Control ctl in this.Controls)
      > {
      > if(ctl.GetType( ).ToString()
      > == "System.Web.UI. WebControls.Dro pDownList")
      > {
      > DropDownList ddl = (DropDownList)c tl.FindControl
      > ("ddlAddNewLoca tion");
      > if(ddl != null)
      > {
      > locationId = int.Parse(ddl.S electedItem.Val ue);
      > }
      > }
      > }[/color]


      Comment

      • Ray Hsieh (Ray Djajadinata)

        #4
        Re: GetType()

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Hi Ron,

        if(ctl.GetType( ) == typeof(System.W eb.UI.WebContro ls.DropDownList )) { ... }

        That way you can have the compiler check that
        "System.Web.UI. WebControls.Dro pDownList" indeed exist, instead of having
        to make sure that you've typed it correctly :)

        ron wrote:

        | Hi,
        | I am currently using the base class GetType() in the
        | following way and was woundering if there was a different
        | way of looking at types and doing a comparison for
        | equality.
        |
        | Thanks Ron
        |
        | foreach(System. Web.UI.Control ctl in this.Controls)
        | {
        | if(ctl.GetType( ).ToString()
        | == "System.Web.UI. WebControls.Dro pDownList")
        | {
        | DropDownList ddl = (DropDownList)c tl.FindControl
        | ("ddlAddNewLoca tion");
        | if(ddl != null)
        | {
        | locationId = int.Parse(ddl.S electedItem.Val ue);
        | }
        | }
        | }


        - --
        Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
        ray underscore usenet at yahoo dot com
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.3 (MingW32)
        Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

        iD8DBQE/oSQWwEwccQ4rWPg RAg4gAJ47gDw/foxCVP4OY61V0km ruyDL5gCfaNjO
        akZ/PKyLrb7XRtkftQA TDZ4=
        =ZUEM
        -----END PGP SIGNATURE-----

        Comment

        • ron

          #5
          GetType()

          Thanks that clears things up better.
          Ron
          [color=blue]
          >-----Original Message-----
          >Hi,
          >I am currently using the base class GetType() in the
          >following way and was woundering if there was a[/color]
          different[color=blue]
          >way of looking at types and doing a comparison for
          >equality.
          >
          >Thanks Ron
          >
          >foreach(System .Web.UI.Control ctl in this.Controls)
          >{
          > if(ctl.GetType( ).ToString()
          >== "System.Web.UI. WebControls.Dro pDownList")
          > {
          > DropDownList ddl = (DropDownList)c tl.FindControl
          >("ddlAddNewLoc ation");
          > if(ddl != null)
          > {
          > locationId = int.Parse(ddl.S electedItem.Val ue);
          > }
          > }
          >}
          >.
          >[/color]

          Comment

          • Greg Ewing [MVP]

            #6
            Re: GetType()

            So Nicholas and Ray, what do you guys have against the is operator? <g> is
            seems to be a whole lot faster than GetType and typeof(). Here's what I
            found for 1000 iterations:

            Using the is operator took 00:00:00.000025 1
            Using GetType() took 00:00:00.000205 3

            Here's the code I used. It is using Whidbey so that might affect things but
            I soft of doubt it would make GetType 10 times slower like I saw.

            [STAThread]
            static void Main(string[] args)
            {
            Control ctl = new DropDownList();
            Stopwatch sw = new Stopwatch();
            int j = 0;
            sw.Start();
            for (int i = 0; i < 1000; i++)
            {
            if (ctl is System.Web.UI.W ebControls.Drop DownList)
            {
            j++;
            //Console.WriteLi ne("ctl is a DropDownList");
            }
            else
            {
            j++;
            //Console.WriteLi ne("ctl is NOT a DropDownList");
            }
            }
            sw.Stop();
            Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
            sw.Start();
            for (int i = 0; i < 1000; i++)
            {
            if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
            {
            j++;
            //Console.WriteLi ne("ctl is a DropDownList");
            }
            else
            {
            j++;
            //Console.WriteLi ne("ctl is NOT a DropDownList");
            }
            }
            sw.Stop();
            Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
            Console.Read();
            }

            --
            Greg Ewing [MVP]
            IT support built exclusively for nonprofits. 25 years of managed IT, help desk, and cybersecurity - so your team can focus on your mission.





            "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
            message news:eqyJqBvnDH A.3700@TK2MSFTN GP11.phx.gbl...[color=blue]
            > ron,
            >
            > You should use this instead:
            >
            > // Check to see if it is a drop down list.
            > if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
            >
            > The typeof operator will get the type defined in parenthesis for you.
            > This is good because it doesn't force you to have the full name of the[/color]
            type[color=blue]
            > (version, etc, etc).
            >
            > Hope this helps.
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - mvp@spam.guard. caspershouse.co m
            >
            > "ron" <anonymous@disc ussions.microso ft.com> wrote in message
            > news:0a4b01c39e ed$b90c61a0$a50 1280a@phx.gbl.. .[color=green]
            > > Hi,
            > > I am currently using the base class GetType() in the
            > > following way and was woundering if there was a different
            > > way of looking at types and doing a comparison for
            > > equality.
            > >
            > > Thanks Ron
            > >
            > > foreach(System. Web.UI.Control ctl in this.Controls)
            > > {
            > > if(ctl.GetType( ).ToString()
            > > == "System.Web.UI. WebControls.Dro pDownList")
            > > {
            > > DropDownList ddl = (DropDownList)c tl.FindControl
            > > ("ddlAddNewLoca tion");
            > > if(ddl != null)
            > > {
            > > locationId = int.Parse(ddl.S electedItem.Val ue);
            > > }
            > > }
            > > }[/color]
            >
            >[/color]


            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: GetType()

              Greg,

              Since you have so much time on your hands, perhaps you should test the
              following as well:

              if ((ctl as System.Web.UI.W ebControls.Drop DownList) != null)
              {
              j++;
              //Console.WriteLi ne("ctl is a DropDownList");
              }
              else
              {
              j++;
              //Console.WriteLi ne("ctl is NOT a DropDownList");
              }

              <g>

              Also, you might want to provide numbers using VS.NET 2003, as Whidbey is
              not available to the general public =)

              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m


              "Greg Ewing [MVP]" <gewing@_NO_SPA M_citidc.com> wrote in message
              news:OOxSIYvnDH A.3732@tk2msftn gp13.phx.gbl...[color=blue]
              > So Nicholas and Ray, what do you guys have against the is operator? <g>[/color]
              is[color=blue]
              > seems to be a whole lot faster than GetType and typeof(). Here's what I
              > found for 1000 iterations:
              >
              > Using the is operator took 00:00:00.000025 1
              > Using GetType() took 00:00:00.000205 3
              >
              > Here's the code I used. It is using Whidbey so that might affect things[/color]
              but[color=blue]
              > I soft of doubt it would make GetType 10 times slower like I saw.
              >
              > [STAThread]
              > static void Main(string[] args)
              > {
              > Control ctl = new DropDownList();
              > Stopwatch sw = new Stopwatch();
              > int j = 0;
              > sw.Start();
              > for (int i = 0; i < 1000; i++)
              > {
              > if (ctl is System.Web.UI.W ebControls.Drop DownList)
              > {
              > j++;
              > //Console.WriteLi ne("ctl is a DropDownList");
              > }
              > else
              > {
              > j++;
              > //Console.WriteLi ne("ctl is NOT a DropDownList");
              > }
              > }
              > sw.Stop();
              > Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
              > sw.Start();
              > for (int i = 0; i < 1000; i++)
              > {
              > if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
              > {
              > j++;
              > //Console.WriteLi ne("ctl is a DropDownList");
              > }
              > else
              > {
              > j++;
              > //Console.WriteLi ne("ctl is NOT a DropDownList");
              > }
              > }
              > sw.Stop();
              > Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
              > Console.Read();
              > }
              >
              > --
              > Greg Ewing [MVP]
              > http://www.citidc.com/
              >
              >
              >
              >
              > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
              in[color=blue]
              > message news:eqyJqBvnDH A.3700@TK2MSFTN GP11.phx.gbl...[color=green]
              > > ron,
              > >
              > > You should use this instead:
              > >
              > > // Check to see if it is a drop down list.
              > > if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
              > >
              > > The typeof operator will get the type defined in parenthesis for[/color][/color]
              you.[color=blue][color=green]
              > > This is good because it doesn't force you to have the full name of the[/color]
              > type[color=green]
              > > (version, etc, etc).
              > >
              > > Hope this helps.
              > >
              > > --
              > > - Nicholas Paldino [.NET/C# MVP]
              > > - mvp@spam.guard. caspershouse.co m
              > >
              > > "ron" <anonymous@disc ussions.microso ft.com> wrote in message
              > > news:0a4b01c39e ed$b90c61a0$a50 1280a@phx.gbl.. .[color=darkred]
              > > > Hi,
              > > > I am currently using the base class GetType() in the
              > > > following way and was woundering if there was a different
              > > > way of looking at types and doing a comparison for
              > > > equality.
              > > >
              > > > Thanks Ron
              > > >
              > > > foreach(System. Web.UI.Control ctl in this.Controls)
              > > > {
              > > > if(ctl.GetType( ).ToString()
              > > > == "System.Web.UI. WebControls.Dro pDownList")
              > > > {
              > > > DropDownList ddl = (DropDownList)c tl.FindControl
              > > > ("ddlAddNewLoca tion");
              > > > if(ddl != null)
              > > > {
              > > > locationId = int.Parse(ddl.S electedItem.Val ue);
              > > > }
              > > > }
              > > > }[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Ray Hsieh (Ray Djajadinata)

                #8
                Re: GetType()

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                I thought he wanted the type to be exact, since I was just translating
                his first line into how I would do it. But after seeing your reply, I
                read Ron's code further down, and I realized that:

                1. Ron apparently just wanted to check if he could cast it to a DropDownList
                2. The DropDownList is already at the bottom of the hierarchy, so "is"
                will give him a true only if the instance is indeed a DropDownList anyway...

                So yeah, "is" does satisfy the requirements, and much more concise to
                type :)

                About the performance thing (where did you get this StopWatch class?
                Seems quite useful), how about putting the typeof() outside the loop?
                Guess that'll make a difference! :)

                Greg Ewing [MVP] wrote:
                | So Nicholas and Ray, what do you guys have against the is operator?
                <g> is
                | seems to be a whole lot faster than GetType and typeof(). Here's what I
                | found for 1000 iterations:
                |
                | Using the is operator took 00:00:00.000025 1
                | Using GetType() took 00:00:00.000205 3
                |
                | Here's the code I used. It is using Whidbey so that might affect
                things but
                | I soft of doubt it would make GetType 10 times slower like I saw.
                |
                | [STAThread]
                | static void Main(string[] args)
                | {
                | Control ctl = new DropDownList();
                | Stopwatch sw = new Stopwatch();
                | int j = 0;
                | sw.Start();
                | for (int i = 0; i < 1000; i++)
                | {
                | if (ctl is System.Web.UI.W ebControls.Drop DownList)
                | {
                | j++;
                | //Console.WriteLi ne("ctl is a DropDownList");
                | }
                | else
                | {
                | j++;
                | //Console.WriteLi ne("ctl is NOT a DropDownList");
                | }
                | }
                | sw.Stop();
                | Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
                | sw.Start();
                | for (int i = 0; i < 1000; i++)
                | {
                | if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
                | {
                | j++;
                | //Console.WriteLi ne("ctl is a DropDownList");
                | }
                | else
                | {
                | j++;
                | //Console.WriteLi ne("ctl is NOT a DropDownList");
                | }
                | }
                | sw.Stop();
                | Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
                | Console.Read();
                | }
                |


                - --
                Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
                ray underscore usenet at yahoo dot com
                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.2.3 (MingW32)
                Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

                iD8DBQE/oTP4wEwccQ4rWPg RAnSWAJ9jRyA0E+ qpCiLpcY8Ap9fty +DoJgCdFKKo
                JeJ3RhBNh9zYoMm lUOdmXrw=
                =U+E1
                -----END PGP SIGNATURE-----

                Comment

                • Greg Ewing [MVP]

                  #9
                  Re: GetType()

                  OK, here are the results with the as operator for you Nicholas.

                  Using the is operator took 00:00:00.000024 0
                  Using GetType() took 00:00:00.000236 3
                  Using the as operator took 00:00:00.000239 4

                  Why not VS.Net 2003? Cause I'm lazy and Whidbey is the only thing I have on
                  my laptop. <g> I expect that the only difference is that there is no
                  stopwatch class on 2003. I don't think the type checking will be any
                  different. I'll check out VS.Net 2003 tonight if I have time. I've
                  attached the code if someone else wants to make it work for VS.Net 2003
                  before then.

                  --
                  Greg Ewing [MVP]
                  IT support built exclusively for nonprofits. 25 years of managed IT, help desk, and cybersecurity - so your team can focus on your mission.



                  "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                  message news:#2Z$3jvnDH A.2772@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > Greg,
                  >
                  > Since you have so much time on your hands, perhaps you should test the
                  > following as well:
                  >
                  > if ((ctl as System.Web.UI.W ebControls.Drop DownList) != null)
                  > {
                  > j++;
                  > //Console.WriteLi ne("ctl is a DropDownList");
                  > }
                  > else
                  > {
                  > j++;
                  > //Console.WriteLi ne("ctl is NOT a DropDownList");
                  > }
                  >
                  > <g>
                  >
                  > Also, you might want to provide numbers using VS.NET 2003, as Whidbey[/color]
                  is[color=blue]
                  > not available to the general public =)
                  >
                  > --
                  > - Nicholas Paldino [.NET/C# MVP]
                  > - mvp@spam.guard. caspershouse.co m
                  >
                  >
                  > "Greg Ewing [MVP]" <gewing@_NO_SPA M_citidc.com> wrote in message
                  > news:OOxSIYvnDH A.3732@tk2msftn gp13.phx.gbl...[color=green]
                  > > So Nicholas and Ray, what do you guys have against the is operator? <g>[/color]
                  > is[color=green]
                  > > seems to be a whole lot faster than GetType and typeof(). Here's what I
                  > > found for 1000 iterations:
                  > >
                  > > Using the is operator took 00:00:00.000025 1
                  > > Using GetType() took 00:00:00.000205 3
                  > >
                  > > Here's the code I used. It is using Whidbey so that might affect things[/color]
                  > but[color=green]
                  > > I soft of doubt it would make GetType 10 times slower like I saw.
                  > >
                  > > [STAThread]
                  > > static void Main(string[] args)
                  > > {
                  > > Control ctl = new DropDownList();
                  > > Stopwatch sw = new Stopwatch();
                  > > int j = 0;
                  > > sw.Start();
                  > > for (int i = 0; i < 1000; i++)
                  > > {
                  > > if (ctl is System.Web.UI.W ebControls.Drop DownList)
                  > > {
                  > > j++;
                  > > //Console.WriteLi ne("ctl is a DropDownList");
                  > > }
                  > > else
                  > > {
                  > > j++;
                  > > //Console.WriteLi ne("ctl is NOT a DropDownList");
                  > > }
                  > > }
                  > > sw.Stop();
                  > > Console.WriteLi ne("Using the is operator took " + sw.Elapsed);
                  > > sw.Start();
                  > > for (int i = 0; i < 1000; i++)
                  > > {
                  > > if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
                  > > {
                  > > j++;
                  > > //Console.WriteLi ne("ctl is a DropDownList");
                  > > }
                  > > else
                  > > {
                  > > j++;
                  > > //Console.WriteLi ne("ctl is NOT a DropDownList");
                  > > }
                  > > }
                  > > sw.Stop();
                  > > Console.WriteLi ne("Using GetType() took " + sw.Elapsed);
                  > > Console.Read();
                  > > }
                  > >
                  > > --
                  > > Greg Ewing [MVP]
                  > > http://www.citidc.com/
                  > >
                  > >
                  > >
                  > >
                  > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
                  > in[color=green]
                  > > message news:eqyJqBvnDH A.3700@TK2MSFTN GP11.phx.gbl...[color=darkred]
                  > > > ron,
                  > > >
                  > > > You should use this instead:
                  > > >
                  > > > // Check to see if it is a drop down list.
                  > > > if (ctl.GetType() == typeof(System.W eb.UI.WebContro ls.DropDownList ))
                  > > >
                  > > > The typeof operator will get the type defined in parenthesis for[/color][/color]
                  > you.[color=green][color=darkred]
                  > > > This is good because it doesn't force you to have the full name of the[/color]
                  > > type[color=darkred]
                  > > > (version, etc, etc).
                  > > >
                  > > > Hope this helps.
                  > > >
                  > > > --
                  > > > - Nicholas Paldino [.NET/C# MVP]
                  > > > - mvp@spam.guard. caspershouse.co m
                  > > >
                  > > > "ron" <anonymous@disc ussions.microso ft.com> wrote in message
                  > > > news:0a4b01c39e ed$b90c61a0$a50 1280a@phx.gbl.. .
                  > > > > Hi,
                  > > > > I am currently using the base class GetType() in the
                  > > > > following way and was woundering if there was a different
                  > > > > way of looking at types and doing a comparison for
                  > > > > equality.
                  > > > >
                  > > > > Thanks Ron
                  > > > >
                  > > > > foreach(System. Web.UI.Control ctl in this.Controls)
                  > > > > {
                  > > > > if(ctl.GetType( ).ToString()
                  > > > > == "System.Web.UI. WebControls.Dro pDownList")
                  > > > > {
                  > > > > DropDownList ddl = (DropDownList)c tl.FindControl
                  > > > > ("ddlAddNewLoca tion");
                  > > > > if(ddl != null)
                  > > > > {
                  > > > > locationId = int.Parse(ddl.S electedItem.Val ue);
                  > > > > }
                  > > > > }
                  > > > > }
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]




                  Comment

                  Working...