-
Jan 11th, 2022, 09:21 AM
#1
Thread Starter
Lively Member
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.
-
Jan 11th, 2022, 09:38 AM
#2
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
-
Jan 11th, 2022, 09:45 AM
#3
Re: Case test not evaluating?
Put a breakpoint in the firts line.
What is the lblH4Path.Text value?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jan 11th, 2022, 10:49 AM
#4
Thread Starter
Lively Member
Re: Case test not evaluating?
Originally Posted by Niya
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.
-
Jan 11th, 2022, 10:52 AM
#5
Thread Starter
Lively Member
Re: Case test not evaluating?
Originally Posted by sapator
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.
-
Jan 11th, 2022, 11:08 AM
#6
Re: Case test not evaluating?
InStr is legacy code that you really have no need to use... Use Contains or IndexOf
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 11th, 2022, 11:11 AM
#7
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")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 11th, 2022, 12:06 PM
#8
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
-
Jan 11th, 2022, 01:16 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|