Posts tagged 'net-generics'
Strong Coders Generic Data Access Library
Until recently, I was using a modified version of the below code that was SQL Server-based for light weight data access. I've since introduced .NET generics to make it database independent. The below code is perfect for smaller projects where a component such as Enterprise Library or IBatis might be...
Continue reading "Strong Coders Generic Data Access Library"
Generic ExecuteScalar method
Just wanted to share with everyone the generic helper method I use for ExecuteScalar. A helper method for ExecuteScalar is the perfect candidate for generics due to the different types that could be returned. Of course, you could always return an object, but this is more fun. :) public static T ExecuteScalar<T>...
Continue reading "Generic ExecuteScalar method"
Free .NET Generics Help from the Experts
Group ( Of Experts) Generics is one of the most noted changes to the .NET Framework 2.0. Generics will reduce overhead due to boxing/unboxing and the amount of errors in your code . Want to learn about .NET Generics but don't know where to start? Join StrongTypes and get your questions answered by our...
Continue reading "Free .NET Generics Help from the Experts"
XML Generics
Inheritence with Generics
I answered a post on the ASP.NET forums where somebody was having problems with Generics and class inheritence. I ended up cooking this up as an example based upon what they had already provided in the thread. using System; using System.Collections.Generic; public abstract class BaseClass { } public...
Continue reading "Inheritence with Generics"
Nullable type to string function
I just answered a thread where somebody wanted to know how to take any nullable type, check if it's value is null, and depending on that either return String.Empty or ToString. Here's how you do that: public static string ConvertNullableToString<T>(T? value) where T : struct { return value.ToString...
Continue reading "Nullable type to string function"
Nullable types and typed DataSets
Earlier today I answered a question on a group where some guy was trying to use nullable types with a typed DataSet. Unfortunately, this feature was cut out at the last moment. But, there is a way around this. Adding Functionality to a Typed DataSet using Partial Classes...
Continue reading "Nullable types and typed DataSets"
Generic List Collection / ObjectDataSource / GridView Sample
I put together sample code that will run on the Northwind database that demonstrates using a generic List collection as a data source for an ObjectDataSource which populates a GridView control. All you have to do is specify the connection string in Web.config and the sample code will run. Enjoy. Download...
Continue reading "Generic List Collection / ObjectDataSource / GridView Sample"
A somewhat type-safe String.Format
Using String.Format you can format any string. However, the last parameter that String.Format accepts is of type object. So, to add some type-safety to the mix, the following function works perfectly. VB.NET Public Function FormatString( Of T)( ByVal originalString As String , ByVal replacement As T...
Continue reading "A somewhat type-safe String.Format"
Creating a List of a custom class
Let's say for instance, you want to create a List of your own class, say Employee, where Employee represents an employee. First, we'll start by creating an employee class. [ Serializable ] public class Employee { private bool m_IsSalaried; private int m_Age; private string m_Email; private string m_Name;...
Continue reading "Creating a List of a custom class"
Interesting Generic Blog Entries
I was surfing the Internet today and came across David Wheeler's blog, which has some interesting entries on Generics. Anonymous methods and handling events in .NET 2.0 Generics: An introduction Generics: More advanced concepts New and improved events...
Continue reading "Interesting Generic Blog Entries"
What does the ` character stand for?
Take the following code, for example: VB.NET Public Class TestClass( Of T) End Class Response.Write(GetType(TestClass( Of Integer ))) C# public class TestClass <T> { } Response.Write( typeof ( TestClass < int >)); The above code results in the following output: TestClass`1[System.Int32]....
Continue reading "What does the ` character stand for?"
What's wrong with this generic class?
Q: What's wrong with the below generic class? VB.NET Imports System.Data Imports System.Web Public Class GenericClass( Of T As DataTable) Public Function GetValue( ByVal val As T) As T Return val End Function Public Function GetValue( ByVal val As T) As DataTable Return val End Function Public Sub GetValue...
Continue reading "What's wrong with this generic class?"
Generic Sub Classes
In generics, you can use sub classes just as you would any non-generic sub class. Take the following class and sub class for example: VB.NET Public Class OuterClass( Of T, U, V) Public Class InnerClass( Of T, U) Public Function InnerMethod( ByVal val1 As T, ByVal val2 As U) As V ' Some code here End...
Continue reading "Generic Sub Classes"
Nullable Types
One of the weirder features in C# 2.0 is nullable types. My thinking is as follows: Why complicate things with Nullable<T>, or its shorter version of DataType? (i.e. int?), instead of allowing null values to be assigned to those data types that don't have any way of handling null? Maybe it was...
Continue reading "Nullable Types"
Generic Type Inference
One of the cooler features I like in Generics is type inference. Type inference allows you to call a generic method just as you would a non-generic method using its method signatures as references to the type(s), instead of specifying the type(s). VB.NET Public Sub TestMethod1( Of T, U)( ByVal val1 As...
Continue reading "Generic Type Inference"
What's wrong with this generic type?
I love mysteries. However, this one is pretty simple. Q: What is wrong with the following generic type? VB.NET Public Class BaseClass( Of K, V) End Class Public Class SubClass1 Inherits BaseClass( Of K, V) End Class Public Class SubClass2 Inherits BaseClass( Of Integer , Long ) End Class C# public class...
Continue reading "What's wrong with this generic type?"
What sets C++ Templates and .NET Generics apart?
C++ Templates and .NET Generics are not the same. There are many differences between them, but the biggest thing that sets them apart is that C++ Templates are instantiated at compile-time while Generics are instantiated at run-time. Since C++ Templates are instantiated at compile-time, a copy of the...
Continue reading "What sets C++ Templates and .NET Generics apart?"
Parametric Polymorphism
What is parametric polymorphism? It sounds like such a cool phrase. Parametric polymorphism is a function or datatype that can be written generically so that it can deal equally well with any objects without depending on their type. .NET Generics are derived from the principles of parametric polymorphism...
Continue reading "Parametric Polymorphism"
Generics and the BLL
Here is an article that has an example of using Generics with a BLL: http://blogs.wwwcoder.com/amachin/archive/2005/02/28/1993.aspx R...
Continue reading "Generics and the BLL"
Generic type parameter variance in the CLR
Here's another cool .NET Generics blog entry. This one is about Generic type parameter variance in the CLR: http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx . R...
Continue reading "Generic type parameter variance in the CLR"
Limitations of Generics Polymorphism
Here is an interesting blog entry on the limitations of Generics polymorphism: http://www.geekswithblogs.net/michelotti/archive/2005/12/05/62273.aspx . R...
Continue reading "Limitations of Generics Polymorphism"
.NET Generic Books
Right now there are only two books out there that cover Generics, and out of the two only one is dedicated to Generics. I ordered Professional .NET 2.0 Generics last week and it should be arriving in the mail any day now. I am in the process of reading Juval Lowy's book. As I finish the books, I will...
Continue reading ".NET Generic Books"
Generic Collections Article
C# Generics List Collection Article
I just published a new article on LearnAsp.com that demonstrates using a Generics List Collection in C# to store the retrieval of all folders, subfolder, and folders within subfolders. Folders File System Article R...
Continue reading "C# Generics List Collection Article"
StrongTypes Group
Group <StrongTypes>( Of Experts) I have created a Yahoo! group called StrongTypes. The focus of StrongTypes is to discuss and provide free, friendly, expert help in the area of .NET Generics. Visit StrongTypes and get your .NET Generics questions answered by a Group of Experts today....
Continue reading "StrongTypes Group"
ST is here
ST is almost here...
Generics ExecuteScalar Helper
Declaring a typed parameter as null
A typed parameter can be set to a value of null using the default keyword as illustrated below: T val; val = default (T); Visit http://msdn2.microsoft.com/en-us/library/xwth0h0d.aspx for more information on the default keyword. R...
Continue reading "Declaring a typed parameter as null"
ST is coming
Excellent Generics Link
Generics
Today I began my venture into Generics. Very interesting subject matter. Below are a few links of interest: http://weblogs.java.net/blog/arnold/archive/2005/06/generics_consid_1.html http://www.artima.com/intv/generics.html http://hinchcliffe.org/archive/2005/01/07/146.aspx http://www.andymcm.com/dotnetfaq...
Continue reading "Generics"




