已更新,以包含更多代码。
vb.net 2012给出了以下代码的三个警告,说明未使用的变量。 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
请原谅代码,我刚开始添加东西,所以有些可能是多余的。但是我无法理解为什么temp,inde和filetype被声明为未使用。
答案 0 :(得分:2)
将filetype设置为nothing(然后再进行测试)应该删除错误。
另外,使用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
答案 1 :(得分:1)
img_picture = Nothing
因此,您将此处的变量声明为null,然后从不使用已发布的内容。
你使用变量吗?就在if开关里面?无论哪种方式,该行看起来都不需要。
此外,通过字符串值测试您的扩展只有两个问题...它可能不起作用,它有点草率。如果您碰巧找到了一个不在您的情况下的文件/否则它将失败,并且还有其他可能性。
IMO {p> This is really the better way to test / check。虽然你必须为VB.Net调整它答案 2 :(得分:1)
添加完所有代码后,您可以看到我的错误。我在检查msgbox中的值时添加了一个exit子命令,忘了将其删除。
感谢两位提供更清晰代码的答案。