Gendude
Gendude

Reputation: 3

Textbox and Class

Have trouble connecting exception to textbox input - Program should only accept "IL", "MO" or "WI" - But it's accepting every input. Everything I tried I'm hitting a brick wall - I know the solution is there, I just need some help. Thanks

Form Codes

Public Class Form1

Dim packages(4) As Packages
Dim pkg As Integer = 0

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    packages(0) = New Packages("Z111", "Eau Claire", "WI")
    packages(1) = New Packages("Z121", "Vicki Vale", "IL")
    packages(2) = New Packages("Z131", "Sammy Davis", "MO")
    packages(3) = New Packages("Z141", "Jon Smith", "IL")
    packages(4) = New Packages("Z151", "Suzie Cassidy", "WI")

    DisplayInfo()

End Sub

Sub DisplayInfo()

    txtID.Text = packages(pkg).PackageID
    txtCity.Text = packages(pkg).DestinationCity
    txtState.Text = packages(pkg).DestinationState
    lblPackageCount.Text = "Package #   " & pkg + 1

End Sub

Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
    pkg = 0
    DisplayInfo()
End Sub

Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
    If pkg > 0 Then
        pkg -= 1
        DisplayInfo()
    End If
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    If pkg < packages.Length - 1 Then
        pkg += 1
        DisplayInfo()
    End If
End Sub

Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
    pkg = packages.Length - 1
    DisplayInfo()
End Sub

Private Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click

    Try

        packages(pkg).PackageID = txtID.Text
        packages(pkg).DestinationCity = txtCity.Text
        packages(pkg).DestinationState = txtState.Text

    Catch ex As Exception

        MessageBox.Show(ex.Message)

    End Try

End Sub

End Class

Class Codes

Public Class Packages

Private packID As String
Private desCity As String
Private desState As String

Sub New(id As String, city As String, state As String)
    packID = id
    desCity = city
    desState = state
End Sub

Public Property PackageID As String
    Get
        Return packID
    End Get
    Set(value As String)
        packID = value
    End Set
End Property

Public Property DestinationCity As String
    Get
        Return desCity
    End Get
    Set(value As String)
        desCity = value
    End Set
End Property

Public Property DestinationState As String
    Get
        Return desState
    End Get
    Set(value As String)
        If desState = "IL" Then
            desState = value
        ElseIf desState = "WI" Then
            desState = value
        ElseIf desState = "MO" Then
            desState = value
        Else
            Throw New Exception("Can not deliver to this state")
        End If
    End Set
End Property

End Class

Upvotes: 0

Views: 46

Answers (1)

tinstaafl
tinstaafl

Reputation: 6948

Your immediate problem is you're checking the property value instead of the passed value. Something like this should work:

Set(value As String)
    'This creates a temporary string array of available states and checks if _
     the value passed is contained in the array
    If {"IL","WI", "MO"}.Contains(value)Then
        desState = value
    Else
        Throw New Exception("Can not deliver to this state")
    End If
End Set

If this is something you may need to change you could make the array a property.

Upvotes: 1

Related Questions