<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Joacim's view on stuff : visual basic, generics</title><link>http://msmvps.com/blogs/joacim/archive/tags/visual+basic/generics/default.aspx</link><description>Tags: visual basic, generics</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>The Untouchables (part II) – Creating an Immutable stack with VB</title><link>http://msmvps.com/blogs/joacim/archive/2009/09/07/the-untouchables-part-ii-creating-an-immutable-stack-with-vb.aspx</link><pubDate>Mon, 07 Sep 2009 01:02:03 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1721263</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1721263</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/09/07/the-untouchables-part-ii-creating-an-immutable-stack-with-vb.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In this article I will shamelessly “steal” the great work originally posted by &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx" target="_blank"&gt;Eric Lippert on his blog&lt;/a&gt;. However I will translate his code into VB and try to explain it from a VB developers point of view.&lt;/p&gt;  &lt;p&gt;In my &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/09/06/the-untouchables-creating-immutable-objects-with-vb.aspx"&gt;last post&lt;/a&gt;, I described what immutable objects are and what the advantage of using them would be. It also described how you could create an immutable class and use it as if it was value type. This time we are going to take this a step further and create an immutable stack.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;An immutable stack, is that possible?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As Eric pointed out on his blog: This might sound a bit strange to you since a stack is by its very nature something that changes. You push stuff to it and pop things from it, last-in-first-out. So how can something like that be immutable? If we create a new stack object each time we push or pop something to/from the stack wouldn’t we then be allocating a great deal of memory? Well, in this case the answer is nope. Eric showed a very elegant way of saving memory, in some cases it might even be more efficient than a mutable version.&lt;/p&gt;  &lt;p&gt;The idea is that you simply have a &lt;em&gt;head &lt;/em&gt;object which is the last value or reference object (depending on what you’re storing on the stack) that was push onto the stack, and a &lt;em&gt;tail&lt;/em&gt; which is a reference to the old stack. The state the stack was before the last item was pushed upon it.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Creating the Stack&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As I’ve already mentioned, I did not create this code, all credit goes to &lt;a href="http://blogs.msdn.com/ericlippert/default.aspx" target="_blank"&gt;Eric Lippert&lt;/a&gt;. All I have done is to translate it to VB, except for one detail. Eric implemented his stack as an &lt;em&gt;IEnumerable(Of T), &lt;/em&gt;taking advantage of the &lt;em&gt;iterator&lt;/em&gt; support that C# has with its &lt;font color="#0000ff"&gt;yield&lt;/font&gt; keyword. Since VB currently doesn’t support that I simply didn’t implement it here. In the next part of this &lt;strong&gt;&lt;em&gt;The Untouchables&lt;/em&gt;&lt;/strong&gt; series I will however extend this stack class with support for iterators. As you will see it currently requires a whole lot more code in VB than it does in C#. If you’re interesting in learning more about how to use iterators in VB now, I would recommend that you read &lt;a href="http://visualstudiomagazine.com/Articles/2009/02/01/Use-Iterators-in-VB-Now.aspx" target="_blank"&gt;Bill McCarthy’s excellent article&lt;/a&gt; about the subject. But for now we just leave them out.&lt;/p&gt;  &lt;p&gt;OK, onward! We are going to implement our stack using generics, so that you can use it to push any type onto the stack. The first thing we will do is to define an interface for our class.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Interface &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
  &lt;span style="color:blue;"&gt;Function &lt;/span&gt;Push(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As &lt;/span&gt;T) &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
  &lt;span style="color:blue;"&gt;Function &lt;/span&gt;Pop() &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
  &lt;span style="color:blue;"&gt;Function &lt;/span&gt;Peek() &lt;span style="color:blue;"&gt;As &lt;/span&gt;T
  &lt;span style="color:blue;"&gt;ReadOnly Property &lt;/span&gt;IsEmpty() &lt;span style="color:blue;"&gt;As Boolean
End Interface&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you have ever used a stack object before, you are probably accustomed that the Pop() method would return the value it just removed from the stack. In this case it doesn’t it simply returns another stack object without the value that was just popped. I must say that I agree with Eric here, that &lt;em&gt;looking&lt;/em&gt; at the data should not require you to &lt;em&gt;change&lt;/em&gt; it. A Pop which returns the value is a design flaw since it conflates two logically distinct and separate operations into one method. So in our case we use the Peek() method to look at the top value, and Pop() to remove it.&lt;/p&gt;

