0

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.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Michael Parr
  • 134
  • 3
  • 14

3 Answers3

2

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
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
  • I tried setting the filetype to something or nothing, but it seems to remain in the warning.... I just realised that the warnings are also showing for other variables used that were not displaying the issue before hand. – Michael Parr Apr 23 '13 at 04:30
  • And i think i will use select case. It looks much cleaner. – Michael Parr Apr 23 '13 at 04:37
  • 1
    Ahh idiot mistake - when testing i added a msgbox and an exit sub command. Of course everything under exit sub was never going to happen. You were right that setting is to nothing helped though. – Michael Parr Apr 23 '13 at 05:06
1
img_picture = Nothing

So you declare the variable here as null, and then never use it from what you have posted.

Do you use the variable? Just inside of the if switch? Either way that line does not look needed.

Also, testing your extension by string value only has two problems... It might not work and it is a little sloppy. If you happen to find a file that is not in your case / if else it will fail for one, and there are other possibilities.

This is really the better way to test / check, IMO. Although you will have to tweak it for VB.Net

Community
  • 1
  • 1
Austin T French
  • 5,022
  • 1
  • 22
  • 40
1

After adding all the code you can see my mistake. I added an exit sub command when checking a value in a msgbox and forgot to remove it.

Thanks to both answers for providing cleaner code though.

Michael Parr
  • 134
  • 3
  • 14