4 Replies - 1526 Views - Last Post: 08 December 2010 - 08:38 AM Rate Topic: -----

#1 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Help with nested if and loop?

Posted 07 December 2010 - 10:35 PM

I am having trouble with this code. The code runs perfectly. What the code does is it loops through the array and searches for a match. that works. The problem is when they either get the match or not the message box always shows up (else statement). I know it has something to do with it being in the loop but I am not sure how else to move it any help guys?


    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        Dim state As String
        Dim Abbreviation As String

        If SearchAbbreviationRadiobutton.Checked And StateNameTextBox.Text <> String.Empty Then

            For Me.Subscript = 0 To index - 1 Step 1
                If StateNameTextBox.Text.ToUpper() = StateAbbreviation(Subscript).ToUpper Then

                    Abbreviation = StateAbbreviation(Subscript)
                    state = StateName(Subscript)
                    SearchResultsLabel.Text = String.Format("{0,-2}", state)
                Else
                    MessageBox.Show("error", "nothing found", MessageBoxButtons.OK)
                End If



Next
        End If


Is This A Good Question/Topic? 0
  • +

Replies To: Help with nested if and loop?

#2 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Help with nested if and loop?

Posted 08 December 2010 - 01:30 AM

Can anyone help!?!

I cant put the else out side of the for loop block because of the level of scope.How can I get pass this?
Was This Post Helpful? 0
  • +
  • -

#3 biggerB  Icon User is offline

  • If at first you donít succeed, call it version 1.0
  • member icon

Reputation: 80
  • View blog
  • Posts: 760
  • Joined: 11-June 10

Re: Help with nested if and loop?

Posted 08 December 2010 - 01:58 AM

Whenever A match is not found Your code displays the messagebox.. Try moving the Messagebox code to after the end of the For Next Loop..
Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2506
  • View blog
  • Posts: 4,615
  • Joined: 24-September 10

Re: Help with nested if and loop?

Posted 08 December 2010 - 07:36 AM

something like this... just fill in the extra bits you need.

If bDoSearch Then

	Dim bFound As Boolean = False

	For i As Integer = 0 To count - 1
		If Test(i) Then
			
			bFound = True
			Exit For
		End If
	Next

	If Not bFound Then
		MessageBox.Show("error", "nothing found", MessageBoxButtons.OK)
	End If
End If





I notice though you have strange things going on with the scope and names of your variables.

Is there a reason Me.SubScripts is scoped to the class and not to this function? Also what does that name mean? Also why is it 'state' and 'Abbreviation', what is the consistency? Are the names of your variables just arbitrary?

This post has been edited by lordofduct: 08 December 2010 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 ayabrego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-September 10

Re: Help with nested if and loop?

Posted 08 December 2010 - 08:38 AM

View Postlordofduct, on 08 December 2010 - 06:36 AM, said:

something like this... just fill in the extra bits you need.

If bDoSearch Then

	Dim bFound As Boolean = False

	For i As Integer = 0 To count - 1
		If Test(i) Then
			
			bFound = True
			Exit For
		End If
	Next

	If Not bFound Then
		MessageBox.Show("error", "nothing found", MessageBoxButtons.OK)
	End If
End If





I notice though you have strange things going on with the scope and names of your variables.

Is there a reason Me.SubScripts is scoped to the class and not to this function? Also what does that name mean? Also why is it 'state' and 'Abbreviation', what is the consistency? Are the names of your variables just arbitrary?

I really am not sure i was going by my professors loop for his search. As for the state and abbreviation they are string variables that hold the found data in the structured array . That will be used with the string format. I will try the boolean thank you

This post has been edited by ayabrego: 08 December 2010 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1