Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Monday, January 02, 2006 - Posts

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( ByVal val As T)
      HttpContext.Current.Response.Write(val)
   
End
Sub
End
Class

C#
using System.Data;
using System.Web;

public

class GenericClass where T : DataTable
{
   
public
T GetValue(T val)
   {
      
return
val;
   }

   public DataTable GetValue(T val)
   {
      
return
val;
   }

   public void GetValue(T val)
   {
      
HttpContext
.Current.Response.Write(val);
   }
}

A: At first glance, the overloaded methods appear to be unique. However, this is not the case. In VB.NET, the first 2 methods would error and in C# the last 2 methods would error. Why does this happen? Like non-generic methods, your generic methods must be unique by their method signatures, not just by their return types.

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Jan 02 2006, 03:44 PM by Ryan Olshan | with no comments
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 Function
   End Class
End Class

C#
public class OuterClass
{
    public class InnerClass
   {
       public  V InnerMethod(T val1, U val2)
      {
          // Some code here
      }
   }
}

The above code is perfectly legal and will compile. The question is which class will T and U in InnerMethod inherit its type references from? The answer is the class which the method is contained in, in the case of the example provided above, InnerClass. However, you should never name the types of a sub class the same as those of the class it is contained in. If you get in the habbit of using strongly named types, you will avoid possible future problems.

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Jan 02 2006, 12:55 AM by Ryan Olshan | with no comments
Filed under:
More Posts