.IndexOf problem

|2eM!x

Member
Joined
Feb 19, 2006
Messages
5
Programming Experience
1-3
Okay, I have the following code:

VB.NET:
Option Strict On
Option Explicit On
Imports System.Text

    Public Class Form1
        Delegate Function EnumWindProc( _
                ByVal hWnd As Int32, _
                ByVal lParam As Int32) As Boolean

        Delegate Function EnumChildWindProc( _
                ByVal hWnd As Int32, _
                ByVal lParam As Int32) As Boolean

        Declare Function EnumWindows Lib "user32.dll" ( _
                ByVal lpEnumProc As EnumWindProc, _
                ByVal lParam As Int32) As Boolean

        Declare Function EnumChildWindows Lib "user32" ( _
             ByVal hWnd As IntPtr, _
             ByVal lpEnumFunc As EnumWindProc, _
             ByRef lParam As IntPtr) As Int32
    Dim sBuild As New StringBuilder(255)
        Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As Int32, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Int32) As Int32
        Private Function EnumChild(ByVal hWnd As Int32, ByVal lParam As Int32) As Boolean
            GetClassName(hWnd, sBuild, sBuild.Capacity)
            Debug.Print(sBuild.ToString)
        EnumChild = True
    End Function

    Private Function EnumWins(ByVal hwnd As Int32, ByVal lParam As Int32) As Boolean
        Dim proc As New EnumWindProc(AddressOf EnumChild)
        GetClassName(hwnd, sBuild, sBuild.Capacity).ToString()
[B]        If sBuild.ToString.IndexOf("ico") > 0 Then
            EnumChildWindows(CType(hwnd, IntPtr), proc, IntPtr.Zero)
        End If[/B]
        EnumWins = True
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim proc As New EnumWindProc(AddressOf EnumWins)
        EnumWindows(proc, 0)
    End Sub
    End Class

The bolded line does return the correct string, because I have debug.printed the line.

However, if i use the following code in place of it:

If CBool(sBuild.ToString.IndexOf("ico")) = False Then
EnumChildWindows(CType(hwnd, IntPtr), proc, IntPtr.Zero)
End If


Doesnt make sense, works, but id like a logical way to do it :)

Thanks :)
 
Sounds very logical to me:
1) 0 as a return value for IndexOf means the value is found at position 0, meaning the string starts with the value you searched for.
2) CBool(0) is False.
If you want to know if a certain string is found or not you should compare to -1 , or > -1, or >= 0
VB.NET:
[B]If sBuild.ToString.IndexOf("ico") <> -1 Then[/B]
[B][COLOR=seagreen]' found "ico" ![/COLOR][/B]
 
Also note that .NET 2.0 introduces the System.String.Contains method:

VB.NET:
[COLOR=#0000ff]Dim[/COLOR] s [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR] = [COLOR=#800000]"okeydokey"[/COLOR]
[COLOR=#0000ff]If[/COLOR] s.Contains([COLOR=#800000]"eydo"[/COLOR]) [COLOR=#0000ff]Then[/COLOR]
[COLOR=#008000]    ' do something[/COLOR]
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR]
 
Back
Top