Casting question

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

    Casting question

    I have code here that checks the type of an object and then cast it so I can
    access its properties. In the simple example below I am accessing the
    ReadOnly property that is unique to textbox controls. This code works
    however I wanted to find out if its possible to write code using a
    collection or array.. see below


    Type oType = oControl.GetTyp e();
    if (oType == typeof(System.W indows.Forms.Te xtBox))

    {

    ((System.Window s.Forms.TextBox )oControl).Read Only == true;

    }

    //=============== =============== =============== =

    So the code below is kinda what I have in mind. Notice the line where I
    am attempting to cast. I know this code

    doesn't work, but I am wondering if there is a way to code such a routine.

    //=============== =============== =============== =

    private ArrayList DevControls;

    DevControls = new ArrayList();

    DevControls.Add (typeof(System. Windows.Forms.T extBox));

    DevControls.Add (typeof(System. Windows.Forms.C omboBox));

    Type oType = oControl.GetTyp e();

    for (int cnt = 0; cnt<DevControls .Count; cnt++)

    {

    if (DevControls[cnt] == oType)

    {

    // This line below is the question... is there a way to code
    this ?

    ( (DevControls[cnt]) oControl).ReadO nly = true;

    }

    }





    //=============== =============== =============== =

    --
    Thanks,

    Dan


  • Stefan Hoffmann

    #2
    Re: Casting question

    hi Dan,

    Dan Tallent wrote:
    private ArrayList DevControls;
    DevControls = new ArrayList();
    DevControls.Add (typeof(System. Windows.Forms.T extBox));
    DevControls.Add (typeof(System. Windows.Forms.C omboBox));
    Type oType = oControl.GetTyp e();
    for (int cnt = 0; cnt<DevControls .Count; cnt++)
    {
    if (DevControls[cnt] == oType)
    {
    ( (DevControls[cnt]) oControl).ReadO nly = true;
    (DevControls[cnt]) doesn't contains a control.
    Using

    DevControls.Add (new ComboBox());

    should work.


    mfG
    --stefan <--

    Comment

    • Alberto Poblacion

      #3
      Re: Casting question

      It's not exactly what you were asking, but you can achieve a similar result
      by means of System.Reflecti on:

      Type t = oControl.GetTyp e();
      PropertyInfo pi = t.GetProperty(" ReadOnly");
      if (pi!=null)
      {
      pi.SetValue(oCo ntrol, true, null);
      }



      "Dan Tallent" <spam@microsoft .comwrote in message
      news:u0ZtgElHJH A.4084@TK2MSFTN GP04.phx.gbl...
      >I have code here that checks the type of an object and then cast it so I
      >can access its properties. In the simple example below I am accessing the
      >ReadOnly property that is unique to textbox controls. This code works
      >however I wanted to find out if its possible to write code using a
      >collection or array.. see below
      >
      >
      Type oType = oControl.GetTyp e();
      if (oType == typeof(System.W indows.Forms.Te xtBox))
      >
      {
      >
      ((System.Window s.Forms.TextBox )oControl).Read Only == true;
      >
      }
      >
      //=============== =============== =============== =
      >
      So the code below is kinda what I have in mind. Notice the line where
      I am attempting to cast. I know this code
      >
      doesn't work, but I am wondering if there is a way to code such a routine.
      >
      //=============== =============== =============== =
      >
      private ArrayList DevControls;
      >
      DevControls = new ArrayList();
      >
      DevControls.Add (typeof(System. Windows.Forms.T extBox));
      >
      DevControls.Add (typeof(System. Windows.Forms.C omboBox));
      >
      Type oType = oControl.GetTyp e();
      >
      for (int cnt = 0; cnt<DevControls .Count; cnt++)
      >
      {
      >
      if (DevControls[cnt] == oType)
      >
      {
      >
      // This line below is the question... is there a way to
      code this ?
      >
      ( (DevControls[cnt]) oControl).ReadO nly = true;
      >
      }
      >
      }
      >

      Comment

      • Dan Tallent

        #4
        Re: Casting question

        I thought about using reflection, the problem I am having there is the some
        of the controls I'm working with have "sub-properites".

        Example: The reach the actual property I would write code like this.
        TextBox1.Proper ties.ReadOnly = true;

        I'm not sure how to get to the sub-properties using reflection.

        Thanks
        Dan



        "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
        in message news:u%23iqkPlH JHA.1364@TK2MSF TNGP04.phx.gbl. ..
        It's not exactly what you were asking, but you can achieve a similar
        result by means of System.Reflecti on:
        >
        Type t = oControl.GetTyp e();
        PropertyInfo pi = t.GetProperty(" ReadOnly");
        if (pi!=null)
        {
        pi.SetValue(oCo ntrol, true, null);
        }
        >
        >
        >
        "Dan Tallent" <spam@microsoft .comwrote in message
        news:u0ZtgElHJH A.4084@TK2MSFTN GP04.phx.gbl...
        >>I have code here that checks the type of an object and then cast it so I
        >>can access its properties. In the simple example below I am accessing the
        >>ReadOnly property that is unique to textbox controls. This code works
        >>however I wanted to find out if its possible to write code using a
        >>collection or array.. see below
        >>
        >>
        >Type oType = oControl.GetTyp e();
        >if (oType == typeof(System.W indows.Forms.Te xtBox))
        >>
        >{
        >>
        > ((System.Window s.Forms.TextBox )oControl).Read Only == true;
        >>
        >}
        >>
        >//=============== =============== =============== =
        >>
        >So the code below is kinda what I have in mind. Notice the line where
        >I am attempting to cast. I know this code
        >>
        >doesn't work, but I am wondering if there is a way to code such a
        >routine.
        >>
        >//=============== =============== =============== =
        >>
        >private ArrayList DevControls;
        >>
        >DevControls = new ArrayList();
        >>
        >DevControls.Ad d(typeof(System .Windows.Forms. TextBox));
        >>
        >DevControls.Ad d(typeof(System .Windows.Forms. ComboBox));
        >>
        >Type oType = oControl.GetTyp e();
        >>
        >for (int cnt = 0; cnt<DevControls .Count; cnt++)
        >>
        >{
        >>
        > if (DevControls[cnt] == oType)
        >>
        > {
        >>
        > // This line below is the question... is there a way to
        >code this ?
        >>
        > ( (DevControls[cnt]) oControl).ReadO nly = true;
        >>
        > }
        >>
        >}
        >>
        >

        Comment

        • Dan Tallent

          #5
          Re: Casting question

          Hi Stefan,

          This method doesn't work because the code won't compile. I believe the
          problem is that the DevControls[cnt] is not a known type.
          ((DevControls[cnt])oControl).Read Only = true;

          In traditional casting code like below uses a known type.
          ((System.Window s.Forms.TextBox )oControl).Read Only = true;

          I am trying to find a way where I can loop throught each control on the form
          and set its properties based on its Type.

          Thx
          Dan




          "Stefan Hoffmann" <stefan.hoffman n@explido.dewro te in message
          news:OlnsrNlHJH A.1088@TK2MSFTN GP02.phx.gbl...
          hi Dan,
          >
          Dan Tallent wrote:
          >private ArrayList DevControls;
          >DevControls = new ArrayList();
          >DevControls.Ad d(typeof(System .Windows.Forms. TextBox));
          >DevControls.Ad d(typeof(System .Windows.Forms. ComboBox));
          >Type oType = oControl.GetTyp e();
          >for (int cnt = 0; cnt<DevControls .Count; cnt++)
          >{
          > if (DevControls[cnt] == oType)
          > {
          > ( (DevControls[cnt]) oControl).ReadO nly = true;
          (DevControls[cnt]) doesn't contains a control.
          Using
          >
          DevControls.Add (new ComboBox());
          >
          should work.
          >
          >
          mfG
          --stefan <--
          >

          Comment

          • Alberto Poblacion

            #6
            Re: Casting question

            "Dan Tallent" <spam@microsoft .comwrote in message
            news:%238sGlUlH JHA.4564@TK2MSF TNGP02.phx.gbl. ..
            >I thought about using reflection, the problem I am having there is the some
            >of the controls I'm working with have "sub-properites".
            >
            Example: The reach the actual property I would write code like this.
            TextBox1.Proper ties.ReadOnly = true;
            >
            I'm not sure how to get to the sub-properties using reflection.
            No problem. Just repeat the operation twice:

            Type t1 = oControl.GetTyp e();
            PropertyInfo pi1 = t1.GetProperty( "Properties ");
            if (pi1!=null)
            {
            object prop = pi1.GetValue(oC ontrol, null);
            Type t2 = prop.GetType();
            PropertyInfo pi2 = t2.GetProperty( "ReadOnly") ;
            if (p2i!=null)
            {
            pi.SetValue(pro p, true, null);
            }
            }


            Comment

            • Peter Duniho

              #7
              Re: Casting question

              On Wed, 24 Sep 2008 07:23:42 -0700, Dan Tallent <spam@microsoft .comwrote:
              I have code here that checks the type of an object and then cast it so I
              can
              access its properties. In the simple example below I am accessing the
              ReadOnly property that is unique to textbox controls. This code works
              however I wanted to find out if its possible to write code using a
              collection or array.. see below [...]
              Since the ComboBox class doesn't have a ReadOnly property, I don't really
              understand the code example you posted. Hopefully Alberto's comments
              about reflection put you on the right track (absent a valid example, they
              seem reasonably appropriate). But if you meant something else, you may
              want to restate the example so that it makes more sense.

              Pete

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: Casting question

                On Sep 24, 10:23 am, "Dan Tallent" <s...@microsoft .comwrote:
                I have code here that checks the type of an object and then cast it so I can
                access its properties. In the simple example below I am accessing the
                ReadOnly property that is unique to textbox controls. This code works
                however I wanted to find out if its possible to write code using a
                collection or array.. see below
                >
                Type oType = oControl.GetTyp e();
                if (oType == typeof(System.W indows.Forms.Te xtBox))
                >
                {
                >
                ((System.Window s.Forms.TextBox )oControl).Read Only == true;
                >
                }
                A better approach is
                TextBox tb = oControl as TextBox;
                if ( tb!= null ){
                }

                you can use the same technique if the number of alternatives is nt too
                long.
                A more general solution would be using a dictionary:
                Dictionary< Type, stringwhere the value indicate the name of the
                property you want.
                You could have:
                < typeof(TextBox) , "readOnly"> ;
                < typeof(DropDown ), "SelectedIndex" >;

                Then you have a method that using reflection get the value based on
                the dictionary:

                object o = GetValue( instance, dcitionary[ typeof(oControl]);

                Comment

                • Ignacio Machin ( .NET/ C# MVP )

                  #9
                  Re: Casting question

                  On Sep 24, 10:52 am, "Dan Tallent" <s...@microsoft .comwrote:
                  I thought about using reflection, the problem I am having there is the some
                  of the controls I'm working with have "sub-properites".
                  >
                  Example: The reach the actual property I would write code like this.
                  TextBox1.Proper ties.ReadOnly = true;
                  >
                  I'm not sure how to get to the sub-properties using reflection.
                  >
                  Thanks
                  Dan
                  >
                  "Alberto Poblacion" <earthling-quitaestoparaco ntes...@poblaci on.orgwrote
                  in messagenews:u%2 3iqkPlHJHA.1364 @TK2MSFTNGP04.p hx.gbl...
                  >
                  It's not exactly what you were asking, but you can achieve a similar
                  result by means of System.Reflecti on:
                  >
                  Type t = oControl.GetTyp e();
                  PropertyInfo pi = t.GetProperty(" ReadOnly");
                  if (pi!=null)
                  {
                  pi.SetValue(oCo ntrol, true, null);
                  }
                  >
                  "Dan Tallent" <s...@microsoft .comwrote in message
                  news:u0ZtgElHJH A.4084@TK2MSFTN GP04.phx.gbl...
                  >I have code here that checks the type of an object and then cast it so I
                  >can access its properties. In the simple example below I am accessing the
                  >ReadOnly property that is unique to textbox controls. This code works
                  >however I wanted to find out if its possible to write code using a
                  >collection or array.. see below
                  >
                  Type oType = oControl.GetTyp e();
                  if (oType == typeof(System.W indows.Forms.Te xtBox))
                  >
                  {
                  >
                  ((System.Window s.Forms.TextBox )oControl).Read Only == true;
                  >
                  }
                  >
                  //=============== =============== =============== =
                  >
                  So the code below is kinda what I have in mind. Notice the line where
                  I am attempting to cast. I know this code
                  >
                  doesn't work, but I am wondering if there is a way to code such a
                  routine.
                  >
                  //=============== =============== =============== =
                  >
                  private ArrayList DevControls;
                  >
                  DevControls = new ArrayList();
                  >
                  DevControls.Add (typeof(System. Windows.Forms.T extBox));
                  >
                  DevControls.Add (typeof(System. Windows.Forms.C omboBox));
                  >
                  Type oType = oControl.GetTyp e();
                  >
                  for (int cnt = 0; cnt<DevControls .Count; cnt++)
                  >
                  {
                  >
                  if (DevControls[cnt] == oType)
                  >
                  {
                  >
                  // This line below is the question... is there a way to
                  code this ?
                  >
                  ( (DevControls[cnt]) oControl).ReadO nly = true;
                  >
                  }
                  >
                  }
                  I have posted code before that do that; recursively evaluate complex
                  expression like Class.SubClass1 .Subclass2.Prop erty

                  This is the relevant part fo the code:
                  int Compare( object x, object y, string comparer)
                  {
                  if ( comparer.IndexO f( ".") != -1 )
                  {
                  //split the string
                  string[] parts = comparer.Split( new char[]{ '.'} );
                  return Compare( x.GetType().Get Property( parts[0]).GetValue(x,
                  null) ,
                  y.GetType().Get Property( parts[0]).GetValue(y, null) , parts[1]
                  );
                  }
                  else
                  {

                  Comment

                  • Dan Tallent

                    #10
                    Re: Casting question

                    This seems like a good approach, but for some reason its erroring out about
                    AmbiguousMatchE xception.

                    I'm going to look into using a recursive approach as suggested by Ignacio
                    Machin A little worried about
                    the speed using recursion and reflection.

                    Should be interesting.. I'll keep you posted.
                    Dan






                    "Alberto Poblacion" <earthling-quitaestoparaco ntestar@poblaci on.orgwrote
                    in message news:%23kCk0llH JHA.1304@TK2MSF TNGP02.phx.gbl. ..
                    "Dan Tallent" <spam@microsoft .comwrote in message
                    news:%238sGlUlH JHA.4564@TK2MSF TNGP02.phx.gbl. ..
                    >>I thought about using reflection, the problem I am having there is the
                    >>some of the controls I'm working with have "sub-properites".
                    >>
                    >Example: The reach the actual property I would write code like this.
                    >TextBox1.Prope rties.ReadOnly = true;
                    >>
                    >I'm not sure how to get to the sub-properties using reflection.
                    >
                    No problem. Just repeat the operation twice:
                    >
                    Type t1 = oControl.GetTyp e();
                    PropertyInfo pi1 = t1.GetProperty( "Properties ");
                    if (pi1!=null)
                    {
                    object prop = pi1.GetValue(oC ontrol, null);
                    Type t2 = prop.GetType();
                    PropertyInfo pi2 = t2.GetProperty( "ReadOnly") ;
                    if (p2i!=null)
                    {
                    pi.SetValue(pro p, true, null);
                    }
                    }
                    >
                    >

                    Comment

                    • Dan Tallent

                      #11
                      Re: Casting question

                      This was a pretty good idea... but I ended up using this code:

                      Type t1 = oControl.GetTyp e();


                      if (t1 == typeof(DevExpre ss.XtraEditors. MemoEdit))

                      {

                      ((DevExpress.Xt raEditors.MemoE dit)oControl).P roperties.ReadO nly = ReadOnly;

                      Handled = true;

                      }

                      if (t1 == typeof(DevExpre ss.XtraEditors. TextEdit))

                      {

                      ((DevExpress.Xt raEditors.TextE dit)oControl).P roperties.ReadO nly = ReadOnly;

                      Handled = true;

                      }

                      Thanks to everyone for your help. I really wish I could of found a way to
                      use a variable in place of the actual Control, but time is short and there
                      is much to do.



                      Thanks again

                      Dan









                      "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                      message
                      news:dd1c4829-437c-4fd8-8ae3-bc82231b3baa@79 g2000hsk.google groups.com...
                      On Sep 24, 10:23 am, "Dan Tallent" <s...@microsoft .comwrote:
                      >I have code here that checks the type of an object and then cast it so I
                      >can
                      >access its properties. In the simple example below I am accessing the
                      >ReadOnly property that is unique to textbox controls. This code works
                      >however I wanted to find out if its possible to write code using a
                      >collection or array.. see below
                      >>
                      >Type oType = oControl.GetTyp e();
                      >if (oType == typeof(System.W indows.Forms.Te xtBox))
                      >>
                      >{
                      >>
                      > ((System.Window s.Forms.TextBox )oControl).Read Only == true;
                      >>
                      >}
                      >
                      A better approach is
                      TextBox tb = oControl as TextBox;
                      if ( tb!= null ){
                      }
                      >
                      you can use the same technique if the number of alternatives is nt too
                      long.
                      A more general solution would be using a dictionary:
                      Dictionary< Type, stringwhere the value indicate the name of the
                      property you want.
                      You could have:
                      < typeof(TextBox) , "readOnly"> ;
                      < typeof(DropDown ), "SelectedIndex" >;
                      >
                      Then you have a method that using reflection get the value based on
                      the dictionary:
                      >
                      object o = GetValue( instance, dcitionary[ typeof(oControl]);

                      Comment

                      • Ignacio Machin ( .NET/ C# MVP )

                        #12
                        Re: Casting question

                        On Sep 24, 3:23 pm, "Dan Tallent" <s...@microsoft .comwrote:
                        This seems like a good approach, but for some reason its erroring out about
                        AmbiguousMatchE xception.
                        >
                        I'm going to look into using a recursive approach as suggested by Ignacio
                        Machin A little worried about
                        the speed using recursion and reflection.
                        >
                        Should be interesting.. I'll keep you posted.
                        Dan
                        >
                        "Alberto Poblacion" <earthling-quitaestoparaco ntes...@poblaci on.orgwrote
                        in messagenews:%23 kCk0llHJHA.1304 @TK2MSFTNGP02.p hx.gbl...
                        >
                        "Dan Tallent" <s...@microsoft .comwrote in message
                        news:%238sGlUlH JHA.4564@TK2MSF TNGP02.phx.gbl. ..
                        >I thought about using reflection, the problem I am having there is the
                        >some of the controls I'm working with have "sub-properites".
                        >
                        Example: The reach the actual property I would write code like this.
                        TextBox1.Proper ties.ReadOnly = true;
                        >
                        I'm not sure how to get to the sub-properties using reflection.
                        >
                        No problem. Just repeat the operation twice:
                        >
                        Type t1 = oControl.GetTyp e();
                        PropertyInfo pi1 = t1.GetProperty( "Properties ");
                        if (pi1!=null)
                        {
                        object prop = pi1.GetValue(oC ontrol, null);
                        Type t2 = prop.GetType();
                        PropertyInfo pi2 = t2.GetProperty( "ReadOnly") ;
                        if (p2i!=null)
                        {
                        pi.SetValue(pro p, true, null);
                        }
                        }
                        The recursion will be just a few steps down. Not too much to worry
                        about

                        Comment

                        Working...