Line Filters in Visual Basic .NET
A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep
and sed
are common line filters.
Here’s an example line filter in Visual Basic .NET that writes a capitalized version of all input text. You can use this pattern to write your own Visual Basic .NET line filters.
To try out our line filter, first make a file with a few lowercase lines.
Then use the line filter to get uppercase lines.
In this Visual Basic .NET version:
We use
StreamReader
to read from the standard input stream (Console.OpenStandardInput()
).We read the input line by line using a
While
loop and theReadLine()
method, which is similar to theScan()
method in the original example.We use the
ToUpper()
method to convert each line to uppercase.We write the uppercase line to the console using
Console.WriteLine()
.Error handling is done by checking
Console.Error
. If there’s an error, we print it to the error stream and exit with a non-zero status code.The command to compile and run the program is different in Visual Basic .NET. We use
vbc
to compile and then run the resulting executable.
This example demonstrates how to create a simple line filter in Visual Basic .NET, reading from standard input, processing each line, and writing to standard output.