Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development
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 on Saturday, October 4, 2008 10:52 PM | Back to top


Comments on this post: Generics function to work with the list of rows of Typed Dataset

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free