?
Solved

Check if the object is List type

Posted on 2014-03-20
7
Medium Priority
?
478 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
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!

 
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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

718 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