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
Solved

Check if the object is List type

Posted on 2014-03-20
7
468 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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