• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

Getting Warning: "Variable" is used before it has been assigned a value. Not sure why

Getting Warning: "desktopArea" is used before it has been assigned a value.  Not sure why?


    Private Function GetScreen(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As Bitmap
        Dim hDeskDC As Integer
        Dim hTempDC As Integer
        Dim hBitmap As Integer
        Dim hTempBmp As Integer
        Dim desktopArea As Bitmap

        hDeskDC = CreateDC("DISPLAY", "", "", "")
        If hDeskDC Then
            hTempDC = CreateCompatibleDC(hDeskDC)
            If hTempDC Then
                hBitmap = CreateCompatibleBitmap(hDeskDC, Width, Height)
                If hBitmap Then
                    hTempBmp = SelectObject(hTempDC, hBitmap)
                    BitBlt(hTempDC, 0, 0, Width, Height, hDeskDC, X, Y, SRCCOPY)
                    desktopArea = Bitmap.FromHbitmap(New IntPtr(hBitmap))
                    DeleteObject(SelectObject(hTempDC, hTempBmp))
                End If
                DeleteDC(hTempDC)
            End If
            DeleteDC(hDeskDC)
        End If
        Return desktopArea
    End Function
0
vaultworld
Asked:
vaultworld
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Because it is possible that the If statements might fail and nothing ever gets assigned to "desktopArea".

You can fix the warning like this:

    Dim desktopArea As Bitmap = Nothing
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For VB.Net 2005 (and above), you don't need a low level API wrapper like that though.

Just use one of the Graphics.CopyFromScreen() overloads:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.copyfromscreen(VS.80).aspx

0
 
vaultworldAuthor Commented:
Thanks again,
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now