Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused Local Variables (Visual Studio 2012)

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?

like image 288
Steve Borland Avatar asked Apr 02 '14 13:04

Steve Borland


People also ask

How do you delete unused variables in Visual Studio?

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.

What does unused local variable mean in Visual Basic?

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.

Do unused variables take up memory?

Does an unused member variable take up memory? No (if it is "really" unused).

What is an unused variable?

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.

What are unused variables in Visual Basic?

- 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.

How do I know if a value is unused in Visual Studio?

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.

What is a local variable a method does not use?

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.

How does the compiler find unused value assignments?

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.


Video Answer


1 Answers

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!

like image 122
Steve Borland Avatar answered Oct 10 '22 21:10

Steve Borland