vbnet
Use when writing or maintaining Visual Basic .NET on modern .NET — console tools that read CSV, year-over-year and unit-cost comparisons, System.CommandLine, xUnit tests and Docker builds. Also use wh
SKILL.mdskills/languages/vbnet/SKILL.md
GitHubVisual Basic .NET
Default: current LTS .NET SDK, Option Strict On, Option Explicit On, Option Infer On, System.CommandLine for arguments, xUnit tests (tests may be written in C# or VB), Docker for a clean build.
Setup
dotnet new console -lang VB -n Tooland a test project; add both to a solution.- Set in the project file:
<OptionStrict>On</OptionStrict>,<OptionExplicit>On</OptionExplicit>,<TreatWarningsAsErrors>true</TreatWarningsAsErrors>. VB has no nullable reference type analysis, so check forNothingexplicitly at boundaries. - Pin the SDK with
global.json.
Why Option Strict matters
With Option Strict Off, VB performs late binding and implicit narrowing conversions (Dim n As Integer = "12" compiles). Errors move from compile time to run time. Turn it on for new code; when porting, turn it on file by file and fix each conversion explicitly (CInt, Integer.Parse with a culture).
Reading CSV data
- Use a real CSV parser (
Microsoft.VisualBasic.FileIO.TextFieldParserhandles quotes and embedded commas; or a library such as CsvHelper). - Parse numbers and dates with
CultureInfo.InvariantCulture(or the culture the file was written in) explicitly. - Validate each row and report the line number on errors; skip-and-report beats crash-on-first-error for user-supplied files.
Comparisons people care about
For utility readings (usage and amount per month):
- Year-over-year by matching the same month of the previous year, not "12 rows ago" (months can be missing).
- Unit cost = amount / usage, with a guard for zero usage.
- Flag spikes with a simple, explainable rule (e.g. more than X % above the same month last year and above the trailing median) and print the reason with the flag.
Testing
- Keep calculations in a class library with pure functions; test them with month-boundary and missing-month cases.
- Use
Decimalfor money.
Verification loop
dotnet buildwith zero warnings underOption Strict On.dotnet test.- Run against a sample CSV including a quoted field, a missing month and a zero-usage month.
Pitfalls
- Integer division:
/always returnsDouble; use\for integer division. - Arrays are declared by upper bound:
Dim a(3)has four elements. Is Nothingvs= Nothing: for value types= Nothingcompares with the default value; useIs Nothingfor references and.HasValuefor nullable values.AndAlso/OrElseshort-circuit;And/Orevaluate both sides.- Legacy
Microsoft.VisualBasicfunctions (Left,Mid,Val) still work but are 1-based and culture-dependent; preferStringmethods in new code.