I'm ridding my code of all compiler warnings, as per my bosses request, when I came across some unused local variables. Naturally, I removed the ones which were legitimate, however I stumbled upon a couple which aren't as straight forward. (Variable names changed for security)
Dim strAAA As String = "aaaa" & strBBB & Now.ToString("yyyyMMddHHmmss") & ".doc"
If FUNCTION_NAME(strCCCC, strAAA) Then Return True
strAAA is allegedly an 'unused local variable' when it is clearly used directly below.
Even when I write it as follows:
Dim strAAA As String
strAAA = "ViewLet" & strBBB & Now.ToString("yyyyMMddHHmmss") & ".doc"
If FUNCTION_NAME(strTmpFileName, strAAA) Then Return True
The warning is still present.
Can anybody solve this mystery?
In the first preview build of Visual Studio 2019 version 16.10, Microsoft adds a Remove Unused References command. It can be invoked by right-clicking on the project name or the dependencies node in Solution Explorer and select the " Remove Unused References... " command from the menu.
Unused local variable: '<localvariablename>' A local variable in a procedure is declared but never used. One possibility is that there is a spelling mistake among the local variables in the procedure.
Does an unused member variable take up memory? No (if it is "really" unused).
Unused variables are a waste of space in the source; a decent compiler won't create them in the object file. Unused parameters when the functions have to meet an externally imposed interface are a different problem; they can't be avoided as easily because to remove them would be to change the interface.
- Visual Basic .NET Unused variables that really are?!? up in the list of warnings are being unused local variables. But they all are. Several of them are the ex variable used in a Try/Catch, and the Catch for each one references the ex exception variable.
Have any value assignment, variable, or parameter that isn't used. The unused value assignment or parameter appears faded out. The unused expression value generates a warning. Learn how to use an EditorConfig file to add file headers to existing files, projects, and solutions.
A method declares a local variable but does not use the variable except possibly as the recipient of an assignment statement. For analysis by this rule, the tested assembly must be built with debugging information and the associated program database (.pdb) file must be available.
The compiler also performs a flow analysis to find any unused value assignments. Unused value assignments fade out and a light bulb appears with a Quick Action to remove the redundant assignment. Unused variables with unknown values show a Quick Action suggestion to use discards instead.
Solved it.
There was a Return True
about 50 lines above.
This was always being hit, thus the variables were never being set.
Sloppy code from my predecessor I'm afraid!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With