VB.Net Tips



1.  Debug.print is not working

Right-click on the project (inc test project), click properties, click tab 'Compile', click button 'Advanced Compile Options...' and put a checkmark in 'Define DEBUG constant'. 

You can then use Debug.Print as in VB6, and see the output in the "Output" tab.  However... you have to run the test as Debug to see anything.

Note that the Output Tab by default is full of crap.  Right-click on it and uncheck everything apart from "Program output".

2.  DeploymentItem - how to copy files before a test runs

Do this:

<TestClass()> <DeploymentItem("DIT", "DIT")> _
Public Class Bas77StemListTest

to copy the contents of folder DIT under the main project directory to a new folder DIT under the same dir as the EXE in TestResults

or this

<TestMethod()> <DeploymentItem("DIT", "DIT")> _
Public  testGetStems()

to do the same as part of a method.

You can also specify individual files, and also have multiple <DeploymentItem> tags.

You can also copy files before all tests using Test | Edit Test Run Configurations | Local Test Run | Deployment.

Make sure "Enable Deployment" is ticked.

You can add a folder in "Additional files and directories to deploy".  But if you add a folder, the contents of the folder are copied to the same dir as the executable, in TestResults, not the folder itself.

Note that if need be you can prefix a test with

<Ignore()> 

3.  String.split does not work correctly with vbCrLf

Splitting a string containing a text file with the lines ending in CR and LF is an obvious thing to do.  Unfortunately Microsoft never thought about it.  If you do String.split(mystr, vbCrLf) it will split on both, throw blank lines, add CR or LF onto bits at start of end, etc.  There's no way around that.

If a text file is actually CRLF separated, run cat -ve stemlist to see the endings in the Bash shell.  cr as ^M and lf as $

See https://stackoverflow.com/questions/3569997/how-to-find-out-line-endings-in-a-text-file

4.  VB Upgrade Wizard introduces CDbl into code for Strings

Which of course fails when you run it.  Do a global search.  This often happens where strings are joined with a "+".  You get cstr(cdbl(mystring)).

5.  Beware concatenating strings with "+" - you can get "Conversion from string "No" to type 'Double' is not valid."

Unlike VB6, VB.Net treats '+' as arithmetic, and tries to convert the string to a double implicitly!

6.  Enable break on exception

When you have exception handlers all over the place, you can't see where the error arises.  So enable debugging to stop when an exception is thrown.

In VB.Net 2008:

7.  Load files in a base test class run only once per run (AssemblyInitialize)

If you have files to open, you must do this, not at start of test class (because tests are multi-threaded) but at the start of the run.  Create a new test class, and call it 00BaseTest or something.  In it:

<TestClass()> _
<DeploymentItem("DIT", "DIT")> _
Public Class BaseTest

<AssemblyInitialize()> _
Public Shared Sub AssemblyInit(ByVal context As TestContext)

   Debug.Print("---------> Run Started")
   ...
End Sub

<AssemblyCleanup()> _
Public Shared Sub AssemblyCleanUp()

   Debug.Print("---------> Run complete")

   ...
End Sub

These run at start and end of a run, whether you run one test class or all of them. 

8.  Adding code to the installer (custom actions)

You can get some code to run when you install your software.  This is often used for copy protection code, to write a file somewhere.

1.  Right click on your project, Add New Item ... General ... Installer Class.  This will be created in your project.  This comes with a New() but nothing else.  What you need is the other events.  Paste this in:

Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
    MyBase.Install(stateSaver)
    MsgBox("hello")
End Sub

Public Overrides Sub Commit(ByVal savedState As System.Collections.IDictionary)
    MyBase.Commit(savedState)
End Sub

Public Overrides Sub Rollback(ByVal savedState As System.Collections.IDictionary)
    MyBase.Rollback(savedState)
End Sub

Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
    MyBase.Uninstall(savedState)
End Sub

You can add extra code after each MyBase.xxx.

