Need help with VB.net involving simple array look-up

Hi guys, I'm trying to have a State Name and Abbreviation finder, that stores the stuff in an array, but I'm not sure on how to go about doing it.

So far, I've gotten this stuff done:

Public Class frmVBACSL

Inherits System.Windows.Forms.Form

[Windows Form Designer generated code]

Structure StateInfo

Dim StateName As String

Dim Abbreviation As String

End Structure

Dim States(56) As StateInfo

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

Me.Close()

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

With txtStateName

.Clear()

End With

With txtAbbreviation()

.Clear()

End With

radStateName.Checked = True

End Sub

Private Sub radStateName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radStateName.CheckedChanged, radAbbreviation.CheckedChanged

Dim radSelected As RadioButton

radSelected = CType(sender, RadioButton)

Select Case radSelected.Name

Case "radStateName"

With txtStateName

.Focus()

.Enabled = True

End With

With txtAbbreviation

.Enabled = False

.Clear()

End With

Case "radAbbreviation"

With txtAbbreviation

.Focus()

.Enabled = True

End With

With txtStateName

.Enabled = False

.Clear()

End With

End Select

End Sub

Private Sub frmVBACSL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

States(0).StateName = "Alabama"

States(0).Abbreviation = "AL"

States(1).StateName = "Alaska"

States(1).Abbreviation = "AK"

States(2).StateName = "American Samoa"

States(2).Abbreviation = "AS"

States(3).StateName = "Arizona"

States(3).Abbreviation = "AZ"

States(4).StateName = "Arkansas"

States(4).Abbreviation = "AR"

States(5).StateName = "California"

States(5).Abbreviation = "CA"

States(6).StateName = "Colorado"

States(6).Abbreviation = "CO"

States(7).StateName = "Connecticut"

States(7).Abbreviation = "CT"

States(8).StateName = "Delaware"

States(8).Abbreviation = "DE"

States(9).StateName = "District of Columbia"

States(9).Abbreviation = "DC"

States(10).StateName = "Florida"

States(10).Abbreviation = "FL"

States(11).StateName = "Georgia"

States(11).Abbreviation = "GA"

States(12).StateName = "Guam"

States(12).Abbreviation = "GU"

States(13).StateName = "Hawaii"

States(13).Abbreviation = "HI"

States(14).StateName = "Idaho"

States(14).Abbreviation = "ID"

States(15).StateName = "Illinois"

States(15).Abbreviation = "IL"

States(16).StateName = "Indiana"

States(16).Abbreviation = "IN"

States(17).StateName = "Iowa"

States(17).Abbreviation = "IA"

States(18).StateName = "Kansas"

States(18).Abbreviation = "KS"

States(19).StateName = "Kentucky"

States(19).Abbreviation = "KY"

States(20).StateName = "Louisiana"

States(20).Abbreviation = "LA"

States(21).StateName = "Maine"

States(21).Abbreviation = "ME"

States(22).StateName = "Maryland"

States(22).Abbreviation = "MD"

States(23).StateName = "Massachusetts"

States(23).Abbreviation = "MA"

States(24).StateName = "Michagan"

States(24).Abbreviation = "MI"

States(25).StateName = "Minnesota"

States(25).Abbreviation = "MN"

States(26).StateName = "Mississippi"

States(26).Abbreviation = "MS"

States(27).StateName = "Missouri"

States(27).Abbreviation = "MO"

States(28).StateName = "Montana"

States(28).Abbreviation = "MT"

States(29).StateName = "Nebraska"

States(29).Abbreviation = "NE"

States(30).StateName = "Nevada"

States(30).Abbreviation = "NV"

States(31).StateName = "New Hampshire"

States(31).Abbreviation = "NH"

States(32).StateName = "New Jersey"

States(32).Abbreviation = "NJ"

States(33).StateName = "New Mexico"

States(33).Abbreviation = "NM"

States(34).StateName = "New York"

States(34).Abbreviation = "NY"

States(35).StateName = "North Carolina"

States(35).Abbreviation = "NC"

States(36).StateName = "North Dakota"

States(36).Abbreviation = "ND"

States(37).StateName = "Ohio"

States(37).Abbreviation = "OH"

States(38).StateName = "Oklahoma"

States(38).Abbreviation = "OK"

States(39).StateName = "Oregon"

States(39).Abbreviation = "OR"

States(40).StateName = "Pennsylvania"

States(40).Abbreviation = "PA"

States(41).StateName = "Puerto Rico"

States(41).Abbreviation = "PR"

States(42).StateName = "Rhode Island"

States(42).Abbreviation = "RI"

