Results 1 to 9 of 9

Thread: Case test not evaluating?

  1. #1

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Question Case test not evaluating?

    I have a simple Case statement inside a loop that should work but doesn't and I have no idea why.

    I'm reading a simple text file that I'm parsing for certain strings, and if found, replaces them:

    Code:
            Using MyReader As New FileIO.TextFieldParser(lblH4Path.Text)                                            ' Read source file.
                Do Until MyReader.EndOfData
                    strTemp = ""
                    strLineBuffer = MyReader.ReadLine
    
                    Select Case strLineBuffer
                        Case InStr(strLineBuffer, "Bob") > 0
                            strLineBuffer.Replace("Bob", "Ray")
                    Select Case strLineBuffer
                        Case instr(strLineBuffer, "test") > 0
                            Stop
                    End Select
    
                    strOutput += strLineBuffer & vbCrLf
                Loop
            End Using
    My text file:

    [experiment]
    Bob
    This is a test.


    When run, neither Case evaluation is tripped. Single-stepping in debug mode, "InStr(strLineBuffer, "Bob")" evaluates as 1 and should trigger the the Replace. Instead it's ignored and I have no idea why.

    Any help is appreciated. TIA.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Case test not evaluating?

    You're using Select Case wrong. Here is an example of the correct usage based on what you're trying to do:-
    Code:
            Dim strLineBuffer As String = "Bob has no clue"
    
            Select Case strLineBuffer.Contains("Bob")
                Case True
                    strLineBuffer = strLineBuffer.Replace("Bob", "Ray")
            End Select
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Case test not evaluating?

    Put a breakpoint in the firts line.
    What is the lblH4Path.Text value?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Re: Case test not evaluating?

    Quote Originally Posted by Niya View Post
    You're using Select Case wrong.
    I was wondering the same thing, so I tried replacing the Select Case with "If" statements and they still are not being triggered:

    Code:
    If InStr(strLineBuffer, "Bob") > 0 Then strLineBuffer.Replace("Bob", "Ray")
    A check of the contents of strLineBuffer returns: Bob
    ? InStr(strLineBuffer, "Bob") returns 1



    I'll try your revised Case.

    FOLLOW-UP: Revised "Case" using "contains" worked. Not sure why "instr()" does not. Thx.
    Last edited by Mugsy323; Jan 11th, 2022 at 10:56 AM.

  5. #5

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    83

    Re: Case test not evaluating?

    Quote Originally Posted by sapator View Post
    Put a breakpoint in the firts line.
    What is the lblH4Path.Text value?
    The path is displayed in a label on my form, so I know it is correct. And regardless of location, the text being read is still "Bob".

    Thx.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Case test not evaluating?

    InStr is legacy code that you really have no need to use... Use Contains or IndexOf

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Case test not evaluating?

    Also, you're using Replace wrong...

    Code:
    If InStr(strLineBuffer, "Bob") > 0 Then strLineBuffer.Replace("Bob", "Ray")
    Should be...

    Code:
    If InStr(strLineBuffer, "Bob") > 0 Then strLineBuffer = strLineBuffer.Replace("Bob", "Ray")

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Case test not evaluating?

    There are several legacy holdovers and other issues in your original example, and .paul./Niya addressed some of them.

    First, you're using a TextFieldParser, but never specifying a delimiter. This means that you're only using it to read a file, line-by-line, and do something on each line. Instead, just use IO.File.ReadAllLines and then iterate over the result.

    Also, you're using the Select/Case wrong. If you want to "do something to a string" when the string matches a condition, then consider setting up a delegate collection.

    Take a look at this example: https://dotnetfiddle.net/LTxnFh
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Public Module Module1
    	
    	Private Delegate Function Transform(ByVal value As String) As String
    	Private ReadOnly _transformations As New Dictionary(Of String, Transform)
    
    	Public Sub Main()
    		IO.File.WriteAllLines("test.txt", {"bob is cool", "john is cooler", "nothing in here matches"})
    
    		_transformations.Add("bob", Function(value) value.Replace("bob", "ray"))
    		_transformations.Add("john", Function(value) value.Replace("john", "jane"))
    
    		Dim strOutput As New StringBuilder()
    		Dim lines() As String = IO.File.ReadAllLines("test.txt")
    		For Each line In lines
    			Dim transformation = _transformations.FirstOrDefault(Function(t) line.Contains(t.Key))
    			If (Not String.IsNullOrWhitespace(transformation.Key)) Then
    				strOutput.AppendLine(transformation.Value(line))
    			Else
    				strOutput.AppendLine(line)
    			End If
    		Next
    		Console.WriteLine(strOutput)
    	End Sub
    
    End Module
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Case test not evaluating?

    You could use a couple of arrays, one 1D lines array, and a 2D replacements array...

    Code:
    Dim lines() As String = New String() {"bob is cool", "john is cooler", "nothing in here matches"} 'IO.File.ReadAllLines("test.txt")
    Dim replacements(,) As String = New String(,) {{"john", "jane"}, {"bob", "ray"}}
    lines = Array.ConvertAll(lines, Function(l As String) replaceAllWords(l, replacements))
    Code:
    Private Function replaceAllWords(ByVal line As String, ByVal replacements(,) As String) As String
        For x As Integer = 0 To replacements.GetUpperBound(0)
            line = line.Replace(replacements(x, 0), replacements(x, 1))
        Next
        Return line
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width