Generics: Building a List of Customers

Posted Fri, Jul 3 2009 8:23 by Deborah Kurata

Often times applications require lists of things: lists of customers, lists of experiments, lists of accounts and so on. The generic lists provided in .NET 2.0 made working with these lists easy. And the list initializers in C# and the object initializers in VB make lists easier still.

As an example, here is a first draft of a simple Customer class.

In C#:  

public class Customer
{
    public int CustomerId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string EmailAddress { get; set; }
}

The above code takes advantage of automatically implemented properties. VB does not yet have this feature, but it is expected with VB 10.

In VB:

Public Class Customer

    Private _CustomerId As Integer
    Public Property CustomerId() As Integer
        Get
            Return _CustomerId
        End Get
        Set(ByVal value As Integer)
            _CustomerId = value
        End Set
    End Property

    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property

    Private _LastName As String   
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Private _EmailAddress As String
    Public Property EmailAddress () As String
        Get
            Return _EmailAddress
        End Get
        Set(ByVal value As String)
            _EmailAddress = value
        End Set
    End Property
End Class

Most often, you would read the customers from a database and build the list. But there are times where you may want to build the list manually. For example, for prototyping or testing.

With C#, you can build a list of customers in one line of code:

List<Customer> custList = new List<Customer>
                    {new Customer()
                          { CustomerId = 1,
                            FirstName="Bilbo",
                            LastName = "Baggins",
                            EmailAddress = "bb@hob.me"},
                    new Customer()
                          { CustomerId = 2,
                            FirstName="Frodo",
                            LastName = "Baggins",
                            EmailAddress = "fb@hob.me"},
                    new Customer()
                          { CustomerId = 3,
                            FirstName="Samwise",
                            LastName = "Gamgee",
                            EmailAddress = "sg@hob.me"},
                    new Customer()
                          { CustomerId = 4,
                            FirstName="Rosie",
                            LastName = "Cotton",
                            EmailAddress = "rc@hob.me"}};

The above example takes advantage of the list initializers feature of C#. VB does not yet have this feature, but it is expected in VB10.

The code to do this in VB takes advantage of object initializers:

Dim custList As New List(Of Customer)
custList.Add(New Customer With {.CustomerId = 1, _
                                .LastName = "Baggins", _
                                .FirstName = "Bilbo", _
                                .EmailAddress="
bb@hob.me"})
custList.Add(New Customer With {.CustomerId = 2, _
                                .LastName = "Baggins", _
                                .FirstName = "Frodo", _
                                .EmailAddress = "
fb@hob.me"})
custList.Add(New Customer With {.CustomerId = 3, _
                                .LastName = "Gamgee", _
                                .FirstName = "Samwise", _
                                .EmailAddress = "
sg@hob.me"})
custList.Add(New Customer With {.CustomerId = 4, _
                                .LastName = "Cotton", _
                                .FirstName = "Rosie", _
                                .EmailAddress = "
rc@hob.me"})

Notice the With statement in this VB code.

Enjoy!

Filed under: , , , , ,

Comments

# XML Literals: Creating an XML File

Friday, July 03, 2009 10:27 AM by Deborah's Developer MindScape

One of my favorite features in VB 9 (Visual Studio 2008) is XML Literals. This example demonstrates how

# Lambdas: Aggregating Strings

Friday, July 03, 2009 12:24 PM by Deborah's Developer MindScape

Looping through a list to append strings is often more challenging than it should be. For example… In

# XML Literals: Reading an XML File

Wednesday, July 08, 2009 4:31 PM by Deborah's Developer MindScape

In a prior post here , I created an XML file using VB 9 (Visual Basic 2008/.NET Framework 3.5). This

# Populating a Business Object from a DataTable

Friday, July 10, 2009 3:43 PM by Deborah's Developer MindScape

Most business applications have business objects such as customer, order, or invoice. Often, the data

# Formatting Text Files

Tuesday, July 21, 2009 3:16 PM by Deborah's Developer MindScape

There are often times that you need to write out text files containing data managed by your application

# Lambda Expressions: An Introduction

Sunday, October 11, 2009 2:11 PM by Deborah's Developer MindScape

My presentation at our local Code Camp was fun. I covered a lot of material and had a GREAT audience

# Lambda Expressions: Finding an Item in a Generic List

Sunday, October 11, 2009 2:52 PM by Deborah's Developer MindScape

In this prior post , I detailed how to build lists of things using a generic List<T>. Once you

# Lambda Expressions: Syntax

Sunday, October 11, 2009 4:02 PM by Deborah's Developer MindScape

This post covers the syntax for lambda expressions. It uses the customer list defined in this prior post

Leave a Comment

(required) 
(required) 
(optional)
(required)