0

I'm trying to remove the lines from a text file that contains a specific word using vb.net. I have this done using c#

c#

var oldLines = System.IO.File.ReadAllLines(TemporaryLBL);
var newLines = oldLines.Where(line => !line.Contains("$PhysicsNo#"));
System.IO.File.WriteAllLines(TemporaryLBL, newLines);

vb.net

Dim oldLines = System.IO.File.ReadAllLines(TemporaryLBL)
Dim newLines = oldLines.Where(Function(line) Return Not line.Contains("$PhysicsNo#") End Function)
System.IO.File.WriteAllLines(TemporaryLBL, newLines)

Not sure if this is the correct way to do it, so far I'm getting an error on the Function part: Expression expected.

7
  • 1
    Function(line) Not line.Contains("$PhysicsNo#")) should do it. Commented Aug 11, 2015 at 16:33
  • In case you're not aware of it: converter.telerik.com - using that confirms @Saragis's comment, which ought to be the answer :) Commented Aug 11, 2015 at 16:35
  • possible duplicate of Examples of VB.NET lambda expression Commented Aug 11, 2015 at 16:35
  • You're trying to define a function in place of a lambda expression. You'll need to review what the lambda syntax looks like. Commented Aug 11, 2015 at 16:36
  • @Saragis the error is still there Commented Aug 11, 2015 at 16:38

1 Answer 1

1
IO.File.WriteAllLines(TemporaryLBL, IO.File.ReadAllLines(TemporaryLBL).Where(Function(line) Not line.Contains("$PhysicsNo#")))

you could also try:

Dim oldLines = System.IO.File.ReadAllLines(TemporaryLBL)
Dim newLines = From line In oldLines Where (Not line.Contains("$PhysicsNo#"))
System.IO.File.WriteAllLines(TemporaryLBL, newLines)
Sign up to request clarification or add additional context in comments.

4 Comments

I just tested it and it worked for me. Is it run-time error or compile error?
Compile error: Expression expected. In fact when typing the word Fuction appears underline with the before error
is it Windows or Webb application? Can you post your exact code? All of it. I tried using the line above and it worked in windows app. without importing any libraries
check my update. it does not use the word function but produces the same output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.