Results 1 to 5 of 5

Thread: [RESOLVED] help checking object type

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Resolved [RESOLVED] help checking object type

    hi guys! i have a method that has one parameter of type object(Pls see the code below) and I know that only objects of a Textbox, Combox, and Listbox are the posible value that i will pass to that parameter. My question is how will i check if the object is Textbox, ComboBox or ListBox. By the way, What im trying to do is to set tha background color of the object that calls the method.

    Code:
            public static void SetCntrlBackColrEnter(object arg)
            {
    	
                //arg.BackColor = Color.Lavender;
                //arg.ForeColor = Color.Black;
            }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help checking object type

    Your argument should be type Control, not Object. That way you can simply set the ForeColor and BackColor properties without type-checking because both properties are inherited from the Control class.

    That said, if you did need to distinguish between types then you'd have several options. The best would be to overload the method, i.e. declare three methods with the same name but different argument types. The system would automatically invoke the appropriate overload for you.

    Alternatively you could test the type within the method, e.g.
    C# Code:
    1. if (arg is ListBox)
    2. {
    3.     ListBox lb = (ListBox)arg;
    4. }
    5. else if (arg is TextBox)
    6. {
    7.     TextBox tb = (TextBox)arg;
    8. }
    9. else if (arg is ComboBox)
    10. {
    11.     ComboBox cb = (ComboBox)arg;
    12. }
    That's less appropriate though because there's no point using a common method if you're going to do something different in each case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: help checking object type

    I think,your suggestion to change the type of the parameter object to control is more appropriate. Thanks again!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help checking object type

    You should always use the most specific type that you can for your arguments. In this case it's Control because that's the lowest common denominator for the types that are expected. If you were only expecting ListBoxes and ComboBoxes then it would be appropriate to declare the argument as type ListControl, which is the lowest common denominator for those two types.

    Don't forget to mark your threads as resolved when you have a solution.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: help checking object type

    Ok..Thanks for the info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width