Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 251293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:34:56+00:00 2026-05-11T21:34:56+00:00

I am having trouble casting an object to a generic IList. I have a

  • 0

I am having trouble casting an object to a generic IList. I have a group of in statements to try to work around this, but there has to be a better way to do this.

This is my current method:

string values;

if (colFilter.Value is IList<int>)
{
    values = BuildClause((IList<int>)colFilter.Value, prefix);
}
else if (colFilter.Value is IList<string>)
{
    values = BuildClause((IList<string>)colFilter.Value, prefix);
}
else if (colFilter.Value is IList<DateTime>)
{
    values = BuildClause((IList<DateTime>)colFilter.Value, prefix);
}
else if (...) //etc.

What I want to do is this:

values = BuildClause((IList<colFilter.ColumnType>)colFilter.Value, prefix);

or

values = BuildClause((IList<typeof(colFilter.ColumnType)>)colFilter.Value, prefix);

or

values = BuildClause((IList<colFilter.ColumnType.GetType()>)colFilter.Value, prefix);

Each of these produces this compiler error:
The type or namespace name ‘colFilter’ could not be found (are you missing a using directive or an assembly reference?)

In my example, colFilter.ColumnType is int, string, datetime, etc. I am not sure why this does not work.

Any ideas?

EDIT: This is C#2.0

EDIT #2

Here is the BuildClause method (I have overloads for each type):

private static string BuildClause(IList<int> inClause, string strPrefix)
{
    return BuildClause(inClause, strPrefix, false);
}

private static string BuildClause(IList<String> inClause, string strPrefix)
{
    return BuildClause(inClause, strPrefix, true);
}

private static string BuildClause(IList<DateTime> inClause, string strPrefix)
{
    return BuildClause(inClause, strPrefix, true);
}
//.. etc for all types

private static string BuildClause<T>(IList<T> inClause, string strPrefix, bool addSingleQuotes)
    {
        StringBuilder sb = new StringBuilder();

        //Check to make sure inclause has objects
        if (inClause.Count > 0)
        {
            sb.Append(strPrefix);
            sb.Append(" IN(");

            for (int i = 0; i < inClause.Count; i++)
            {
                if (addSingleQuotes)
                {
                    sb.AppendFormat("'{0}'", inClause[i].ToString().Replace("'", "''"));
                }
                else
                {
                    sb.Append(inClause[i].ToString());
                }

                if (i != inClause.Count - 1)
                {
                    sb.Append(",");
                }
            }

            sb.Append(") ");
        }
        else
        {
            throw new Exception("Item count for In() Clause must be greater than 0.");
        }

        return sb.ToString();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:34:57+00:00Added an answer on May 11, 2026 at 9:34 pm

    There’s no way to relate method overloading and generics: although they look similar, they are very different. Specifically, overloading lets you do different things based on the type of arguments used; while generics allows you to do the exact same thing regardless of the type used.

    If your BuildClause method is overloaded and every overload is doing something different (not just different by the type used, but really different logic, in this case – choosing whether or not to add quotes) then somewhere, ultimately, you’re gonna have to say something like “if type is this do this, if type is that do that” (I call that “switch-on-type”).

    Another approach is to avoid that “switch-on-type” logic and replace it with polymorphism. Suppose you had a StringColFilter : ColFilter<string> and a IntColFilter : ColFilter<int>, then each of them could override a virtual method from ColFilter<T> and provide its own BuildClause implementation (or just some piece of data that would help BuildClause process it). But then you’d need to explicitly create the correct subtype of ColFilter, which just moves the “switch-on-type” logic to another place in your application. If you’re lucky, it’ll move that logic to a place in your application where you have the knowledge of which type you’re dealing with, and then you could explicitly create different ColFilters at different places in your application and process them generically later on.

    Consider something like this:

    abstract class ColFilter<T> 
    {
        abstract bool AddSingleQuotes { get; }
        List<T> Values { get; }
    }
    
    class IntColFilter<T>
    {
        override bool AddSingleQuotes { get { return false; } }    
    }
    
    class StringColFilter<T>
    {
        override bool AddSingleQuotes { get { return true; } }
    }
    
    class SomeOtherClass 
    {
        public static string BuildClause<T>(string prefix, ColFilter<T> filter)
        {
            return BuildClause(prefix, filter.Values, filter.AddSingleQuotes);
        }
    
        public static string BuildClause<T>(string prefix, IList<T> values, bool addSingleQuotes) 
        {
            // use your existing implementation, since here we don't care about types anymore -- 
            // all we do is call ToString() on them.
            // in fact, we don't need this method to be generic at all!
        }
    }
    

    Of course this also gets you to the problem of whether ColFilter should know about quotes or not, but that’s a design issue and deserves another question 🙂

    I also stand by the other posters in saying that if you’re trying to build something that creates SQL statements by joining strings together, you should probably stop doing it and move over to parameterized queries which are easier and, more importantly, safer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 254k
  • Answers 254k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Excel versions before 2007 have a standard palette, and given… May 13, 2026 at 10:07 am
  • Editorial Team
    Editorial Team added an answer The page jumping to the top is usually a sign… May 13, 2026 at 10:07 am
  • Editorial Team
    Editorial Team added an answer I usually see this a seperate entities as MembershipUser revolves… May 13, 2026 at 10:07 am

Related Questions

I am writing a SQL Function that will take in a decimal and return
I am writing an application where we will need to extend a basic entity
I am currently exploring the specification of the Digital Mars D language, and am
I have a python script that is writing text to images using the PIL.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.