&lt;p&gt;Our Stack class which will implement the above interface will have a constructor, but we will keep that private. You will get a new Stack object by calling the Push() and Pop() methods. However we also need a way to create an empty stack, otherwise we have nothing that we can Push or Pop anything to or from. Since every empty stack is the same, we will have a singleton empty stack that is created via a shared (static) method that we simply call &lt;em&gt;Empty&lt;/em&gt;(). So we will be creating a new empty stack object (of strings in this example) in this manner:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myStack &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;) = Stack(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;).Empty&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Note that the &lt;em&gt;myStack&lt;/em&gt; object above is not declared as Stack(Of String) but rather as our interface IStack(Of String). This is because our Empty() method returns an IStack(Of T) object that contains an instance to a private inner class called &lt;em&gt;EmptyStack&lt;/em&gt; that like our Stack(Of T) class also implements the IStack(Of T) interface.&lt;/p&gt;

&lt;p&gt;OK, here’s the full listing of the class.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public NotInheritable Class &lt;/span&gt;Stack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
  &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)

  &lt;span style="color:blue;"&gt;Private NotInheritable Class &lt;/span&gt;EmptyStack
    &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)

    &lt;span style="color:blue;"&gt;Public ReadOnly Property &lt;/span&gt;IsEmpty() &lt;span style="color:blue;"&gt;As Boolean _&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).IsEmpty
      &lt;span style="color:blue;"&gt;Get
        Return True
      End Get
    End Property

    Public Function &lt;/span&gt;Peek() &lt;span style="color:blue;"&gt;As &lt;/span&gt;T &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).Peek
      &lt;span style="color:blue;"&gt;Throw New &lt;/span&gt;Exception(&lt;span style="color:#a31515;"&gt;&amp;quot;Empty stack&amp;quot;&lt;/span&gt;)
    &lt;span style="color:blue;"&gt;End Function

    Public Function &lt;/span&gt;Pop() &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T) &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).Pop
      &lt;span style="color:blue;"&gt;Throw New &lt;/span&gt;Exception(&lt;span style="color:#a31515;"&gt;&amp;quot;Empty stack&amp;quot;&lt;/span&gt;)
    &lt;span style="color:blue;"&gt;End Function

    Public Function &lt;/span&gt;Push(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As &lt;/span&gt;T) &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T) _&lt;br /&gt;        &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).Push
      &lt;span style="color:blue;"&gt;Return New &lt;/span&gt;Stack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)(value, &lt;span style="color:blue;"&gt;Me&lt;/span&gt;)
    &lt;span style="color:blue;"&gt;End Function&lt;/span&gt;&lt;span style="color:blue;"&gt;
  End Class

  Private ReadOnly &lt;/span&gt;_head &lt;span style="color:blue;"&gt;As &lt;/span&gt;T
  &lt;span style="color:blue;"&gt;Private ReadOnly &lt;/span&gt;_tail &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)

  &lt;span style="color:blue;"&gt;Private Shared ReadOnly &lt;/span&gt;_empty &lt;span style="color:blue;"&gt;As New &lt;/span&gt;EmptyStack
  &lt;span style="color:blue;"&gt;Public Shared Function &lt;/span&gt;Empty() &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
    &lt;span style="color:blue;"&gt;Return &lt;/span&gt;_empty
  &lt;span style="color:blue;"&gt;End Function

  Public ReadOnly Property &lt;/span&gt;IsEmpty() &lt;span style="color:blue;"&gt;As Boolean _&lt;br /&gt;      Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).IsEmpty
    &lt;span style="color:blue;"&gt;Get
      Return False
    End Get
  End Property

  Public Function &lt;/span&gt;Peek() &lt;span style="color:blue;"&gt;As &lt;/span&gt;T &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).Peek
    &lt;span style="color:blue;"&gt;Return &lt;/span&gt;_head
  &lt;span style="color:blue;"&gt;End Function

  Public Function &lt;/span&gt;Pop() &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T) &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).Pop
    &lt;span style="color:blue;"&gt;Return &lt;/span&gt;_tail
  &lt;span style="color:blue;"&gt;End Function

  Public Function &lt;/span&gt;Push(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As &lt;/span&gt;T) &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T) _&lt;br /&gt;      &lt;span style="color:blue;"&gt;Implements &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T).Push
    &lt;span style="color:blue;"&gt;Return New &lt;/span&gt;Stack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)(value, &lt;span style="color:blue;"&gt;Me&lt;/span&gt;)
  &lt;span style="color:blue;"&gt;End Function

  Private Sub New&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;head &lt;span style="color:blue;"&gt;As &lt;/span&gt;T, &lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;tail &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T))
    _head = head
    _tail = tail
  &lt;span style="color:blue;"&gt;End Sub
