2013-12-09 36 views
0

無法將異常連接到文本框輸入 - 程序只應接受「IL」,「MO」或「WI」 - 但它接受每個輸入。我試過的每一件事我都碰到了一堵磚牆 - 我知道解決方案就在那裏,我只需要一些幫助。由於文本框和類

表代碼

公共類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 

末級

類代碼

公共類包

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 

末級

+0

我不確定「接受每一個輸入」是什麼意思。我看到,一旦你點擊「btnSaveChanges」,你的「txtState」就會有「PA」,你應該有一個消息框 –

+0

如果我輸入AZ到狀態文本框,它將它發佈到數組中,它不會引發異常。 – Gendude

+0

你可能只是在DestinationState屬性的setter處設置一個斷點,並找出這裏設置的值 - 這可能有點奇怪......從那裏你可以深入到原因。 – Rex

回答

1

立即解決問題是你正在檢查的屬性值,而不是傳遞的值。像這樣的東西應該工作:

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 

如果這是你可能需要改變你可以使數組的屬性。

+0

非常感謝tinstaafl - 不知道爲什麼我沒有想到這一點。 – Gendude