|
-
Oct 20th, 2009, 08:16 AM
#1
Thread Starter
Registered User
debugging help
the next stage in the tutorial is debugging, ive got the code theyve said and like they said, i cant find whats wrong and am saying to myself that it should work LOL
heres the code
Dim I As Integer
Dim LetterCount As Integer
Dim strText As String
Dim Letter As Char
strText = "Debugging"
For I = 1 To strText.Length - 1
Letter = strText.Substring(0)
If Letter = "g" Then
LetterCount = LetterCount + 1
End If
Next
TextBox1.Text = "g Appears" & " " & LetterCount & " " & "Times"
End Sub
thanks in advance
can u spot whats going wrong please???
-
Oct 20th, 2009, 08:25 AM
#2
Fanatic Member
Re: debugging help
well, break the program down logically.
What is it trying to do? Count the number of times "g" appears in a word
How does it do that? the loop
What's not working? I'm guessing it's outputting zero for you.
So, if it's not "working" and the thing that's supposed to make it "work" is your loop, where do you think the first place you should look at to solve the issue? I'll give you a nudge, it is a problem to do with an index.
Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".
VS 2008 .NetFW 2.0
-
Oct 20th, 2009, 08:27 AM
#3
Thread Starter
Registered User
Re: debugging help
i have no idea what you just said. ive only been doing this for a few weeks and am still not confident enough. im still on tutorial materials could you please tell me which line it is on
-
Oct 20th, 2009, 08:42 AM
#4
Fanatic Member
Re: debugging help
Well, I'm not going to give you the answer. What purpose would that serve? You're there to learn and if you really do want to learn about programming, learning the why is infinitely more important then knowing the how.
vb Code:
For I = 1 To strText.Length - 1 Letter = strText.Substring(0) If Letter = "g" Then LetterCount = LetterCount + 1 End If Next
This is basically the "guts" of your program. What this is doing is reading the length (how many characters) of your string variable strText and using that to count how many times to go through the loop.
Looking at it now there is a problem with your loop to begin with. In VB (and pretty much every other language) loops and lists and arrays are all ZERO based. Which means instead of starting at 1 to count the objects they contain, they start at 0. See one of your problems yet?
Right underneath your loop is a method of the string class called substring. When ever you see something in code you don't understand, press F1 in visual studio to bring up help and search on it! If you need a better explanation, google or go to MSDN's site and read more documentation. Read, read, read, read. There will be points in time where you hit problems and must utilize your resources to solve them, now's a good time to start learning where and how to look for answers to your problem
Anyways, what substring does is return a smaller portion of a large string. This is useful for breaking up large strings, grabbing specific portions of strings, etc. What it's doing here in your code is looking at individual characters in your string variable strText. See the (0) in the substring line? That's saying "return the character at position ZERO". However, it never moves from zero so it will always return "d". So in order for it to move from (0) you need to tell it to, and you do that with a variable that increases by 1 each time the loop iterates.
Do you see a variable in your loop that meets those conditions? If so, where would you put it?
Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".
VS 2008 .NetFW 2.0
-
Oct 20th, 2009, 08:42 AM
#5
Thread Starter
Registered User
Re: debugging help
i still dont get the answer, its just a load of garble to me, all i want to know is what is wrong, then i will understand
Last edited by stuart1512; Oct 20th, 2009 at 08:46 AM.
-
Oct 20th, 2009, 08:48 AM
#6
Fanatic Member
Re: debugging help
how will you understand if you don't even understand where the problem is coming from in the first place? I've pretty much laid out the problems with your code, can you at least take a guess at the lines in which the problems are occurring?
Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".
VS 2008 .NetFW 2.0
-
Oct 20th, 2009, 08:51 AM
#7
Thread Starter
Registered User
Re: debugging help
ive been learning for 2 weeks ONLY. this is my first ever programming and i am not used to looking through and thinking about he answers for myself much, please just tell me what i have to do
-
Oct 20th, 2009, 08:59 AM
#8
Hyperactive Member
Re: debugging help
Hi there,
Backwoodscoder pretty much said it all but maybe you'll understand it in a different context:
The problem is in your loop:
Code:
For I = 1 To strText.Length - 1
Letter = strText.Substring(0)
If Letter = "g" Then
LetterCount = LetterCount + 1
End If
Next
To your program the letters in strText are numbered like this:
D = 0
E = 1
B = 2
B = 3
U = 4
G = 5
G= 6
I = 7
N = 8
G = 9
Your loop says this: start from number 1 to number 9
So your not checking the letter D at place 0!
That is one problem, the 2nd is at this line:
Code:
Letter = strText.Substring(0)
Look at the 0 you placed beteween the brackets of Substring(). That of course should be I.
If you changed those things this is what the loop would do:
I = 0
letter = D
letter is not g
next
I = 1
letter = E
letter is not g
next
I = 2
etc... You get the idea
So what your code should be like:
Code:
For I = 0 To strText.Length - 1
Letter = strText.Substring(I)
If Letter = "g" Then
LetterCount = LetterCount + 1
End If
Next
Hopefully that helped!
-
Oct 20th, 2009, 09:01 AM
#9
Fanatic Member
Re: debugging help
I'd hope so, you just fed him the answer lol!
Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".
VS 2008 .NetFW 2.0
-
Oct 20th, 2009, 09:13 AM
#10
Thread Starter
Registered User
Re: debugging help
thanks gonzalioz. i was just about to give up on the whole thing
-
Oct 20th, 2009, 09:16 AM
#11
Hyperactive Member
Re: debugging help
No problem, I know you can sometimes just look at a code for hours and can't spot the problem. But I would suggest following this tutorial:
http://www.homeandlearn.co.uk/NET/vbNet.html
That's how I started 7 weeks ago. It's a great tutorial. Very easy to read and very good explanation.
This is the chapter about loops, if you didn't understand all of it yet:
http://www.homeandlearn.co.uk/NET/nets3p1.html
-
Oct 20th, 2009, 09:19 AM
#12
Thread Starter
Registered User
Re: debugging help
thats the tutorial im using, its great but i just dont understand it at times
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|