Hi. I was doing a tutorial and it told me to put a structure into a new class all by itself (By class I mean right clicking on my project name in the Solution Explorer, going into the add sub-menu, and clicking on class). I experimented and I put the structure into the form class and it worked perfectly. I wanted to know if there were any benefits to putting a structure in a separate class or if it's just to stay organized. I don't know if you need my code but I included it below.
Code from Form1.vb
Here is the code from Customer.vb
Code from Form1.vb
Code:
Public Class Form1
Private objCustomers As New ArrayList
Public Sub CreateCustomer(firstName As String, lastName As String, email As String)
Dim objNewCustomer As Customer
objNewCustomer.FirstName = firstName
objNewCustomer.LastName = lastName
objNewCustomer.Email = email
objCustomers.Add(objNewCustomer)
lstCustomers.Items.Add(objNewCustomer)
End Sub
Public Sub DisplayCustomer(Customer As Customer)
txtName.Text = Customer.Name
txtFirstName.Text = Customer.FirstName
txtLastName.Text = Customer.LastName
txtEmail.Text = Customer.Email
End Sub
Private Sub btnListCustomer_Click(sender As Object, e As EventArgs) Handles btnListCustomer.Click
CreateCustomer("Darrel", "Hilton", "dhilton@somecompany.com")
CreateCustomer("Frank", "Peoples", "fpeoples@somecompany.com")
CreateCustomer("Bill", "Scott", "bscott@somecompany.com")
End Sub
Here is the code from Customer.vb
Code:
Public Structure Customer
Public FirstName As String
Public LastName As String
Public Email As String
Public ReadOnly Property Name() As String
Get
Return FirstName & " " & LastName
End Get
End Property
''' <summary>
''' Overides the default ToString method
''' </summary>
''' <returns>String</returns>
''' <Remarks>Returns the customer name and email address</Remarks>
Public Overrides Function ToString() As String
Return Name & " (" & Email & ")"
End Function
End Structure