parametize the "is" keyword

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • newcomer2k@yahoo.com

    parametize the "is" keyword

    Hi,
    I understand how to use the "is" keyword straight forward.
    like: if (obj is MyClass1)

    However, I would like to parametize the "is"'s argument. Basically, I
    would to replace MyClass1 with an argument. Here is an example:

    private void foo(WhatType arg)
    {
    foreach (Control uc in _displayPanel.C ontrols)
    {
    if (uc is arg)
    uc.Visible = true;
    else
    uc.Visible = false;
    }
    }

    And call foo like...

    foo(MyClass1);

    My trouble is, I don't know what is the "arg"'s type? i.e. what
    should "WhatType" be?
    Is this is even possible?
    Thanks for any pointer.
    Minh (MCP)

  • Terry Rogers

    #2
    Re: parametize the "is&quo t; keyword

    Perhaps you require a generic method, e.g.

    private void foo<T>(T arg)
    {
    foreach (Control uc in _displayPanel.C ontrols)
    {
    if(uc is T)
    uc.Visible = true;
    else
    uc.Visible = false;
    }
    }

    Do some research on generics for more info.


    On Jan 30, 4:42 pm, newcome...@yaho o.com wrote:
    Hi,
    I understand how to use the "is" keyword straight forward.
    like: if (obj is MyClass1)
    >
    However, I would like to parametize the "is"'s argument. Basically, I
    would to replace MyClass1 with an argument. Here is an example:
    >
    private void foo(WhatType arg)
    {
    foreach (Control uc in _displayPanel.C ontrols)
    {
    if (uc is arg)
    uc.Visible = true;
    else
    uc.Visible = false;
    }
    }
    >
    And call foo like...
    >
    foo(MyClass1);
    >
    My trouble is, I don't know what is the "arg"'s type? i.e. what
    should "WhatType" be?
    Is this is even possible?
    Thanks for any pointer.
    Minh (MCP)

    Comment

    • Horacio N. Hdez.

      #3
      Re: parametize the &quot;is&quo t; keyword

      Hi,

      You can use the typeof,
      something like this

      private void foo(Type arg)
      {
      foreach (Control uc in _displayPanel.C ontrols)
      {
      if (typeof(uc) is arg)
      uc.Visible = true;
      else
      uc.Visible = false;
      }
      }
      And you can use it:

      Type theType = typeof(bar);
      foo(theType);

      _______________ ______________
      You never know enough, specially about programming


      On 30 ene, 17:42, newcome...@yaho o.com wrote:
      Hi,
      I understand how to use the "is" keyword straight forward.
      like: if (obj is MyClass1)
      >
      However, I would like to parametize the "is"'s argument. Basically, I
      would to replace MyClass1 with an argument. Here is an example:
      >
      private void foo(WhatType arg)
      {
      foreach (Control uc in _displayPanel.C ontrols)
      {
      if (uc is arg)
      uc.Visible = true;
      else
      uc.Visible = false;
      }
      }
      >
      And call foo like...
      >
      foo(MyClass1);
      >
      My trouble is, I don't know what is the "arg"'s type? i.e. what
      should "WhatType" be?
      Is this is even possible?
      Thanks for any pointer.
      Minh (MCP)

      Comment

      • newcomer2k@yahoo.com

        #4
        Re: parametize the &quot;is&quo t; keyword

        I think typeof takes class name not an instance.
        I did tried in foo
        if (uc.GetType() == type)

        However, in my case (ASP.NET UserControl), the GetType() returns
        ASP.MyClass1 rather than just MyClass1. Hence, the comparison fail.

        Terry, I will try the generic function version later...

        On Jan 30, 11:55 am, "Horacio N. Hdez." <hnh12...@gmail .comwrote:
        Hi,
        >
        You can use the typeof,
        something like this
        >
        private void foo(Type arg)
        >
        {
        foreach (Control uc in _displayPanel.C ontrols)
        {
        if (typeof(uc) is arg)
        uc.Visible = true;
        else
        uc.Visible = false;
        }
        }And you can use it:
        >
        Type theType = typeof(bar);
        foo(theType);
        >
        _______________ ______________h ttp://elblogdehoracio .blogspot.com
        >
        On 30 ene, 17:42, newcome...@yaho o.com wrote:
        >
        >
        >
        Hi,
        I understand how to use the "is" keyword straight forward.
        like: if (obj is MyClass1)
        >
        However, I would like to parametize the "is"'s argument. Basically, I
        would to replace MyClass1 with an argument. Here is an example:
        >
        private void foo(WhatType arg)
        {
        foreach (Control uc in _displayPanel.C ontrols)
        {
        if (uc is arg)
        uc.Visible = true;
        else
        uc.Visible = false;
        }
        }
        >
        And call foo like...
        >
        foo(MyClass1);
        >
        My trouble is, I don't know what is the "arg"'s type? i.e. what
        should "WhatType" be?
        Is this is even possible?
        Thanks for any pointer.
        Minh (MCP)- Hide quoted text -- Show quoted text -

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: parametize the &quot;is&quo t; keyword

          <newcomer2k@yah oo.comwrote:
          I think typeof takes class name not an instance.
          I did tried in foo
          if (uc.GetType() == type)
          >
          However, in my case (ASP.NET UserControl), the GetType() returns
          ASP.MyClass1 rather than just MyClass1. Hence, the comparison fail.
          Have a look at Type.IsAssignab leFrom.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • newcomer2k@yahoo.com

            #6
            Re: parametize the &quot;is&quo t; keyword

            On Jan 30, 2:36 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
            <newcome...@yah oo.comwrote:
            I think typeof takes class name not an instance.
            I did tried in foo
            if (uc.GetType() == type)
            >
            However, in my case (ASP.NET UserControl), the GetType() returns
            ASP.MyClass1 rather than just MyClass1. Hence, the comparison fail.
            >
            Have a look at Type.IsAssignab leFrom.
            >
            Yeah, I think that would work too...

            Anyway, this seems to work too. Pass typeof(ASP.MyCl ass1) insteads of
            typeof(MyClass1 )

            Still would like to use the "is" keyword...:(

            Comment

            • Frans Bouma [C# MVP]

              #7
              Re: parametize the &quot;is&quo t; keyword

              newcomer2k@yaho o.com wrote:
              Hi,
              I understand how to use the "is" keyword straight forward.
              like: if (obj is MyClass1)
              >
              However, I would like to parametize the "is"'s argument. Basically,
              I would to replace MyClass1 with an argument. Here is an example:
              >
              private void foo(WhatType arg)
              {
              foreach (Control uc in _displayPanel.C ontrols)
              {
              if (uc is arg)
              uc.Visible = true;
              else
              uc.Visible = false;
              }
              }
              >
              And call foo like...
              >
              foo(MyClass1);
              >
              My trouble is, I don't know what is the "arg"'s type? i.e. what
              should "WhatType" be?

              private void foo(Type arg)
              {
              foreach (Control uc in _displayPanel.C ontrols)
              {
              uc.Visible=(uc. GetType()==arg) ;
              }
              }

              FB


              --
              ------------------------------------------------------------------------
              Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
              LLBLGen Pro website: http://www.llblgen.com
              My .NET blog: http://weblogs.asp.net/fbouma
              Microsoft MVP (C#)
              ------------------------------------------------------------------------

              Comment

              Working...