Link to home
Start Free TrialLog in
Avatar of abenage
abenage

asked on

Class construction and usage fundamentals.

I have been learning VB.NET and ASP.NET development on my own.  This has been going pretty well, but I realize I am not using classes correctly.  I think I understand that a class is an object definition or blueprint.  If I create a "Person" class, I would probably want to do so using common elements such as "First Name', "Last Name", "Address1", "Address2", etc.

Here is my person class:
Public Class Person
    Private _firstname As String
    Private _lastname As String
    Private _middlename As String
    Private _salutation As String
    Private _homephone As String
    Private _homeaddress1 As String
    Private _homeaddress2 As String
    Private _homecity As String
    Private _homestate As String
    Private _homezip As Integer
    Private _homezipext As Integer
    Private _homeemail1 As String
    Private _homeemail2 As String
    Private _homecell1 As String
    Private _homecell2 As String
    Private _homeurl As String
    Private _sigother As String
    Private _currentcompany As String
    Private _previouscompany1 As String
    Private _previouscompany2 As String
    Private _previouscompany3 As String
    Private _workphone As String
    Private _workaddress1 As String
    Private _workaddress2 As String
    Private _workcity As String
    Private _workstate As String
    Private _workzip As Integer
    Private _workzipext As Integer
    Private _workemail1 As String
    Private _workemail2 As String
    Private _workcell1 As String
    Private _workcell2 As String
    Private _workurl As String
    Private _currentsalary As Double
    Private _currenttitle As String
    Private _currenttitlecd As String
    Private _previoustitle1 As String
    Private _previoustitlecd1 As String
    Private _previoustitle2 As String
    Private _previoustitlecd2 As String
    Private _previoustitle3 As String
    Private _previoustitlecd3 As String

    Public Sub New(ByVal firstname As String, _
    ByVal lastname As String, _
    ByVal middlename As String, _
    ByVal salutation As String, _
    ByVal homephone As String, _
    ByVal homeaddress1 As String, _
    ByVal homeaddress2 As String, _
    ByVal homecity As String, _
    ByVal homestate As String, _
    ByVal homezip As Integer, _
    ByVal homezipext As Integer, _
    ByVal homeemail1 As String, _
    ByVal homeemail2 As String, _
    ByVal homecell1 As String, _
    ByVal homecell2 As String, _
    ByVal homeurl As String, _
    ByVal sigother As String, _
    ByVal currentcompany As String, _
    ByVal previouscompany1 As String, _
    ByVal previouscompany2 As String, _
    ByVal previouscompany3 As String, _
    ByVal workphone As String, _
    ByVal workaddress1 As String, _
    ByVal workaddress2 As String, _
    ByVal workcity As String, _
    ByVal workstate As String, _
    ByVal workzip As Integer, _
    ByVal workzipext As Integer, _
    ByVal workemail1 As String, _
    ByVal workemail2 As String, _
    ByVal workcell1 As String, _
    ByVal workcell2 As String, _
    ByVal workurl As String, _
    ByVal currentsalary As Double, _
    ByVal currenttitle As String, _
    ByVal currenttitlecd As String, _
    ByVal previoustitle1 As String, _
    ByVal previoustitlecd1 As String, _
    ByVal previoustitle2 As String, _
    ByVal previoustitlecd2 As String, _
    ByVal previoustitle3 As String, _
    ByVal previoustitlecd3 As String)

        _firstname = firstname
        _lastname = lastname
        _middlename = middlename
        _salutation = salutation
        _homephone = homephone
        _homeaddress1 = homeaddress1
        _homeaddress2 = homeaddress2
        _homecity = homecity
        _homestate = homestate
        _homezip = homezip
        _homezipext = homezipext
        _homeemail1 = homeemail1
        _homeemail2 = homeemail2
        _homecell1 = homecell1
        _homecell2 = homecell2
        _homeurl = homeurl
        _sigother = sigother
        _currentcompany = currentcompany
        _previouscompany1 = previouscompany1
        _previouscompany2 = previouscompany2
        _previouscompany3 = previouscompany3
        _workphone = workphone
        _workaddress1 = workaddress1
        _workaddress2 = workaddress2
        _workcity = workcity
        _workstate = workstate
        _workzip = workzip
        _workzipext = workzipext
        _workemail1 = workemail1
        _workemail2 = workemail2
        _workcell1 = workcell1
        _workcell2 = workcell2
        _workurl = workurl
        _currentsalary = currentsalary
        _currenttitle = currenttitle
        _currenttitlecd = currenttitlecd
        _previoustitle1 = previoustitle1
        _previoustitlecd1 = previoustitlecd1
        _previoustitle2 = previoustitle2
        _previoustitlecd2 = previoustitlecd2
        _previoustitle3 = previoustitle3
        _previoustitlecd3 = previoustitlecd3
    End Sub
    Public ReadOnly Property Firstname() As String
        Get
            Return _firstname
        End Get
    End Property


    Public ReadOnly Property Lastname() As String
        Get
            Return _lastname
        End Get
    End Property


    Public ReadOnly Property Middlename() As String
        Get
            Return _middlename
        End Get
    End Property


    Public ReadOnly Property Salutation() As String
        Get
            Return _salutation
        End Get
    End Property

    Public ReadOnly Property Homephone() As String
        Get
            Return _homephone
        End Get
    End Property

    Public ReadOnly Property Homeaddress1() As String
        Get
            Return _homeaddress1
        End Get
    End Property

    Public ReadOnly Property Homeaddress2() As String
        Get
            Return _homeaddress2
        End Get
    End Property

    Public ReadOnly Property Homecity() As String
        Get
            Return _homecity
        End Get
    End Property

    Public ReadOnly Property Homestate() As String
        Get
            Return _homestate
        End Get
    End Property

    Public ReadOnly Property Homezip() As Integer
        Get
            Return _homezip
        End Get
    End Property

    Public ReadOnly Property Homezipext() As Integer
        Get
            Return _homezipext
        End Get
    End Property

    Public ReadOnly Property Homeemail1() As String
        Get
            Return _homeemail1
        End Get
    End Property

    Public ReadOnly Property Homeemail2() As String
        Get
            Return _homeemail2
        End Get
    End Property

    Public ReadOnly Property Homecell1() As String
        Get
            Return _homecell1
        End Get
    End Property

    Public ReadOnly Property Homecell2() As String
        Get
            Return _homecell2
        End Get
    End Property

    Public ReadOnly Property Homeurl() As String
        Get
            Return _homeurl
        End Get
    End Property

    Public ReadOnly Property Sigother() As String
        Get
            Return _sigother
        End Get
    End Property

    Public ReadOnly Property Currentcompany() As String
        Get
            Return _currentcompany
        End Get
    End Property

    Public ReadOnly Property Previouscompany1() As String
        Get
            Return _previouscompany1
        End Get
    End Property

    Public ReadOnly Property Previouscompany2() As String
        Get
            Return _previouscompany2
        End Get
    End Property

    Public ReadOnly Property Previouscompany3() As String
        Get
            Return _previouscompany3
        End Get
    End Property

    Public ReadOnly Property Workphone() As String
        Get
            Return _workphone
        End Get
    End Property

    Public ReadOnly Property Workaddress1() As String
        Get
            Return _workaddress1
        End Get
    End Property

    Public ReadOnly Property Workaddress2() As String
        Get
            Return _workaddress2
        End Get
    End Property

    Public ReadOnly Property Workcity() As String
        Get
            Return _workcity
        End Get
    End Property

    Public ReadOnly Property Workstate() As String
        Get
            Return _workstate
        End Get
    End Property

    Public ReadOnly Property Workzip() As Integer
        Get
            Return _workzip
        End Get
    End Property

    Public ReadOnly Property Workzipext() As Integer
        Get
            Return _workzipext
        End Get
    End Property

    Public ReadOnly Property Workemail1() As String
        Get
            Return _workemail1
        End Get
    End Property

    Public ReadOnly Property Workemail2() As String
        Get
            Return _workemail2
        End Get
    End Property

    Public ReadOnly Property Workcell1() As String
        Get
            Return _workcell1
        End Get
    End Property

    Public ReadOnly Property Workcell2() As String
        Get
            Return _workcell2
        End Get
    End Property

    Public ReadOnly Property Workurl() As String
        Get
            Return _workurl
        End Get
    End Property

    Public ReadOnly Property Currentsalary() As Double
        Get
            Return _currentsalary
        End Get
    End Property

    Public ReadOnly Property Currenttitle() As String
        Get
            Return _currenttitle
        End Get
    End Property

    Public ReadOnly Property Currenttitlecd() As String
        Get
            Return _currenttitlecd
        End Get
    End Property

    Public ReadOnly Property Previoustitle1() As String
        Get
            Return _previoustitle1
        End Get
    End Property

    Public ReadOnly Property Previoustitlecd1() As String
        Get
            Return _previoustitlecd1
        End Get
    End Property

    Public ReadOnly Property Previoustitle2() As String
        Get
            Return _previoustitle2
        End Get
    End Property

    Public ReadOnly Property Previoustitlecd2() As String
        Get
            Return _previoustitlecd2
        End Get
    End Property

    Public ReadOnly Property Previoustitle3() As String
        Get
            Return _previoustitle3
        End Get
    End Property


End Class

I found a simple "Bug" Class on page 384 of the O'Reilly "Programming ASP.NET" second edition book.  I basically followed what they were doing and created my own person class.  This is the first time I have created one.

I now have an ASPX page, using web services which returns to me a dataset.  The data consists of a the basic person elements described in my person class, such as "First Name', "Last Name", "Address1", "Address2", etc.

What I would like to do is turn each row in the dataset into a "Person".  

The code I have in my ASPX page currently looks like this:
Imports PersonClass
Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub
    Private Sub BindGrid()
        Dim consumeWebService As New WebService.Service1()
        Dim people As DataSet = consumeWebService.GetPeopleByName("b%", "a%") 'Lastname, firstname
        DataGrid1.DataSource = people
        DataGrid1.DataBind()
    End Sub

End Class

This code retrieves a dataset from a web service.  I then bind a datagrid to the dataset, and my data is displayed.

Lets say I wanted to bind my datagrid to a person class instead of to a dataset. How would you instantiate a new person for each row from the data in the dataset?

I hope this is not too confusing....
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial