all groups > visual studio .net general > october 2005 >
You're in the

visual studio .net general

group:

Code help


Code help Stephanie
10/31/2005 12:23:02 PM
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.

--
Re: Code help Joseph Ferris
10/31/2005 1:09:40 PM
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]
Re: Code help Stephanie
10/31/2005 1:43:16 PM
Joseph,

Thanks. But, how would I write it with a Do While statement?
--
Steph


[quoted text, click to view]
Re: Code help Joseph Ferris
10/31/2005 1:54:01 PM
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
Re: Code help Stephanie
10/31/2005 2:32:04 PM
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]
Re: Code help Stephanie
10/31/2005 5:29:03 PM
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]
Re: Code help Kevin Spencer
10/31/2005 7:42:11 PM
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]

Re: Code help Kevin Spencer
10/31/2005 11:54:09 PM
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]

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]

AddThis Social Bookmark Button