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. VS2010 showing "unused variables"
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
stoneycurtis

8/22/2022 - Mon
kaufmed

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  : \
lludden

There is a return with no End IF.  
tipsybroom

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
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.
kaufmed

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

and as none of the code folloeing the RETURN will be executed, EVERY variable declared after the return is unused. Very clever, this compiler.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
stoneycurtis

ASKER
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
lludden

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
stoneycurtis

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