&lt;/span&gt;&lt;span style="color:blue;"&gt;End Class&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice how each time we Push a new value onto the Stack we call our private constructor that create a new Stack object. However our &lt;em&gt;_tail &lt;/em&gt;will simply contain a reference to the old stack, that way we conserve memory resources. Since more than one stack object can share the same tail it will actually be more effective than a regular mutable version of a Stack object.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;s1 &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of Integer&lt;/span&gt;) = Stack(&lt;span style="color:blue;"&gt;Of Integer&lt;/span&gt;).Empty
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;s2 &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of Integer&lt;/span&gt;) = s1.Push(10)
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;s3 &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of Integer&lt;/span&gt;) = s2.Push(20)
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;s4 &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of Integer&lt;/span&gt;) = s2.Push(30) &lt;span style="color:green;"&gt;&amp;#39;shares its tail with s3&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Remember that the Stack object is immutable so pushing a value onto the stack doesn’t change it. Instead it returns a new Stack object containing the newly pushed value. The following code will raise an exception “&lt;em&gt;Empty Stack”&lt;/em&gt; since &lt;em&gt;myStack &lt;/em&gt;is still empty.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myStack &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;) = Stack(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;).Empty()
myStack.Push(&lt;span style="color:#a31515;"&gt;&amp;quot;Hello&amp;quot;&lt;/span&gt;)&lt;br /&gt;Console.WriteLine(myStack.Peek) &lt;span style="color:green;"&gt;&amp;#39;raises an exception since myStack is empty&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That is like calling the &lt;em&gt;Replace()&lt;/em&gt; method of a string without assigning the return value to anything. It will not have changed the immutable string. The correct way of doing the above would be this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myStack &lt;span style="color:blue;"&gt;As &lt;/span&gt;IStack(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;) = Stack(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;).Empty()
myStack = myStack.Push(&lt;span style="color:#a31515;"&gt;&amp;quot;Hello&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;&amp;#39;change the reference of myStack
&lt;/span&gt;Console.WriteLine(myStack.Peek) &lt;span style="color:green;"&gt;&amp;#39;writes &amp;quot;Hello&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;OK, next time I’m going to extend our Stack class to add support for iterators, so that we can use a For Each loop on our stack object. So stay tuned…&lt;/p&gt;

&lt;p&gt;Until then, have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1721263" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/generics/default.aspx">generics</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/immutable/default.aspx">immutable</category></item><item><title>Go for the Generic brand…</title><link>http://msmvps.com/blogs/joacim/archive/2009/09/02/go-for-the-generic-brand.aspx</link><pubDate>Wed, 02 Sep 2009 15:05:33 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1720175</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1720175</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/09/02/go-for-the-generic-brand.aspx#comments</comments><description>&lt;p&gt;Before .Net 2.0 the only way that you could use a generic type was to use System.Object. Although it’s been 4 years since the release of .Net 2.0 I still see developers using the System.Object &lt;em&gt;way&lt;/em&gt; too often, which leads to unnecessary &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx"&gt;boxing/unboxing and casting&lt;/a&gt;. So in this article I’m going to explain how you can use Generics in your classes and methods.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What are Generics?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I entitled this article “Go for the Generic brand” because outside the world of programming the term &lt;em&gt;generic&lt;/em&gt; means something that is not tied to a particular brand name. You may buy a bottle of shampoo without it having one of the more recognizable names printed on its label but you can still use it and expect it to clean your hair. Generics in programming works in a similar manner, you can refer to a class without it being related to a particular Type, but we can still use it in a type-safe manner.&lt;/p&gt;  &lt;p&gt;However unlike generic brands of merchandises, that often are thought of as inferior to well known household brands, Generics in .Net are &lt;em&gt;usually&lt;/em&gt; a good thing. I said &lt;em&gt;usually a good thing&lt;/em&gt; because you should not look at generics as a way to replace other techniques such as method overloading, but rather as a way to replace the usage of System.Object.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;A quick example&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Below is a small example on what a generic class could look like.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;MyGenericClass(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
  &lt;span style="color:blue;"&gt;Private &lt;/span&gt;_value &lt;span style="color:blue;"&gt;As &lt;/span&gt;T
  &lt;span style="color:blue;"&gt;Public Property &lt;/span&gt;Value() &lt;span style="color:blue;"&gt;As &lt;/span&gt;T
    &lt;span style="color:blue;"&gt;Get
      Return &lt;/span&gt;_value
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As &lt;/span&gt;T)
      _value = value
    &lt;span style="color:blue;"&gt;End Set
  End Property
End Class&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The (Of T) part after the class name tells the compiler that this is a generic class. The T itself is merely a placeholder of a type, any type. The name T doesn’t really matter, you could just as well have typed (Of MyType) if you wanted to, however it has become a general convention to just use the capital T, unless the class expects more than one type.&lt;/p&gt;

&lt;p&gt;In C# the same class would look like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyGenericClass&lt;/span&gt;&amp;lt;T&amp;gt;
{
  &lt;span style="color:blue;"&gt;private &lt;/span&gt;T _value;
  &lt;span style="color:blue;"&gt;public &lt;/span&gt;T Value
  {
    &lt;span style="color:blue;"&gt;get&lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;_value; }
    &lt;span style="color:blue;"&gt;set&lt;/span&gt;{ _value = &lt;span style="color:blue;"&gt;value&lt;/span&gt;;}
  }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;C# uses the angle brackets instead of the &lt;font color="#0000ff"&gt;Of&lt;/font&gt; keyword as VB does, but other than that they work exactly in the same manner.&lt;/p&gt;

&lt;p&gt;So let’s see how this class can be used. When we create a new object from the class we must provide the type we want to use.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myString &lt;span style="color:blue;"&gt;As New &lt;/span&gt;MyGenericClass(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;)
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myInteger &lt;span style="color:blue;"&gt;As New &lt;/span&gt;MyGenericClass(&lt;span style="color:blue;"&gt;Of Integer&lt;/span&gt;)
myString.Value = &lt;span style="color:#a31515;"&gt;&amp;quot;Hello World&amp;quot;
&lt;/span&gt;myInteger.Value = 123
Console.WriteLine(myString.Value)
Console.WriteLine(myInteger.Value)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here I create two instances of the &lt;em&gt;MyGenericClass&lt;/em&gt; one of the String type and the other of the Integer type. The Value property will then become of that particular type, so trying to do the following would create a compile error (or a runtime error if you have Option Strict set to Off) since I’m trying to assign a string value to an integer type.&lt;/p&gt;

&lt;pre class="code"&gt;myInteger.Value = &lt;span style="color:#a31515;"&gt;&amp;quot;Hello&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This generic class is in no way restricted to only primitives and strings, you could use it with any type. Below I use a StringBuilder.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myStringBuilder &lt;span style="color:blue;"&gt;As New &lt;/span&gt;MyGenericClass(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;StringBuilder)
myStringBuilder.Value = &lt;span style="color:blue;"&gt;New &lt;/span&gt;StringBuilder
myStringBuilder.Value.AppendLine(&lt;span style="color:#a31515;"&gt;&amp;quot;A string&amp;quot;&lt;/span&gt;)
myStringBuilder.Value.AppendLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Another string&amp;quot;&lt;/span&gt;)
Console.WriteLine(myStringBuilder.Value)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;Generic Methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The whole class doesn’t have to use generics, you can use generics on a single method as well.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Function &lt;/span&gt;CreateArray(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;size &lt;span style="color:blue;"&gt;As Integer&lt;/span&gt;) &lt;span style="color:blue;"&gt;As &lt;/span&gt;T()
  &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;array(size) &lt;span style="color:blue;"&gt;As &lt;/span&gt;T
  &lt;span style="color:blue;"&gt;Return &lt;/span&gt;array