States(43).StateName = "South Carolina"

States(43).Abbreviation = "SC"

States(44).StateName = "South Dakota"

States(44).Abbreviation = "SD"

States(45).StateName = "Tennessee"

States(45).Abbreviation = "TN"

States(46).StateName = "Texas"

States(46).Abbreviation = "TX"

States(47).StateName = "Trust Territories"

States(47).Abbreviation = "TT"

States(48).StateName = "Utah"

States(48).Abbreviation = "UT"

States(49).StateName = "Vermont"

States(49).Abbreviation = "VT"

States(50).StateName = "Virginia"

States(50).Abbreviation = "VA"

States(51).StateName = "Virgin Islands"

States(51).Abbreviation = "VI"

States(52).StateName = "Washington"

States(52).Abbreviation = "WA"

States(53).StateName = "West Virgina"

States(53).Abbreviation = "WV"

States(54).StateName = "Wisconsin"

States(54).Abbreviation = "WI"

States(55).StateName = "Wyoming"

States(55).Abbreviation = "WY"

radStateName.Checked = True

txtStateName.Focus()

End Sub

Private Sub btnAbbreviation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbbreviation.Click

MessageBox.Show("The abbreviation for the state " & txtStateName.Text & " is " & States(56).Abbreviation, "Abbreviation", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

Private Sub btnStateName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStateName.Click

MessageBox.Show("The state name for the abbreviation " & txtAbbreviation.Text & " is " & States(56).StateName, "State Name", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

End Class

I'm not sure what I should do to use an input and look up its' correspinding info; State Name for Abbreviation and vice-versa. If there's anyone who can help me with this, that would be great. I need this by Monday though.

If you want, I can send you the actual files.

Thanks,

Paul




Answer this question

Need help with VB.net involving simple array look-up

  • cfBianchi

    That is a weird error, try using the messagebox without the last two parameter. Your code/algorithm seem to be correct.

    Other thing:

    1- Instead of hard coding 54, use States.Length

    2- You might not want to print a message box if no value were found



  • Nagaraj K

    Okay I tried it like this, and it alllllmost worked.

    Private Sub btnAbbreviation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbbreviation.Click

    Dim blnFound As Boolean = False

    Dim intIndex As Integer = 0

    Dim AbbreviationData As String

    Do Until blnFound Or intIndex > 54

    If txtStateName.Text = States(intIndex).StateName Then

    AbbreviationData = States(intIndex).Abbreviation

    blnFound = True

    End If

    intIndex += 1

    Loop

    MessageBox.Show("The abbreviation for the state " & txtStateName.Text & " is " & AbbreviationData & ".", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub btnStateName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStateName.Click

    Dim blnFound As Boolean = False

    Dim intIndex As Integer = 0

    Dim StateNameData As String

    Do Until blnFound Or intIndex > 54

    If txtAbbreviation.Text = States(intIndex).Abbreviation Then

    StateNameData = States(intIndex).StateName

    blnFound = True

    End If

    intIndex += 1

    Loop

    MessageBox.Show("The state name for the abbreviation " & txtAbbreviation.Text & " is " & StateNameData & ".", "State Name", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    End Class

    But I get an error on the bolded lines.

    It says "An unhandled exception of type 'System.ComponentModel.InvalidEnumArgumentException' occurred in system.windows.forms.dll

    Additional information: Enum argument value 64 is not valid for buttons. buttons should be a value from DialogResult."

    I was so close too...



  • pearljam

    Haha, I just realized the messagebox.show function was coded wrong.

    I changed it to this:

    Private Sub btnAbbreviation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbbreviation.Click

    Dim blnFound As Boolean = False

    Dim intIndex As Integer = 0

    Dim ActualAbbreviation As String

    Do Until blnFound Or intIndex > 54

    If txtStateName.Text = States(intIndex).StateName Then

    ActualAbbreviation = States(intIndex).Abbreviation

    blnFound = True

    End If

    intIndex += 1

    Loop

    MessageBox.Show("The abbreviation for the state of " & txtStateName.Text & " is " & ActualAbbreviation & ".", "Abbreviation", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub btnStateName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStateName.Click

    Dim blnFound As Boolean = False

    Dim intIndex As Integer = 0

    Dim ActualName As String

    Do Until blnFound Or intIndex > 54

    If txtAbbreviation.Text = States(intIndex).Abbreviation Then

    ActualName = States(intIndex).StateName

    blnFound = True

    End If

    intIndex += 1

    Loop

    MessageBox.Show("The state name for the abbreviation " & txtAbbreviation.Text & " is " & ActualName & ".", "State Name", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    And it works fine now, but you're right, I can't display an empty Name or Abbreviation, and I'm not sure how to code it to be like "If txt.StateName.text = NotInArray, display error messagebox."

    Mind helping me out there

    Also, what do you mean by States.Length



  • Ricky Lundstal

    Something like this (not tested)

    Private Sub btnAbbreviation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbbreviation.Click

    Dim blnFound As Boolean = False

    Dim intIndex As Integer = 0

    Dim ActualAbbreviation As String

    Do Until blnFound Or intIndex >= States.Length

    If txtStateName.Text = States(intIndex).StateName Then

    ActualAbbreviation = States(intIndex).Abbreviation

    blnFound = True

    End If

    intIndex += 1

    Loop

    If blnFound Then
    MessageBox.Show("The abbreviation for the state of " & txtStateName.Text & " is " & ActualAbbreviation & ".", "Abbreviation", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
    MessageBox.Show("Could not find")
    End If

    End Sub



  • Itzik Katzav

    AceJay,

    please mark the replies that helped you as answers.



  • Ambient

    Thanks, that does work, but why does a blank field still trigger blnFound = True

    Entire code:

    Public Class frmVBACSL

    Inherits System.Windows.Forms.Form

    [Windows Form Designer generated code]

    Structure StateInfo

    Dim StateName As String

    Dim Abbreviation As String

    End Structure

    Dim States(56) As StateInfo

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

    Me.Close()

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    With txtStateName

    .Clear()

    End With

    With txtAbbreviation()

    .Clear()

    End With

    radStateName.Checked = True

    End Sub

    Private Sub radStateName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radStateName.CheckedChanged, radAbbreviation.CheckedChanged

    Dim radSelected As RadioButton

    radSelected = CType(sender, RadioButton)

    Select Case radSelected.Name

    Case "radStateName"

    With txtStateName

    .Focus()

    .Enabled = True

    End With

    With txtAbbreviation

    .Enabled = False

    .Clear()

    End With

    Case "radAbbreviation"

    With txtAbbreviation

    .Focus()

    .Enabled = True

    End With

    With txtStateName

    .Enabled = False

    .Clear()

    End With

    End Select

    End Sub

    Private Sub frmVBACSL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    States(0).StateName = "Alabama"

    States(0).Abbreviation = "AL"

    States(1).StateName = "Alaska"

    States(1).Abbreviation = "AK"

    States(2).StateName = "American Samoa"

    States(2).Abbreviation = "AS"

    States(3).StateName = "Arizona"

    States(3).Abbreviation = "AZ"

    States(4).StateName = "Arkansas"

    States(4).Abbreviation = "AR"

    States(5).StateName = "California"

    States(5).Abbreviation = "CA"

    States(6).StateName = "Colorado"

    States(6).Abbreviation = "CO"

    States(7).StateName = "Connecticut"

    States(7).Abbreviation = "CT"

    States(8).StateName = "Delaware"

    States(8).Abbreviation = "DE"

    States(9).StateName = "District of Columbia"

    States(9).Abbreviation = "DC"

    States(10).StateName = "Florida"

    States(10).Abbreviation = "FL"

    States(11).StateName = "Georgia"

    States(11).Abbreviation = "GA"

    States(12).StateName = "Guam"

    States(12).Abbreviation = "GU"

    States(13).StateName = "Hawaii"

    States(13).Abbreviation = "HI"

    States(14).StateName = "Idaho"

    States(14).Abbreviation = "ID"

    States(15).StateName = "Illinois"

    States(15).Abbreviation = "IL"

    States(16).StateName = "Indiana"

    States(16).Abbreviation = "IN"

    States(17).StateName = "Iowa"

    States(17).Abbreviation = "IA"

    States(18).StateName = "Kansas"

    States(18).Abbreviation = "KS"

    States(19).StateName = "Kentucky"

    States(19).Abbreviation = "KY"

    States(20).StateName = "Louisiana"

    States(20).Abbreviation = "LA"

    States(21).StateName = "Maine"

    States(21).Abbreviation = "ME"

    States(22).StateName = "Maryland"

    States(22).Abbreviation = "MD"

    States(23).StateName = "Massachusetts"

    States(23).Abbreviation = "MA"

    States(24).StateName = "Michagan"

    States(24).Abbreviation = "MI"

    States(25).StateName = "Minnesota"

    States(25).Abbreviation = "MN"

    States(26).StateName = "Mississippi"

    States(26).Abbreviation = "MS"

    States(27).StateName = "Missouri"

    States(27).Abbreviation = "MO"

    States(28).StateName = "Montana"

    States(28).Abbreviation = "MT"

    States(29).StateName = "Nebraska"

    States(29).Abbreviation = "NE"

    States(30).StateName = "Nevada"

    States(30).Abbreviation = "NV"

    States(31).StateName = "New Hampshire"

    States(31).Abbreviation = "NH"

    States(32).StateName = "New Jersey"

    States(32).Abbreviation = "NJ"

    States(33).StateName = "New Mexico"

    States(33).Abbreviation = "NM"

    States(34).StateName = "New York"

    States(34).Abbreviation = "NY"

    States(35).StateName = "North Carolina"

    States(35).Abbreviation = "NC"

    States(36).StateName = "North Dakota"

    States(36).Abbreviation = "ND"

    States(37).StateName = "Ohio"

    States(37).Abbreviation = "OH"

    States(38).StateName = "Oklahoma"

    States(38).Abbreviation = "OK"

    States(39).StateName = "Oregon"

    States(39).Abbreviation = "OR"

    States(40).StateName = "Pennsylvania"

    States(40).Abbreviation = "PA"

    States(41).StateName = "Puerto Rico"

    States(41).Abbreviation = "PR"

    States(42).StateName = "Rhode Island"

    States(42).Abbreviation = "RI"

    States(43).StateName = "South Carolina"

    States(43).Abbreviation = "SC"

    States(44).StateName = "South Dakota"

    States(44).Abbreviation = "SD"

    States(45).StateName = "Tennessee"

    States(45).Abbreviation = "TN"

    States(46).StateName = "Texas"

    States(46).Abbreviation = "TX"

    States(47).StateName = "Trust Territories"

    States(47).Abbreviation = "TT"

    States(48).StateName = "Utah"

    States(48).Abbreviation = "UT"

    States(49).StateName = "Vermont"

    States(49).Abbreviation = "VT"

    States(50).StateName = "Virginia"

    States(50).Abbreviation = "VA"

    States(51).StateName = "Virgin Islands"

    States(51).Abbreviation = "VI"

    States(52).StateName = "Washington"

    States(52).Abbreviation = "WA"

    States(53).StateName = "West Virgina"

    States(53).Abbreviation = "WV"

    States(54).StateName = "Wisconsin"

    States(54).Abbreviation = "WI"

    States(55).StateName = "Wyoming"

    States(55).Abbreviation = "WY"

    radStateName.Checked = True

    txtStateName.Focus()

    End Sub

    Private Sub btnAbbreviation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbbreviation.Click

    Dim blnFound As Boolean = False

    Dim intIndex As Integer = 0

    Dim ActualAbbreviation As String

    Do Until blnFound Or intIndex >= States.Length

    If txtStateName.Text = States(intIndex).StateName Then

    ActualAbbreviation = States(intIndex).Abbreviation

    blnFound = True

    End If

    intIndex += 1

    Loop

    If blnFound = True Then

    MessageBox.Show("The abbreviation for the state of " & txtStateName.Text & " is " & ActualAbbreviation & ".", "Abbreviation", MessageBoxButtons.OK, MessageBoxIcon.Information)

    Else

    MessageBox.Show("Please enter a valid State Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand)

    End If

    End Sub

    Private Sub btnStateName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStateName.Click

    Dim blnFound As Boolean = False

    Dim intIndex As Integer = 0

    Dim ActualName As String

    Do Until blnFound Or intIndex >= States.Length

    If txtAbbreviation.Text = States(intIndex).Abbreviation Then

    ActualName = States(intIndex).StateName

    blnFound = True

    End If

    intIndex += 1

    Loop

    If blnFound = True Then

    MessageBox.Show("The state name for the abbreviation " & txtAbbreviation.Text & " is " & ActualName & ".", "State Name", MessageBoxButtons.OK, MessageBoxIcon.Information)

    Else

    MessageBox.Show("Please enter a valid State Abbreviation", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand)

    End If

    End Sub

    Thanks to whoever can help me out.



  • runfaster2000

    Could you show me exactly how to do that

  • Mike55

    Using a For Next loop all the values in your States array, if the current state is equal to the value inserted by the user, print the message...

  • B2

    Thanks man, it works fine now.

  • Vinnie Tripodi

    You should set States to be

    Dim States(0 To 55) As StateInfo

    If you declare it as

    Dim States(56) As StateInfo

    it will contain 57 items, 0 to 56. You only set 0 to 55 so the last item is blank.

    That is why you get a match when you do not enter anything, it matches the last item.



  • Need help with VB.net involving simple array look-up