Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Extension Methods
Message
From
27/01/2010 12:40:28
 
 
To
27/01/2010 10:07:16
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01446025
Message ID:
01446199
Views:
29
>Part of the reason I used a gazillion overloads in my methods is so that casting to the proper type wasn't necessary ... but that's not the point of your suggestion, just a point I wanted to make.
>
>You've got a great idea though ... I could change my code to be extensions of DataRow instead of object. I like that.

It was just your mention of being annoyed by having the extension method show up in IntelliSense for object types that started me thinking.... :-}

Thanks for the idea ... looks like I'll have to change my blog yet again!
>
>~~Bonnie
>
>
>
>>>No, sorry Kevin, I don't currently have any downloading capabilities on my blog. I haven't bothered yet to figure out if I can do it or not from blogspot. Probably can, but I don't know for sure.
>>>
>>>BTW, the main thing that I *don't* like about those extension methods is that all those GetNonNull() methods are extensions for "object" (remember, the methods were originally intended to work with values in DataTable columns) ... consequently, Intellisense "picks up" those methods for *everything* (since everything is an object). I find it annoying.
>>
>>Would something like this have worked as a more 'generic' approach:
    public static class BBExtensions
>>    {
>>        public static object GetNonNull(this System.Data.DataRow dataRow, string columnName, object useValue)
>>        {
>>            if (dataRow.Table.Columns[columnName] == null)
>>                throw new Exception("Column does not belong to Table");
>>
>>            var v = dataRow[columnName];
>>            if (v != DBNull.Value) return v;
>>
>>            System.Data.DataColumn dc = dataRow.Table.Columns[columnName];
>>
>>            if (useValue != null)
>>            {
>>                if (dc.DataType != useValue.GetType())
>>                {
>>                    throw new Exception("Invalid type for default");
>>                }
>>                return useValue;
>>            }
>>            else
>>            {
>>                if (dc.DataType == typeof(string))
>>                    return "";
>>                else if (dc.DataType == typeof(DateTime))
>>                    return DateTime.Now;
>>                //  etc for other types
>>                else throw new Exception("No default supplied for " + dc.DataType.ToString());
>>            }
>>        }
>>
>>        public static object GetNonNull(this System.Data.DataRow dataRow,string columnName)
>>        {
>>            return GetNonNull(dataRow, columnName, null);
>>        }
>>    }
Usage then becomes:
DateTime date = (DateTime) dt.Rows[0].GetNonNull("MyDateColumn", DateTime.Now);
>>string s = (string)dt.Rows[0].GetNonNull("MyStringColumn");
>>
>>
>>>
>>>~~Bonnie
>>>
>>>
>>>
>>>
>>>>Bonnie,
>>>>
>>>>I noticed that you updated your blog with a new version of your extension methods. Looks great. When I try to copy the code, it all comes out as one line. No breaks/formatting.
>>>>
>>>>Do you have this class in a download anywhere?
>>>>
>>>>
>>>>Thanks
>>>>K
Previous
Reply
Map
View

Click here to load this message in the networking platform