VB.Net Tips
7. Load files in a base test class run only once per run (AssemblyInitialize)
13. Upgrade VB.Net in Visual Studio 2008 to VB.Net in Visual Studio 2013 - Don't!
18. "Go to Definition" (F12) takes you to the worthless Object Browser, not the source code
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".
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()>
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
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)).
Unlike VB6, VB.Net treats '+' as arithmetic, and tries to convert the string to a double implicitly!
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:
Debug ... Exceptions
Tick "thrown" column for "Common Language Runtime Exceptions" and "Native run-time checks".
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.
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:
http://resources.esri.com/help/9.3/arcgisengine/dotnet/0df20605-b457-42d6-b63c-341a3824474a.htm
https://stackoverflow.com/questions/14361594/how-to-add-some-code-to-c-sharp-installer
You can use Ctrl+K, Ctrl+C and Ctrl+K, Ctrl+U to comment or uncomment selected lines of text.
You can evaluate only the first of a test and not check the rest using AndAlso or OrElse, just like Java && and ||
Ctrl + Shift + F2
Like this:
Dim myArray() as string = {"element0", "element1")
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.
Get a product key
Go to https://visualstudio.microsoft.com/vs/older-downloads/
You will have to create a few acccount. Then try again. It will tell you that you need a subscription or the VS "essentials" (which is free). Click to add that, then try again, and you'll get the list of downloads.
Download the .iso, right click on it in Windows Explorer in Win10, choose Mount. Then run the install. By all means launch it, choose Visual Basic.
If you have SETUP projects, you need to install an add-in for those, as they don't work out of the box. Download it from "https://marketplace.visualstudio.com/items?itemName=UnniRavindranathan-MSFT.MicrosoftVisualStudio2013InstallerProjects" and run.
You almost certainly need the Microsoft Visual Basic Powerpack for Winforms stuff. Instructions are here, get the installer from http://go.microsoft.com/fwlink/?LinkId=321343 The installer can also repair or uninstall.
Now you can try to open your project. Mine crashed when I did so, after installing the powerpack. PATHETIC!
The upgrade to VS 2010 is a breeze, on the other hand. Use that instead.
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.
Walkthrough here.
Under the Solution, expand Solution Items, find the test config (may be called several things). Double-click on it to bring up test settings.
Select Data and Diagnosis. Check "Code Coverage" and then the "Configure" button at the top of the list of "adaptors".
Select your code project and maybe your test project.
Click OK, Save As -> Save, Yes to replace.
Open the Test->Windows->Test List Editor. Check some tests, then run from the icon top left "Run checked tests"
Test->Windows->Code Coverage Results. Expand your way down to your code, then double click on the code you want.
Blue means line was run, beige means partly exercised path, red-brown means not travelled.
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.
Tools->Options, Environment->Fonts and Colors, look for "Coverage..." in the Display Items.
To set to the values used in eclipse, do: No coverage / Coverage not touched = 255, 160, 160. Partial coverage / Coverage partially touched = 255, 255, 128, Full /Coverage touched = 192, 255, 192.
Looking back:
You will have to upgrade to VB 2008, and no later, because the Upgrade Wizard is not included in VS 2010 onwards.
You are best advised to fix stuff in the VB6 code and upgrade, rather than try to fix them on the dotNet side. If you do you will end up with a non-working VB.Net project with a million errors, and you will give up.
Do the upgrade iteratively - try and upgrade, collect the errors, discard the upgrade, fix them in VB6, try the upgrade again. Always keep your project functional.
Feel free to comment out stuff. The front-end stuff may need a rewrite.
Get rid of Microsoft VisualBasic Powerpack. You can indeed upgrade it as far as VS2010, but no further.
VB2008 and VS2010 are good releases. VS2013 is complete crap.
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".
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.
The IDE color schemes are awful. You can change them, however, by installing an extension.
Tools -> Extension Manager, in "Search" enter (with quotes) "Visual Studio Color Theme Editor". When found, click to download and install and restart IDE.
New menu Theme.
See here for examples of choices. I went with Windows Aero.
Tools->Options->Text Editor->All languages, make sure checked "Auto list members" and "Parameter information", and OK.
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).
Select a block of text. Then:
Ctrl-K Ctrl-C = comment out
Ctrl-K Ctrl-U = uncomment
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.