&lt;span style="color:blue;"&gt;End Function

Public Sub &lt;/span&gt;Main()
  &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myIntArray &lt;span style="color:blue;"&gt;As Integer&lt;/span&gt;() = CreateArray(&lt;span style="color:blue;"&gt;Of Integer&lt;/span&gt;)(5)
  &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myStringArray &lt;span style="color:blue;"&gt;As String&lt;/span&gt;() = CreateArray(&lt;span style="color:blue;"&gt;Of String&lt;/span&gt;)(5)
&lt;span style="color:blue;"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;This can be especially useful for &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/08/24/nifty-extension-methods.aspx"&gt;extension methods&lt;/a&gt;, when you want to add a generic method to any type. 

&lt;p&gt;&lt;strong&gt;Type constraints&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even though the code in a generic type definition should be as type-independent as possible it is sometimes useful to add some constraints to limit the number of types that are possible. A good example is if you for example need to compare items to be able to sort them in which case they must implement the IComparable interface.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;MyComparer(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T &lt;span style="color:blue;"&gt;As &lt;/span&gt;IComparable)
  &lt;span style="color:green;"&gt;&amp;#39;Insert code...
&lt;/span&gt;&lt;span style="color:blue;"&gt;End Class&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Apart from interfaces the type of constraints you can use are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The type must be of a type of, or inherit from, a class. &lt;/li&gt;

  &lt;li&gt;The type must expose a parameterless constructor. &lt;/li&gt;

  &lt;li&gt;The type must be a reference type, or it must be a value type. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s have a look at the first, the the type must be of, or inherit from, a specified class. Let’s say you have the following classes:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;Identity
  &lt;span style="color:blue;"&gt;Private &lt;/span&gt;_id &lt;span style="color:blue;"&gt;As Integer
  Public Property &lt;/span&gt;ID() &lt;span style="color:blue;"&gt;As Integer
    Get
      Return &lt;/span&gt;_id
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As Integer&lt;/span&gt;)
      _id = value
    &lt;span style="color:blue;"&gt;End Set
  End Property
