John Lam 1997 — 2026

Various fun VB.NET observations

I created some VB.NET code where I needed an array with 1 element. This forced me to have to dimension the array with what looks like zero elements: Dim myArray(0) As MyType.

I’m also having a lot of fun with NUnit these days. One thing that I like to do is write stuff out to the Console. Here’s a simple class that lets you write unit tests that assert output to the console:

Public Class ConsoleTester : Inherits Assertion

  Private writer_ As StringWriter
  Private oldWriter_ As TextWriter

  <SetUp()> Public Sub Setup()
    oldWriter_ = System.Console.Out
    writer_ = New StringWriter
    System.Console.SetOut(writer_)
  End Sub

  <TearDown()> Public Sub TearDown()
    System.Console.SetOut(oldWriter_)
  End Sub

  Protected ReadOnly Property ConsoleOutput()
    Get
      Return writer_.GetStringBuilder().ToString
    End Get
  End Property

End Class
3 archived reader comments

Moi (2004-04-20)

I don’t get that (but then I’m not a VB programmer). Why do you have to do the zero thing?

Some Guy (2004-05-03)

Huh? Why not Dim myArray as MyType?

Unless you’re going to later ReDim, which you shouldn’t be doing anyhow, why would you explicitly set (0)?

**** (2004-06-11)

public class TestListener { public TestListener() { TextWriterTraceListener stdout = new TextWriterTraceListener(System.Console.Out); Trace.Listeners.Clear(); Trace.Listeners.Add( stdout ); Trace.AutoFlush = true; } }