visual studio .net general:
I'm writing a program that requests a word. The word must contain the letters n and r. If it doesn't, a message appears. I got it to show the error message, but it shows it no matter what. Here's my code. Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvaluate.Click Dim info As String = txtWord.Text Dim letter, answer, n, r As String letter = info.ToUpper Dim x, y As Integer x = letter.IndexOf(" ") y = letter.IndexOf(" ") Do While (letter = "n") And (letter = "r") n = letter.Substring(0, x) r = letter.Substring(0, y) If n < r Then answer = n txtResults.Text = answer & " appears first in " & txtWord.Text ElseIf r < n Then answer = r txtResults.Text = answer & " appears first in " & txtWord.Text End If Loop If (letter <> "n") And (letter <> "r") Then MsgBox("Word must contain the letters N and R", , "Error") End If txtWord.Clear() txtWord.Focus() End Sub Any help would be appreciated. --
Steph, I think I see what you are trying to do. You aren't taking into account, when comparing r < n and vice versa, that one can be a -1 if the index of the character is non-existant. Your requirements also say that both must be present. I am assuming that you want to set the txtResult textbox in the event that both are present. Let me see if this simplified version will get the job done for you: Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvaluate.Click Dim targetText As String = txtWord.Text.ToUpper Dim nPos As Integer = targetText.IndexOf("N") Dim rPos As Integer = targetText.IndexOf("R") If (rPos = -1) Or (nPos = -1) Then MsgBox("Word must contain the letters N and R", , "Error") ElseIf rPos < nPos Then txtResults.Text = "R appears first in " & txtWord.Text Else txtResults.Text = "N appears first in " & txtWord.Text End If txtWord.Clear() txtWord.Focus() End Sub HTH! Joseph [quoted text, click to view] Stephanie wrote: > I'm writing a program that requests a word. The word must contain the > letters n and r. If it doesn't, a message appears. I got it to show the > error message, but it shows it no matter what. Here's my code. > > Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles btnEvaluate.Click > > Dim info As String = txtWord.Text > Dim letter, answer, n, r As String > letter = info.ToUpper > Dim x, y As Integer > > x = letter.IndexOf(" ") > y = letter.IndexOf(" ") > > Do While (letter = "n") And (letter = "r") > n = letter.Substring(0, x) > r = letter.Substring(0, y) > > If n < r Then > answer = n > txtResults.Text = answer & " appears first in " & txtWord.Text > > ElseIf r < n Then > answer = r > txtResults.Text = answer & " appears first in " & txtWord.Text > > End If > > Loop > > If (letter <> "n") And (letter <> "r") Then > > MsgBox("Word must contain the letters N and R", , "Error") > End If > txtWord.Clear() > txtWord.Focus() > End Sub > > Any help would be appreciated. > > -- > Steph
Joseph, Thanks. But, how would I write it with a Do While statement? -- Steph [quoted text, click to view] "Joseph Ferris" wrote: > Steph, > > I think I see what you are trying to do. You aren't taking into > account, when comparing r < n and vice versa, that one can be a -1 if > the index of the character is non-existant. > > Your requirements also say that both must be present. I am assuming > that you want to set the txtResult textbox in the event that both are > present. > > Let me see if this simplified version will get the job done for you: > > Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As > > System.EventArgs) Handles btnEvaluate.Click > > Dim targetText As String = txtWord.Text.ToUpper > Dim nPos As Integer = targetText.IndexOf("N") > Dim rPos As Integer = targetText.IndexOf("R") > > If (rPos = -1) Or (nPos = -1) Then > MsgBox("Word must contain the letters N and R", , "Error") > ElseIf rPos < nPos Then > txtResults.Text = "R appears first in " & txtWord.Text > Else > txtResults.Text = "N appears first in " & txtWord.Text > End If > > txtWord.Clear() > txtWord.Focus() > > End Sub > > HTH! > > Joseph > > Stephanie wrote: > > I'm writing a program that requests a word. The word must contain the > > letters n and r. If it doesn't, a message appears. I got it to show the > > error message, but it shows it no matter what. Here's my code. > > > > Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As > > System.EventArgs) Handles btnEvaluate.Click > > > > Dim info As String = txtWord.Text > > Dim letter, answer, n, r As String > > letter = info.ToUpper > > Dim x, y As Integer > > > > x = letter.IndexOf(" ") > > y = letter.IndexOf(" ") > > > > Do While (letter = "n") And (letter = "r") > > n = letter.Substring(0, x) > > r = letter.Substring(0, y) > > > > If n < r Then > > answer = n > > txtResults.Text = answer & " appears first in " & txtWord.Text > > > > ElseIf r < n Then > > answer = r > > txtResults.Text = answer & " appears first in " & txtWord.Text > > > > End If > > > > Loop > > > > If (letter <> "n") And (letter <> "r") Then > > > > MsgBox("Word must contain the letters N and R", , "Error") > > End If > > txtWord.Clear() > > txtWord.Focus() > > End Sub > > > > Any help would be appreciated. > > > > -- > > Steph >
Steph, I don't understand what you are trying to accomplish with the Do While. If you could explain what it is you are looking to do in that regard, I might be able to offer some advice. At least in my mind, the Do While would be if you were doing a character by character examination of the string. But by grabbing the index of the characters up front, there is no longer any need to loop over the logic one letter at a time. I guess what I am trying to say, is there more to the code than this specific problem that you need the loop for? Joseph
I'm taking a class...it's a teaching tool. Had the same experience in other classes. Have you write code that's not necessarily needed. Unfortunately, I'm not learning from it. I can make it work without it. But with it, I'm lost. If you could help me, I'd really appreciate it. -- Steph [quoted text, click to view] "Joseph Ferris" wrote: > Steph, > > I don't understand what you are trying to accomplish with the Do While. > If you could explain what it is you are looking to do in that regard, > I might be able to offer some advice. > > At least in my mind, the Do While would be if you were doing a > character by character examination of the string. But by grabbing the > index of the characters up front, there is no longer any need to loop > over the logic one letter at a time. > > I guess what I am trying to say, is there more to the code than this > specific problem that you need the loop for? > > Joseph >
Kevin, I understand that. But my assignment is Do While. There must be a way to make this happen, whether it's necessary or not. Can you help? -- Steph [quoted text, click to view] "Kevin Spencer" wrote: > Just use the IndexOf() method. No looping needed: > > letter = info.ToUpper() > Dim x As Integer = letter.IndexOf("n") > Dim y As Integer = letter.IndexOf("r") > If x < 0 Or y < 0 Then > MsgBox("Word must contain the letters N and R", , "Error") > End If > > -- > HTH, > > Kevin Spencer > Microsoft MVP > ..Net Developer > A watched clock never boils. > > "Stephanie" <Stephanie@discussions.microsoft.com> wrote in message > news:E12986BB-64CB-49E7-AD72-4986D3E70E87@microsoft.com... > > I'm writing a program that requests a word. The word must contain the > > letters n and r. If it doesn't, a message appears. I got it to show the > > error message, but it shows it no matter what. Here's my code. > > > > Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As > > System.EventArgs) Handles btnEvaluate.Click > > > > Dim info As String = txtWord.Text > > Dim letter, answer, n, r As String > > letter = info.ToUpper > > Dim x, y As Integer > > > > x = letter.IndexOf(" ") > > y = letter.IndexOf(" ") > > > > Do While (letter = "n") And (letter = "r") > > n = letter.Substring(0, x) > > r = letter.Substring(0, y) > > > > If n < r Then > > answer = n > > txtResults.Text = answer & " appears first in " & txtWord.Text > > > > ElseIf r < n Then > > answer = r > > txtResults.Text = answer & " appears first in " & txtWord.Text > > > > End If > > > > Loop > > > > If (letter <> "n") And (letter <> "r") Then > > > > MsgBox("Word must contain the letters N and R", , "Error") > > End If > > txtWord.Clear() > > txtWord.Focus() > > End Sub > > > > Any help would be appreciated. > > > > -- > > Steph > >
Just use the IndexOf() method. No looping needed: letter = info.ToUpper() Dim x As Integer = letter.IndexOf("n") Dim y As Integer = letter.IndexOf("r") If x < 0 Or y < 0 Then MsgBox("Word must contain the letters N and R", , "Error") End If -- HTH, Kevin Spencer Microsoft MVP ..Net Developer A watched clock never boils. [quoted text, click to view] "Stephanie" <Stephanie@discussions.microsoft.com> wrote in message news:E12986BB-64CB-49E7-AD72-4986D3E70E87@microsoft.com... > I'm writing a program that requests a word. The word must contain the > letters n and r. If it doesn't, a message appears. I got it to show the > error message, but it shows it no matter what. Here's my code. > > Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles btnEvaluate.Click > > Dim info As String = txtWord.Text > Dim letter, answer, n, r As String > letter = info.ToUpper > Dim x, y As Integer > > x = letter.IndexOf(" ") > y = letter.IndexOf(" ") > > Do While (letter = "n") And (letter = "r") > n = letter.Substring(0, x) > r = letter.Substring(0, y) > > If n < r Then > answer = n > txtResults.Text = answer & " appears first in " & txtWord.Text > > ElseIf r < n Then > answer = r > txtResults.Text = answer & " appears first in " & txtWord.Text > > End If > > Loop > > If (letter <> "n") And (letter <> "r") Then > > MsgBox("Word must contain the letters N and R", , "Error") > End If > txtWord.Clear() > txtWord.Focus() > End Sub > > Any help would be appreciated. > > -- > Steph
I can help, but you've got to list ALL of the requirements of the assignment. I will always come up with the most elegant or simple solution to the problem, and if one of the requirements of your assignments prohibits using that solution, it would really help me to know up front. This was your original description of your requirement: [quoted text, click to view] >> > I'm writing a program that requests a word. The word must contain the >> > letters n and r. If it doesn't, a message appears
So, before I waste my time on other solutions that don't satisfy any as-yet-unstated requirements, could you tell me what all of them are? -- HTH, Kevin Spencer Microsoft MVP ..Net Developer A watched clock never boils. [quoted text, click to view] "Stephanie" <Stephanie@discussions.microsoft.com> wrote in message news:93F634B6-8A38-45FA-98A8-D9AB3BA18D3A@microsoft.com... > Kevin, > > I understand that. But my assignment is Do While. There must be a way to > make this happen, whether it's necessary or not. > > Can you help? > -- > Steph > > > "Kevin Spencer" wrote: > >> Just use the IndexOf() method. No looping needed: >> >> letter = info.ToUpper() >> Dim x As Integer = letter.IndexOf("n") >> Dim y As Integer = letter.IndexOf("r") >> If x < 0 Or y < 0 Then >> MsgBox("Word must contain the letters N and R", , "Error") >> End If >> >> -- >> HTH, >> >> Kevin Spencer >> Microsoft MVP >> ..Net Developer >> A watched clock never boils. >> >> "Stephanie" <Stephanie@discussions.microsoft.com> wrote in message >> news:E12986BB-64CB-49E7-AD72-4986D3E70E87@microsoft.com... >> > I'm writing a program that requests a word. The word must contain the >> > letters n and r. If it doesn't, a message appears. I got it to show the >> > error message, but it shows it no matter what. Here's my code. >> > >> > Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As >> > System.EventArgs) Handles btnEvaluate.Click >> > >> > Dim info As String = txtWord.Text >> > Dim letter, answer, n, r As String >> > letter = info.ToUpper >> > Dim x, y As Integer >> > >> > x = letter.IndexOf(" ") >> > y = letter.IndexOf(" ") >> > >> > Do While (letter = "n") And (letter = "r") >> > n = letter.Substring(0, x) >> > r = letter.Substring(0, y) >> > >> > If n < r Then >> > answer = n >> > txtResults.Text = answer & " appears first in " & txtWord.Text >> > >> > ElseIf r < n Then >> > answer = r >> > txtResults.Text = answer & " appears first in " & txtWord.Text >> > >> > End If >> > >> > Loop >> > >> > If (letter <> "n") And (letter <> "r") Then >> > >> > MsgBox("Word must contain the letters N and R", , "Error") >> > End If >> > txtWord.Clear() >> > txtWord.Focus() >> > End Sub >> > >> > Any help would be appreciated. >> > >> > -- >> > Steph >> >> >>
Don't see what you're looking for? Try a search.
|