Generics function to work with the list of rows of Typed Dataset

I wanted to create common function, that will work with the list of rows of Typed  Dataset, e.g. List<MyTypedDataSet.MyTableDataRow). 
I've created a function that expects as a parameter List of base DataRow objects, i.e. List<DataRow>. But compiler showed error, that it unable to cast List<MyTypedDataSet.MyTableDataRow) to List<DataRow>. It does make sense, but what are the valid options?
The one, that I've used, is actually collect MyTableDataRow into the list of base DataRow records.
E.g use something like the following
List<DataRow> rows= new List<DataRow>();
rows.Add(myTableDataRow); 
 
Another option is to create generic function, where typed parameter has constraint-derived from base DataRow class.
Below are tho methods, that implement 2 possible options.
        public static void DeleteRows(List<DataRow> rows, bool hideExceptions)
        {
            foreach (DataRow row in rows)
            {
                try
                {
                    row.Delete();
                }
                catch (Exception exc)
                {
                    if (hideExceptions == true)
                    {                    //nothing to do
                        Debug.Assert(false, "Why " + exc.ToString());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        public static void DeleteRows<TRow>(List<TRow> rows, bool hideExceptions)  where TRow: DataRow
        {
            foreach (DataRow row in rows)
            {
                try
                {
                    row.Delete();
                }
                catch (Exception exc)
                {
                    if (hideExceptions == true)
                    {                    //nothing to do
                        Debug.Assert(false, "Why " + exc.ToString());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
posted @ Saturday, October 4, 2008 10:52 PM
Print

Comments on this entry:

No comments posted yet.

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234