Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if the object is List type

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

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 41

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 41

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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