Solved

C# Generic Method Parameter

Posted on 2010-09-20
4
533 Views
Last Modified: 2012-05-10
Hello,

Is there a way to make the method below generic?  

CustomerData is a strongly-typed dataset.  I would like to be able to somehow pass a generic strongly-typed dataset in as a parameter or however this can be done so that we only use one method for all instances.  Otherwise we will need to duplicate this method about 10 times with different dataset types.

We would also pass in the table name as string for the dataset.

Thanks.
public string[] GetCustomerSuggestionList(string columnName, string prefix, int count)

        {

 		CustomerData customerData = new CustomerData();

                customerData = GetCustomersAll();



              	EnumerableRowCollection<DataRow> query = from customer in customerData.Tables["customer"].AsEnumerable()

                        where customer.Field<string>(columnName) != null && customer.Field<string>(columnName).ToLower().StartsWith(prefix.ToLower())

                        select customer;

                     

 		UtilityBF utilityBF = new UtilityBF();

                return utilityBF.GetDistinctQueryItems(query, columnName, count);

        }

Open in new window

0
Comment
Question by:kruegerste
  • 3
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 33718590
Something like this?
public string[] GetCustomerSuggestionList<T>(T dataSet, string tableName, string columnName, string prefix, int count)
    where T : DataSet
{
    EnumerableRowCollection<DataRow> query = from customer in dataSet.Tables[tableName].AsEnumerable()
                                             where customer.Field<string>(columnName) != null && customer.Field<string>(columnName).ToLower().StartsWith(prefix.ToLower())
                                             select customer;

    UtilityBF utilityBF = new UtilityBF();
    return utilityBF.GetDistinctQueryItems(query, columnName, count);
}

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33718630
Inferred usage:
public void SomeSub()
{
    CustomerData customerData = GetCustomersAll();

    string[] result = GetCustomerSuggestionList<CustomerData>(customerData, "customer", "address", "something", 4);
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:kruegerste
ID: 33719271
Thanks.  I was missing the where statement.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 33719356
NP. Glad to help  :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

18 Experts available now in Live!

Get 1:1 Help Now