Solved

Check if the object is List type

Posted on 2014-03-20
7
476 Views
Last Modified: 2014-03-24
Hi there,

I am writing a method in C# which will populate the listbox, combobox or checked list. I have a method  which checks that whether an object is of these types

 private bool IsListControl(object objIn)
        {
            if (objIn.GetType() == typeof(ListBox) || objIn.GetType() == typeof(CheckedListBox) || objIn.GetType() == typeof(ComboBox))
            {
                return true;
            }
            return false;
        }

Then in my other method
Public short (object objInOut)

i want to do something like
if(IsListControl(objInOut))
objInOut.Items.Clear();
objInOut.Items.Add(New ListItem(1,2))

how do i achieve this? do i need to reference a name space as i think it works fine in vb.Net
0
Comment
Question by:shah36
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39943215
is objectinOut an object?

you might be better off doing something like:

//by ref so changes stick
public void populateMyObject(ref object obj)
{
  if (obj.GetType() == typeof(ListBox) )
    {
     //cast to type, then perform actions.
      ((ListBox)obj).Items.Clear();
      ((ListBox)obj).Items.Add(new ListItem(1,2));
    }
  //do for other types
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39943360
@Kyle

The ref modifier is unnecessary.
0
 

Author Comment

by:shah36
ID: 39943727
Thanks for your replies. Actually i was thinking to make a generic method as i would need to call the method in different methods. It seems that there is now other way doing this.

regards
0
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39944506
shah, can you explain exactly what you were trying to do?

you could definitely clear the items, regardless of the type.  And if the handling was similar enough, it IS possible to do a generic function for it.
0
 

Author Comment

by:shah36
ID: 39944843
HI Kyle,

I want to populate a listbox or combobox or checkedlist using a sql query containing description and codes
A method with might be similar signatures

public short PopulateListControl(object objInOut, bool blnSetFirstEntry, string strFirstEntry, string strSql, SqlConnection conObject, bool blnIsCheckedListBoxWithTickValue = false)
        {
        }

Open in new window


I am declaring objInOut as object as the argument can be listbox or combobox or checkedlsit item and if it is one of these items

 private bool IsListItem(object ObjIn)
        {
            if (ObjIn.GetType() == typeof(ListBox) || ObjIn.GetType() == typeof(ComboBox) || ObjIn.GetType() == typeof(CheckedListBox))
            {
                return true;
            }
            return false;
        }

Open in new window


then in my method PopulateListControl I want to add / clear items from objInOut.

Actually I have started digging into object oriented programming and i am trying to write a generic library for myself for sql operations to use in all applications.

kind regards
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39945284
You're on the right track.

I've done something similiar in the past.  The best way to do that for me was to pass the object and a name of the stored proc that was responsible for getting the items.  The stored proc always returned "Val" and "Text" columns so that I could process it regardless of the object.

eg:

   select myId as Val, t_description Text from table

then regardless of the object I always knew what to bind on.


You could  build on that and add filters or what not, but for my purpose they needed each option every time.


I would make a generic method, and then from there call another populate method for the individual type . . . or just cast the object as needed.  I personally don't mind the second function call but some people do.

PopulateListControl( ... options)
{
   if (ObjIn.GetType() == typeof(ListBox))
       PopulateListBoxControl((ListBox)ObjIn, ... rest of params);  
   //next else if
}

PopulateListBoxControl(ListBox ObjIn, ...options)
{
   // handle your listbox here
}


Hope that helps.
0
 

Author Closing Comment

by:shah36
ID: 39950399
Thanks a lot yes it helped me. Would keep disturbing you guys though as now really want to explore the .Net platform however it's only start from scratch.

regards
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question