End Class

Public Class &lt;/span&gt;Person
  &lt;span style="color:blue;"&gt;Inherits &lt;/span&gt;Identity

  &lt;span style="color:blue;"&gt;Private &lt;/span&gt;_firstName &lt;span style="color:blue;"&gt;As String
  Public Property &lt;/span&gt;FirstName() &lt;span style="color:blue;"&gt;As String
    Get
      Return &lt;/span&gt;_firstName
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As String&lt;/span&gt;)
      _firstName = value
    &lt;span style="color:blue;"&gt;End Set
  End Property

  Private &lt;/span&gt;_lastName &lt;span style="color:blue;"&gt;As String
  Public Property &lt;/span&gt;LastName() &lt;span style="color:blue;"&gt;As String
    Get
      Return &lt;/span&gt;_lastName
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As String&lt;/span&gt;)
      _lastName = value
    &lt;span style="color:blue;"&gt;End Set
  End Property
End Class

Public Class &lt;/span&gt;Company
  &lt;span style="color:blue;"&gt;Inherits &lt;/span&gt;Identity

  &lt;span style="color:blue;"&gt;Private &lt;/span&gt;_name &lt;span style="color:blue;"&gt;As String
  Public Property &lt;/span&gt;Name() &lt;span style="color:blue;"&gt;As String
    Get
      Return &lt;/span&gt;_name
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As String&lt;/span&gt;)
      _name = value
    &lt;span style="color:blue;"&gt;End Set
  End Property

  Private &lt;/span&gt;_city &lt;span style="color:blue;"&gt;As String
  Public Property &lt;/span&gt;City() &lt;span style="color:blue;"&gt;As String
    Get
      Return &lt;/span&gt;_city
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As String&lt;/span&gt;)
      _city = value
    &lt;span style="color:blue;"&gt;End Set
  End Property
  &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;more properties goes here...
&lt;/span&gt;&lt;span style="color:blue;"&gt;End Class&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;So basically we here have an Identity class that expose an ID property and then we have a Person and a Company class that both inherit from the Identity class. Now let’s say that you want to create a collection class that only accepts objects of the Identity class or one of its descendants, it could then look something like the following.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;IdentityCollection(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T &lt;span style="color:blue;"&gt;As &lt;/span&gt;Identity)
  &lt;span style="color:blue;"&gt;Inherits &lt;/span&gt;List(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
&lt;span style="color:blue;"&gt;End Class&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;You can specify more than one constraint if you like, in which case you list the different constraints inside curly braces. 

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;IdentityCollection(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T &lt;span style="color:blue;"&gt;As &lt;/span&gt;{Identity, IComparable})
  &lt;span style="color:blue;"&gt;Inherits &lt;/span&gt;List(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;T)
&lt;span style="color:blue;"&gt;End Class&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To require an accessible parameterless constructor you would include the &lt;font color="#0000ff"&gt;New&lt;/font&gt; keyword in the list. For a reference type include the &lt;font color="#0000ff"&gt;Class&lt;/font&gt; keyword, and for value types you would include the &lt;font color="#0000ff"&gt;Structure&lt;/font&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generic collections&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generics are mostly used with collections, and the .Net team have included a bunch of various generic collections for us, including list, stack, queue, and dictionary collections. They are all part of the System.Collections.Generic namespace.&lt;/p&gt;

&lt;p&gt;In .Net 1.1 we where more or less forced to use the ArrayList collection type. However that excepted items of the System.Object type which means that casting and, if you use value types, boxing/unboxing needs to be performed. That can be very inefficient, especially in For Each loops. Using the List(Of T) we don’t have to do any of that.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myList &lt;span style="color:blue;"&gt;As New &lt;/span&gt;List(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;Person)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So in this article I’ve covered most of what you need to know about using generics. If you haven’t started using generics yet, I hope this article will get you started.&lt;/p&gt;

&lt;p&gt;Have fun.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1720175" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/generics/default.aspx">generics</category></item></channel></rss>