Search code examples
vb.netsystem.drawing

vb.net unused local variable


Updated to include more code.

vb.net 2012 is giving me three warnings for the code below, saying unused variables. temp, filetype, and inde are all being warned as unused.

Private Sub Next_Image()
    ' TO Do -  is same, maybe make a function? Don't know if its worth it though
msgbox(My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension)
exit sub
    If changes = True Then
        If filesettings(2) = 0 Then
            If MessageBox.Show("Save Changes?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                Dim filetype As System.Drawing.Imaging.ImageFormat
                If My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension.ToLower = ".png" Then
                    filetype = System.Drawing.Imaging.ImageFormat.Png
                ElseIf My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension.ToLower = ".jpg" OrElse My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension.ToLower = ".jpeg" Then
                    filetype = System.Drawing.Imaging.ImageFormat.Jpeg
                ElseIf My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension.ToLower = ".bmp" Then
                    filetype = System.Drawing.Imaging.ImageFormat.Bmp
                ElseIf My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension.ToLower = ".gif" Then
                    filetype = System.Drawing.Imaging.ImageFormat.Gif
                ElseIf My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension.ToLower = ".tiff" Then
                    filetype = System.Drawing.Imaging.ImageFormat.Tiff
                End If
                img_picture = Nothing
                imageadjust.Save(filename, filetype)
            End If
        End If
    End If
    If Not img_picture.ImageLocation = Nothing Then
        Dim inde As Integer = files.IndexOf(filename)
        If inde = files.Count - 1 Then
            img_picture.ImageLocation = files(0)
        Else
            img_picture.ImageLocation = files(inde + 1)
        End If
        filename = img_picture.ImageLocation
        Me.Text = filename.Substring(filename.LastIndexOf("\") + 1) & " - Picture Viewer"

        If filesettings(0) = 1 Then
            img_picture.SizeMode = PictureBoxSizeMode.CenterImage
        ElseIf filesettings(0) = 2 Then
            img_picture.SizeMode = PictureBoxSizeMode.Zoom
        Else
            Dim temp As New Bitmap(filename)
            Me.img_picture.Refresh()
            If temp.Width > Me.img_picture.Width OrElse temp.Height > Me.img_picture.Height Then
                Me.img_picture.SizeMode = PictureBoxSizeMode.Zoom
            Else
                Me.img_picture.SizeMode = PictureBoxSizeMode.CenterImage
            End If
            temp.Dispose()
        End If
    End If
End Sub

Please excuse the code, I've just started adding things so some might be redundant. However I can't understand why temp, inde, and filetype are being declared unused.


Solution

  • Setting filetype to nothing (and then testing it later) should remove the error.

    Also, using a select case statement should tidy up the code a bit:

    Dim filetype As System.Drawing.Imaging.ImageFormat
    Select My.Computer.FileSystem.GetFileInfo(img_picture.ImageLocation).Extension.ToLower
        Case ".png"
            filetype = System.Drawing.Imaging.ImageFormat.Png
        Case ".jpg", ".jpeg"
            filetype = System.Drawing.Imaging.ImageFormat.Jpeg
        Case ".bmp"
            filetype = System.Drawing.Imaging.ImageFormat.Bmp
        Case ".gif"
            filetype = System.Drawing.Imaging.ImageFormat.Gif
        Case ".tif", ".tiff"
            filetype = System.Drawing.Imaging.ImageFormat.Tiff
        Case Else
            filetype = Nothing
    End Select
    If filetype IsNot Nothing Then
        img_picture = Nothing
        imageadjust.Save(filename, filetype)
    End If