Udostępnij za pośrednictwem


Just playing with Generics in Visual Basic Whidbey

My simple class:

 Public Class Customer
    Dim m_CustomerName As String
    Dim m_CustomerID As Integer
    Public Property CustomerName() As String
        Get
            Return m_CustomerName
        End Get
        Set(ByVal Value As String)
            m_CustomerName = Value
        End Set
    End Property
    Public Property CustomerID() As Integer
        Get
            Return m_CustomerID
        End Get
        Set(ByVal Value As Integer)
            m_CustomerID = Value
        End Set
    End Property
End Class

Then creating a Generic List and working with it:

     Private Sub FillCustomers()
        Dim myCustomers _
            As New Generic.List(Of Customer) 
        Dim C As Customer
        For i As Integer = 1 To 500
            C = New Customer
            C.CustomerID = i
            C.CustomerName = "Name " & i
            myCustomers.Add(C)
        Next
        For Each cust As Customer In myCustomers
            Debug.Print(cust.CustomerName)
        Next
    End Sub

Now, isn't that nice?

Comments

  • Anonymous
    February 19, 2004
    The comment has been removed
  • Anonymous
    February 19, 2004
    Aside from the new syntax but, that example doesn't really show me anything about super special Generics per se. For example, where you have:

    Dim myCustomers _
    As New Generic.List(Of Customer)

    Change it to:

    Dim myCustomers _
    As New ArrayList

    Compile it in 2003 and run it. Voila!
  • Anonymous
    February 19, 2004
    Darren, your missing the point of Generics. It's giving you strongly typed collections without having to write a ton of code. For example, create a new class and tell it to Inherit CollectionBase. Put your cursor on CollectionBase and go to the help for it. From there, to the overview for CollectionBase and scroll down to the sample it shows. Now, you have something to compare what Duncan is pointing out and what you have to do today to accomplish the same sort of thing.
  • Anonymous
    February 19, 2004
    Cory, yes I'm aware of how cool Generics are and when you will use them. My point is that the only thing that reading Duncan's demo will give you is a glimpse of the new syntax.

    So, I guess if Duncan is saying:

    "Now, isn't that nice?" (that migrating code will be simple)

    Then, I agree and I probably missed his point.

    But, if he is saying:

    "Now, isn't that nice?" (that this new stuff rocks based on what I've shown you in this sample)

    Then, no... I don't think that this demo shows much.

    I really only "piped-up" about it because I thought that I might have missed something important that was shown here.
  • Anonymous
    February 19, 2004
    Woo yay walks off
  • Anonymous
    February 19, 2004
    The comment has been removed
  • Anonymous
    February 19, 2004
    It isn't necessary at all, I was just trying to actually do something with each item as I created them.... merely filler code.
  • Anonymous
    February 19, 2004
    I have seen C# generic samples and can quite understand it. In the VB sample presented what I think was missing was to mention the Generic.List declaration I guess might be:

    namespace Generic
    {
    class List<T>
    {
    List(<T>) //constructor
    {...}
    }
    }

    And whose call in C# would be

    List<Custumer> myCustomers = new List<Customer>

    For me, what was to grab is that List<Custumer> myCustomers = new List<Customer> in C#, translates to Dim myCustomer As New Generic.List(Of Customer).

    Am I right? Any comment welcome.

    --
    Luciano Evaristo Guerche
    Jacarei, SP, Brazil
    P.S.: I cannot believe that, I am able to write little C# snippets!
  • Anonymous
    February 19, 2004
    Duncan,

    Is it possible to also create generic classes on VB.NET or just consume them? If your answer is that it is possible to create them, I wonder you would mind porting the article "An Introduction to C# Generics" found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/csharp_generics.asp to VB.NET.

    I read it and feel it is a good example.

    Regards,

    Luciano Evaristo Guerche
    Jacarei, SP, Brazil
  • Anonymous
    February 20, 2004
    This is just cool. I already spend too much time creating typed custom collections. Generics should add another cool tool to add to the 'ol toolbox.

    Thanks for the snipit Duncan.
  • Anonymous
    February 21, 2004
    Yet another boring comment on Generics because nobody can think of anything original to talk about.
  • Anonymous
    February 21, 2004
    Yet another boring comment on Generics because nobody can think of anything original to talk about.
  • Anonymous
    June 04, 2004
    The cool part that Darren wasn't seeing would be changing the for each loop as follows:

    For i As Integer = 0 To myCustomer.Count - 1
    ' Notice the strong typing of the Item property
    Debug.Print(myCustomers.Item(0).CustomerName)
    Next