Solved

C# Generic Method Parameter

Posted on 2010-09-20
4
545 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 75

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 75

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 75

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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