Link to home
Start Free TrialLog in
Avatar of stoneycurtis
stoneycurtis

asked on

Warnings about "unused variables" in .Net 4.0 Framework

Hello,
I'm playing around with Visual Basic code to generate Sudoku puzzles, the code was originally written for the .Net 2.0 Framework and it works correctly there. I'm building my version as a Visual Studio 2010 project using the .Net 4.0 Framework. The only problem I've ran into are the 2 warnings about "unused variable" as shown in the screenshot. When I run the application this portion of the code will not work correctly and the board will not initialize.
Why can't .Net 4.0 compile this correctly?
I'm quite new at VB and .Net programming so if this is a dumb question I apologize in advance.
Thanks for any help you might give. User generated image
Avatar of kaufmed
kaufmed
Flag of United States of America image

An "unused variable" is just like it sounds--you have told the compiler, "create this variable for me," but you haven't actually read or written to that variable anywhere else in your code other than the line it is declared on. You code should compile even with unused variables, but you generally don't want to ask for memory if you're not going to use it.

As for why your code doesn't work correctly, you need to step through your code or indicate what behavior you are expecting and what behavior you are experiencing. "This portion of the code will not work correctly and the board will not initialize" tells us nothing  : \
There is a return with no End IF.  
you can decide what happens when an "unused variable" is detected

in your project properties, there is a tab called "compile". htere you can decide what happens at "unused variable"

Error = no compile
Warning
None = nothing happens
Unbenannt.png
Avatar of stoneycurtis
stoneycurtis

ASKER

Thanks to all for responding so quickly;

To lluden: There is an EndIf folowing Next, I just didn't get it cropped into the screenshot.

To kaufmed: The code running in .Net 2.0 will populate a Sudoku grid with a set of numbers saved in a text file as an 81 digit string. Viewing the code in .Net 2.0 there are no "unused variables", yet the code generates the warnings with .Net 4.0.

To tipsybroom: If I set the selection to 'None', the warnings do not appear, but the application will still not populate the grid with the string from the saved file.

The code shows no warnings in .Net 2.0 and populates the grid, .Net 4.0 does show the warnings and will not populate the grid.
Piggy-backing on lludden's comment, why exactly do you have a Return statement as the first line of your Else? None of the code after the Return will ever be executed if you leave the Return at that location.
and as none of the code folloeing the RETURN will be executed, EVERY variable declared after the return is unused. Very clever, this compiler.
It's my understanding that If the dialog picker can't save the target file then the Else->Return fires.

Private Sub OpenToolStripMenuItem_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles OpenToolStripMenuItem.Click

        If GameStarted Then
            Dim response As MsgBoxResult = _
            MessageBox.Show("Do you want to save current game?", _
                            "Save current game", _
                            MessageBoxButtons.YesNoCancel, _
                            MessageBoxIcon.Question)

            If response = MsgBoxResult.Yes Then
                SaveGameToDisk(False)
            ElseIf response = MsgBoxResult.Cancel Then
                Return
            End If
        End If

        '---load the game from disk---
        Dim fileContents As String
        Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "SDO files (*.sdo)|*.sdo|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = False

        If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            fileContents = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
            ToolStripStatusLabel1.Text = openFileDialog1.FileName
            saveFileName = openFileDialog1.FileName
        Else
            Return
        End If

        StartNewGame()

        '---initialize the board---
        Dim counter As Short = 0
        For row As Integer = 1 To 9
            For col As Integer = 1 To 9
                Try
                    If CInt(fileContents(counter).ToString()) <> 0 Then
                        SetCell(col, row, CInt(fileContents(counter).ToString()), 0)
                    End If
                Catch ex As Exception
                    MsgBox("File does not contain a valid Sudoku puzzle")
                    Exit Sub
                End Try
                counter += 1
            Next
        Next
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I finally just cut this sub out of the project, pasted it into notepad++, set the language to Visual Basic, then copied it back into the same project where it compiles correctly. No warnings, and when I run the application, the grid populates correctly.