2.  Make sure that you have a setup project in your solution.  This will make use of the Installer class.

3.  Right-click on Setup project, View, Custom Actions.  Right-Click on Install, Add Custom Action, then browse to Application Folder and click on Primary Output.  This tells the Setup where to find the Installer class above.

4.  Click on the Install | Primary output in the "Custom Actions" window which should already be open.  The properties at right should be accessible.  Make sure InstallerClass is set to true.  (Parameters passed into the custom action can be specified in CustomActionData)

5.  Build the setup project, wait until it has finished (look at screen bottom left: "Building...").  Until it has "Install" will be greyed out.

6.  Right-click on setup project, Install.  This will run, but halt at one point for the new messagebox.  Note that this may pop-under your install window.

See also:

9.  Multi-line comment/uncomment hot keys

You can use Ctrl+K, Ctrl+C and Ctrl+K, Ctrl+U to comment or uncomment selected lines of text.

10.  Short-circuit if-test evaluation - AndAlso / OrElse

You can evaluate only the first of a test and not check the rest using AndAlso or OrElse, just like Java && and ||

11.  "Go back" to previous place in code hot key

Ctrl + Shift + F2

12.  Initialize an array

Like this:

Dim myArray() as string = {"element0", "element1")

See also https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/how-to-initialize-an-array-variable

13.  Upgrade VB.Net in Visual Studio 2008 to VB.Net in Visual Studio 2013

Looks as if Microsoft never tested this.  Just don't.  VS2013 only seems to work on a clean machine.  The presence of VS2008 confuses it and nothing works.

The upgrade to VS 2010 is a breeze, on the other hand.  Use that instead.

14.  Error: "Executing the current test run will produce a new test run result, which will exceed the limit of 25 that is currently specified. If you continue, older test run results and their associated deployments will be deleted from the hard drive"

The TestResults directory can be deleted.

There's a parameter under Tools -> Options (show all) -> test tools -> test execution that  has a number dial that you can set it to how many you want to keep. I usually keep it set at 200.

15.  VS2010 - enable and run coverage

Walkthrough here.

To get rid of colouration, there is a button in the Code Coverage Results window "Show code coverage colouring", which is a toggle on and off.

NOTE: the icon at the far right of the Test Results window to open Code Coverage results.

NOTE: that you can run just one test, and see what it alone executed using coverage.

NOTE: the test project must be using .NET Framework 4 or later.

You can change the colours used.  See here.

16.  Notes on upgrading a VB6 project to DotNet

Looking back:

17.  Debug.print in Visual Studio 2010

The output will automatically appear in the Output Window.

Ensure that output is not being redirected to the Immediate Window instead: Tools -> Options -> Debugging -> General -> uncheck the option "Redirect all Output Window text to the Immediate Window".

18.  "Go to Definition" (F12) takes you to the worthless Object Browser, not the source code

This happened to me in my Test Project, clicking through to the code.  Within the main code project it worked fine.

The problem was that one project was using .Net Framework 4, the other 3.5.  Double-click on the project, Compile->Advanced Compile Options->Target Framework, make sure the same.  After it sorts itself out, it will work.

19. VS 2010 Change IDE Theme

The IDE color schemes are awful.  You can change them, however, by installing an extension.

20.  Reenable Intellisense

Tools->Options->Text Editor->All languages, make sure checked "Auto list members" and "Parameter information", and OK.

21.  Eclipse-style Ctrl-Click

Tools->Extension Manager, online, in Search (with quotes) "Productivity Power Tools".  Download and install, restart.

Then Options > Productivity Power Tools to see all the good stuff ('Ctrl-Click Go To Definition' is enabled by default).

22.  Block comment/uncomment

Select a block of text.  Then:

23.  Display call stack during debug

This is like the Eclipse call stack.

Halt on a breakpoint, do Debug -> Windows -> Call Stack, or Ctrl-L


Constructive feedback is welcomed to Roger Pearse.

This page has been online since 2nd March 2019.