简体   繁体   English

vb.net未使用的局部变量

[英]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. vb.net 2012给我下面的代码三个警告,说未使用的变量。 temp, filetype, and inde are all being warned as unused. temp,filetype和inde都被警告为未使用。

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. 但是我不明白为什么temp,inde和filetype被声明为未使用。

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: 另外,使用select case语句应该整理一下代码:

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
img_picture = Nothing

So you declare the variable here as null, and then never use it from what you have posted. 因此,您在此处将变量声明为null,然后再从已发布的内容中使用它。

Do you use the variable? 你使用变量吗? Just inside of the if switch? 只是在if开关内部? 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. 这确实是测试/检查 IMO 的更好方法 Although you will have to tweak it for VB.Net 虽然您将不得不为VB.Net进行调整

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. 我在检查msgbox中的值时添加了exit子命令,却忘了删除它。

Thanks to both answers for providing cleaner code though. 感谢这两个答案提供了更简洁的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM