Results 1 to 3 of 3

Thread: Variable 'xxx' is used before it has been assigned a value

  1. #1

    Thread Starter
    New Member remusrigo's Avatar
    Join Date
    Aug 2011
    Location
    Romania, Timisoara
    Posts
    2

    Question Variable 'xxx' is used before it has been assigned a value

    Hi all

    I am using the following VB6 code and I get this error Variable 'osv' is used before it has been assigned a value in VB2010.
    I want to use this code/method for getting the version, I just want to fix this code:


    Code:
    Public Class Form1
    
       Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
        (lpVersionInformation As OSVERSIONINFO) As Long
    
       Private Structure OSVERSIONINFO
          Dim OSVSize As Integer
          Dim dwVerMajor As Integer
          Dim dwVerMinor As Integer
          Dim dwBuildNumber As Integer
          Dim PlatformID As Integer
          Dim szCSDVersion As String
       End Structure
    
       Private Const VER_PLATFORM_WIN32s = 0
       Private Const VER_PLATFORM_WIN32_WINDOWS = 1
       Private Const VER_PLATFORM_WIN32_NT = 2
    
       ' Returns the version of Windows that the user is running
       Public Function GetWindowsVersion() As String
          Dim osv As OSVERSIONINFO
    
          osv = 0
          osv.OSVSize = Len(OSVERSIONINFO)
    
          If GetVersionEx(osv) = 1 Then
             Select Case osv.PlatformID
                Case VER_PLATFORM_WIN32s
                   GetWindowsVersion = "Win32s on Windows 3.1"
                Case VER_PLATFORM_WIN32_NT
                   GetWindowsVersion = "Windows NT"
    
                   Select Case osv.dwVerMajor
                      Case 3
                         GetWindowsVersion = "Windows NT 3.5"
                      Case 4
                         GetWindowsVersion = "Windows NT 4.0"
                      Case 5
                         Select Case osv.dwVerMinor
                            Case 0
                               GetWindowsVersion = "Windows 2000"
                            Case 1
                               GetWindowsVersion = "Windows XP"
                            Case 2
                               GetWindowsVersion = "Windows Server 2003"
                         End Select
                      Case 6
                         Select Case osv.dwVerMinor
                            Case 0
                               GetWindowsVersion = "Windows Vista/Server 2008"
                            Case 1
                               GetWindowsVersion = "Windows 7/Server 2008 R2"
                         End Select
                   End Select
    
                Case VER_PLATFORM_WIN32_WINDOWS
                   Select Case osv.dwVerMinor
                      Case 0
                         GetWindowsVersion = "Windows 95"
                      Case 90
                         GetWindowsVersion = "Windows Me"
                      Case Else
                         GetWindowsVersion = "Windows 98"
                   End Select
             End Select
          Else
             GetWindowsVersion = "Unable to identify your version of Windows."
          End If
       End Function
    
       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
       ....
       call GetWindowsVersion to show win version
       ....
       End Sub
    End Class

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Variable 'xxx' is used before it has been assigned a value

    Why not use managed code, examples

    Code:
    Function IsWinVista() As Boolean
        Dim osInfo As System.OperatingSystem = System.Environment.OSVersion
        Return (osInfo.Platform = PlatformID.Win32NT AndAlso osInfo.Version.Major = 6)
    End Function
    Function IsWinXP() As Boolean
        Dim osInfo As System.OperatingSystem = System.Environment.OSVersion
        Return (osInfo.Platform = PlatformID.Win32NT AndAlso osInfo.Version.Major = 5 AndAlso osInfo.Version.Minor >= 1)
    End Function

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Variable 'xxx' is used before it has been assigned a value

    Quote Originally Posted by remusrigo View Post
    Hi all

    I am using the following VB6 code and I get this error Variable 'osv' is used before it has been assigned a value in VB2010.
    I want to use this code/method for getting the version, I just want to fix this code:


    Code:
    Public Class Form1
    
       Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
        (lpVersionInformation As OSVERSIONINFO) As Long
    
       Private Structure OSVERSIONINFO
          Dim OSVSize As Integer
          Dim dwVerMajor As Integer
          Dim dwVerMinor As Integer
          Dim dwBuildNumber As Integer
          Dim PlatformID As Integer
          Dim szCSDVersion As String
       End Structure
    
       Private Const VER_PLATFORM_WIN32s = 0
       Private Const VER_PLATFORM_WIN32_WINDOWS = 1
       Private Const VER_PLATFORM_WIN32_NT = 2
    
       ' Returns the version of Windows that the user is running
       Public Function GetWindowsVersion() As String
          Dim osv As OSVERSIONINFO
    
          osv = 0
          osv.OSVSize = Len(OSVERSIONINFO)
    
          If GetVersionEx(osv) = 1 Then
             Select Case osv.PlatformID
                Case VER_PLATFORM_WIN32s
                   GetWindowsVersion = "Win32s on Windows 3.1"
                Case VER_PLATFORM_WIN32_NT
                   GetWindowsVersion = "Windows NT"
    
                   Select Case osv.dwVerMajor
                      Case 3
                         GetWindowsVersion = "Windows NT 3.5"
                      Case 4
                         GetWindowsVersion = "Windows NT 4.0"
                      Case 5
                         Select Case osv.dwVerMinor
                            Case 0
                               GetWindowsVersion = "Windows 2000"
                            Case 1
                               GetWindowsVersion = "Windows XP"
                            Case 2
                               GetWindowsVersion = "Windows Server 2003"
                         End Select
                      Case 6
                         Select Case osv.dwVerMinor
                            Case 0
                               GetWindowsVersion = "Windows Vista/Server 2008"
                            Case 1
                               GetWindowsVersion = "Windows 7/Server 2008 R2"
                         End Select
                   End Select
    
                Case VER_PLATFORM_WIN32_WINDOWS
                   Select Case osv.dwVerMinor
                      Case 0
                         GetWindowsVersion = "Windows 95"
                      Case 90
                         GetWindowsVersion = "Windows Me"
                      Case Else
                         GetWindowsVersion = "Windows 98"
                   End Select
             End Select
          Else
             GetWindowsVersion = "Unable to identify your version of Windows."
          End If
       End Function
    
       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
       ....
       call GetWindowsVersion to show win version
       ....
       End Sub
    End Class
    Set Option Strict On and note more issues than the one you got.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width