Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    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

.NET Websites

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

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • 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
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

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

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 class ClassA : BaseClass { }

public class ClassB : ClassA { }

public class GenericClass : ClassB { }

 

public class NonInheritingClass where T : BaseClass

{

   private List < GenericClass > m_list;

 

   public virtual int AddToList( GenericClass baseObject)

   {

      if ( this .m_list == null )

      {

        this .m_list = new List < GenericClass >();

      }

 

      this .m_list.Add(baseObject);

 

      return m_list.Count;

   }

}

 

public class SomeClass

{

   public static int TestMethod()

   {

      int count;

 

      NonInheritingClass < ClassB > nonInheritingClass = new NonInheritingClass < ClassB >();

      GenericClass < ClassB > classB = new GenericClass < ClassB >();

 

      count = nonInheritingClass.AddToList(classB);

 

      return count;

   }

}

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Friday, March 03, 2006 12:27 AMby Ryan Olshan
Filed under:

Comments

No Comments

New Comments to this post are disabled