0

I am having problems figuring out this problem. I am looking at my book and trying to follow along. i have used the get the way the book is showing and I have a couple of errors and I am not sure how to proceed with tying the the two together. I think I have the right format for the client class. Can someone give me advice to move forward??

Public Class AccountInformationForm

    Private clients(0 To 8) As Client ' Client object
   Private position As Integer = 0 ' current account

   ' create array of Client objects
   Private Sub AccountInformationForm_Load(ByVal sender As  _
      System.Object, ByVal e As System.EventArgs) _
      Handles MyBase.Load

      Dim count As Integer ' counter variable

      ' array of first names
      Dim firstName() As String = _
         New String() {"John", "Sarah", "Jack", "Adam", "Diane", _
            "David", "Kristin", "Jennifer"}

      ' array of last names
      Dim lastName() As String = _
         New String() {"Blue", "White", "Red", "Brown", _
            "Yellow", "Black", "Green", "Orange"}

      'array of account numbers 
      Dim account() As Integer = _
         New Integer() {1234652, 1234666, 1234678, 1234681, _
            1234690, 1234770, 1234787, 1234887}

      ' array of account balances
      Dim balance() As Decimal = _
         New Decimal() {Convert.ToDecimal(1000.78), _
            Convert.ToDecimal(2056.24), Convert.ToDecimal(978.65), _
            Convert.ToDecimal(990.0), Convert.ToDecimal(432.75), _
            Convert.ToDecimal(780.78), Convert.ToDecimal(4590.63), _
            Convert.ToDecimal(7910.11)}

      ' loop and create 10 Client objects
      For count = 0 To clients.GetUpperBound(0)
         ' create new object and store into Client array
         clients(count) = New Client(firstName(count), _
            lastName(count), account(count), balance(count))
      Next
   End Sub ' AccountInformationForm_Load

   ' display next object
   Private Sub nextButton_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles nextButton.Click

      position += 1 ' increment position

      ' if position is last (top) object
      If position > clients.GetUpperBound(0) Then
         position = 0 ' set to first position in array
         DisplayInformation() ' display information
      Else
         DisplayInformation()
      End If
   End Sub ' nextButton_Click

   ' display previous object
   Private Sub preveiousButton_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles previousButton.Click

      position -= 1 ' decrement position

      ' if position is last (bottom) object
      If position < 0 Then
         ' set to last position in array
         position = clients.GetUpperBound(0)
         DisplayInformation()
      Else
         DisplayInformation() ' display information
      End If
   End Sub ' previousButton_Click

   ' display information
   Private Sub DisplayInformation()
      ' use Position as index for each object
      firstTextBox.Text = clients(position).First
      lastTextBox.Text = clients(position).Last
      accountTextBox.Text = _
         Convert.ToString(clients(position).Account)

      ' format as currency
      balanceTextBox.Text = _
         String.Format("{0:C}", clients(position).Balance)
   End Sub ' DisplayInformation
End Class ' AccountInformationForm



Public Class Client
    'account information
    Private firstvalue As String = ""
    Private lastvalue As String = ""
    Private accvalue As Integer
    Private balvalue As Decimal

    'name constructor forst and last supplied
    Public Sub New(ByVal fir As String, ByVal las As String)
        first = fir  'invoke first set accessor
        last = las   'invoke last set accessor
    End Sub


    'first name
    Public Property first() As String
        'return first name
        Get
            Return first
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property last() As String
        Get
            Return last
        End Get
        Set(ByVal value As String)

            lastvalue = value
        End Set
    End Property
End Class

Edited by pritaeas: Fixed formatting

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by Teme64
0

I rewrote the Client class and added a lot of (hopefully) helpful and understandable comments. I didn't check the rest of your code, but I hope you 'grasp' now more of the basics of the classes

Option Explicit On
Option Strict On

Public Class Client

  'account information

  ' Local, private variables to hold the Client _instance_ data
  ' since these are private, you can't access them _outside_ of the
  ' Client class, but you do access them _inside_ in the class!

  Private firstvalue As String = ""
  Private lastvalue As String = ""
  Private accvalue As Integer
  Private balvalue As Decimal

  ' A good convention (not shown in you book I guess), is to name these private variables
  ' in a way that you don't easily 'mix' them with property names
  ' For example, prefix 'm_', like
  'Private m_firstvalue As String = ""
  ' Or just '_'
  'Private _firstvalue As String = ""
  ' Now, when you see a variable name with prefix 'm_' or '_' you know immediately
  ' that it's a classes private variable.

  'name constructor forst and last supplied
  Public Sub New(ByVal fir As String, ByVal las As String)
    'first = fir 'invoke first set accessor
    'last = las 'invoke last set accessor
    ' I suggest using private variables directly in here. Not with Get/Set properties
    firstvalue = fir
    lastvalue = las
  End Sub

  'first name
  ' Properties are the way how you can access Client (class) instance's
  ' data from _outside_ of the Client (class)
  Public Property first() As String
    'return first name
    Get
      ' Since the properties name is 'first', calling 'Return first' makes a new
      ' call to 'first' property and so on. You end up with an endless loop!
      'Return first

      ' With properties you provide access to classes _private_ data i.e. variables
      ' This exposes (=returns) classes private data
      Return firstvalue
    End Get
    Set(ByVal value As String)
      ' And this allows the value set outside of the class
      firstvalue = value
    End Set
  End Property

  Public Property last() As String
    Get
      ' Same 'error' in here
      'Return last
      Return lastvalue

    End Get
    Set(ByVal value As String)
      ' This is correct!
      lastvalue = value
    End Set
  End Property

End Class

I also added 'Option Explicit On' and 'Option Strict On' at the beginning. They are *very* useful and helpful to trap errors at the early stage. But now you have to put this code in it's own class-file (or remove those two first lines, I suggest not).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.