Choosing Our Properties





Choosing Our Properties

Let's start writing our class by declaring it and creating the necessary references that we'll need in Listing 5.2. We'll also set up the properties and corresponding private variables.

The start of our data class

C#

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace UberAspNet
{
  public class Customer
  {
    private int _CustomerID;
    public int CustomerID
    {
      get {return _CustomerID;}
    }

    private string _LastName;
    public string LastName
    {
      get {return _LastName;}
      set {_LastName = value;}
    }

    private string _FirstName;
    public string FirstName
    {
      get {return _FirstName;}
      set {_FirstName = value;}
    }

    private string _Address;
    public string Address
    {
      get {return _Address;}
      set {_Address = value;}
    }

    private string _City;
    public string City
    {
      get {return _City;}
      set {_City = value;}
    }

    private string _State;
    public string State
    {
      get {return State;}
      set {_State = value;}
    }

    private string _Zip;
    public string Zip
    {
      get {return _Zip;}
      set {_Zip = value;}
    }

    private string _Phone;
    public string Phone
    {
      get {return _Phone;}
      set {_Phone = value;}
    }

    private DateTime _SignUpDate;
    public DateTime SignUpDate
    {
      get {return _SignUpDate;}
      set {_SignUpDate = value;}
    }
  }
}


VB.NET

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web

Namespace UberAspNet

   Public Class Customer

      Private _CustomerID As Integer
      Public ReadOnly Property CustomerID() As Integer
         Get
            Return _CustomerID
         End Get
      End Property 

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

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

      Private _Address As String
      Public Property Address() As String
         Get
            Return _Address
         End Get
         Set
            _Address = value
         End Set
      End Property 

      Private _City As String
      Public Property City() As String
         Get
            Return _City
         End Get
         Set
            _City = value
         End Set
      End Property 

      Private _State As String
      Public Property State() As String
         Get
            Return State
         End Get
         Set
            _State = value
         End Set
      End Property 

      Private _Zip As String
      Public Property Zip() As String
         Get
            Return _Zip
         End Get
         Set
            _Zip = value
         End Set
      End Property 

      Private _Phone As String
      Public Property Phone() As String
         Get
            Return _Phone
         End Get
         Set
            _Phone = value
         End Set
      End Property 

      Private _SignUpDate As DateTime
      Public Property SignUpDate() As DateTime
         Get
            Return _SignUpDate
         End Get
         Set
            _SignUpDate = value
         End Set
      End Property

   End Class

End Namespace

The code is fairly straightforward. We've created a property to correspond to each of the columns in our database table, matching the data types. We've also created a private variable for each column for internal use in our class. The only unusual thing here is that we've made the CustomerID property read-only. That's because our database table is designed to make this a primary key and an identity field, meaning that SQL Server will number the column automatically when we add new rows. For that reason, we don't want the class to have the ability to alter this property. This read-only strategy also protects other developers (or yourself if you can't remember every detail about what you've written) from doing something that could break the program.

In our case we've named the private variables with the same name as the properties, only with an underscore character in front of them. There are a number of different ways to name these according to various academic standards and recommendations, but ultimately it's up to you. Be consistent in your naming conventions. If they confuse you, imagine what they might do to other developers who need to edit your code!


You could declare default values for each private variable, but we're going to defer those assignments to our constructors. We might want to assign different default values depending on the overload of the constructor called.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows