Solved

Check if the object is List type

Posted on 2014-03-20
7
464 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 39

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 74

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 39

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 39

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now