vb.net, why doesn't this work?

Soldato
Joined
18 Oct 2002
Posts
6,889
I'm doing a "debugging" section on a guide and i can't work out why this doesn't work:

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"


I know i have to advance the letter it is checking, but i don't know how? I am a n00b if you hadn't already guessed :)

B@
 
You need to change these lines:

Code:
For i = 0 To strText.Length - 1 // Substring is zero-based
letter = strText.Substring(i, 1) // this will take care of progressing along the string as the loop iterates.

If letter.ToLower() = "g" Then // this will make the search case insensitive - if you need it to be

It would perform better if you wrote this with a regular expression, but I can't remember what the code would be for that off the top of my head :)
 
Last edited:
Firstly, String.Substring(0) will return the whole string back, you're never actually using your loop variable.
Take a look here: http://msdn.microsoft.com/en-us/library/hxthx5h6.aspx

Secondly, String.Substring(0) returns a string and you're trying to put that into a Char variable and then compare it to a string.
Turn Option Strict On to stop all that implicit type conversion happening.

EDIT: Spunkey, you'd need to use strText.Substring(i,1) to just get a single letter.

Personally I'd do strText.ToCharArray() and then do a For Each over each Char (you could even not bother with the ToCharArray as String implements IEnumerable<Char>, but it makes it more explicit)
 
Last edited:
You need to change these lines:

Code:
For i = 0 To strText.Length - 1 // Substring is zero-based
letter = strText.Substring(i) // this will take care of progressing along the string as the loop iterates.

If letter.ToLower() = "g" Then // this will make the search case insensitive - if you need it to be

It would perform better if you wrote this with a regular expression, but I can't remember what the code would be for that off the top of my head :)
thanks, that's perfect!

It's a bit cheeky of the guide tbh as there's been one small section on loops and no proper discussion of substrings etc.. and just sort'a says "fix it", with no solution, meh.

thanks again.

B@
 
I know your original question has already been answered, but just for reference you can write this method without having to explicitly define a loop.

Code:
'Setup
Const StringUnderReview = "Debugging" 'Inferred variable type
Const CharacterToBeCounted As Char = "g"

'Execution
Dim CharacterCount = StringUnderReview.Count(Function(x) x.Equals(CharacterToBeCounted))

'Display
TextBox1.Text = String.Format("{0} appears {1} time(s)", CharacterToBeCounted, CharacterCount)

Through the use of a pre-existing extension method count and a very simple anonymous function we can mirror what your loop is doing in a single line. As you're still learning, I would recommend avoiding some of the techniques I'm showing you. But you may find it interesting to see what's possible.

There are effectively two distinct parts to the execute process. First, we declare our intentions of performing a count of the elements (chars in this case) in our string. You don't have to worry about splitting the string into individual chars, the function does all that work for us behind the scenes. :D

Code:
StringUnderReview.Count()

We then apply the logic under which that count is performed.

Code:
Function(x) x.Equals(CharacterToBeCounted)

x represents the instance of the char we're performing our test against. You can call this anything you like in all honesty, but x is nice and generic. This may seem very alien at first, but we simply have to somehow provide our conditional logic with the character in the string we're performing logic against. It should be noted that x.Equals(CharacterToBeCounted) simply mirrors your criteria of (If letter = "g"). In fact, you could write the anonymous function as follows:

Code:
Dim CountResults = StringUnderReview.Count(Function(x) x = "g")

...but I prefer the Equals methods with the . :)

Probably not a lot of use, but feel free to ask any questions should you have any! ;)
 
Back
Top Bottom