Variable Declaration Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jcrouse

    Variable Declaration Problem

    I have a project with a startup (Sub Main) module and two forms. Based on the logic in the Sub Main it calls one of the two forms. The two forms are almost identical. They both make calls into the same INI file using the following code:

    Private Declare Auto Function GetPrivateProfi leString Lib "kernel32" _
    (ByVal lpAppName As String, _
    ByVal lpKeyName As String, _
    ByVal lpDefault As String, _
    ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
    ByVal lpNsize As Integer, _
    ByVal lpFileName As String) As Integer

    Howerer, in one of the form’s code windows all of the following variables are underlined and it says their “undeclared. In the other form’s code window everything is fine. I’m lost here. It appears that I have the “Private Declare Auto Function…” at the top of BOTH forms, in the same exact place, right after the “# End Region” line of the “Windows Form Designer Generated Code” section.

    lpAppName = LookUp
    lpDefault = ""
    lpReturnedStrin g = New System.Text.Str ingBuilder(256)
    lpNsize = 256
    lpFileName = "C:\CPViewer_C\ Controls.ini"

    What am I missing here? I have Option Explicit turned “On”. As you can Imagine, if I turn it off, the underscores go away since there is no requirement to declare your variables ahead of time.

    Any Ideas,
    John

  • Herfried K. Wagner [MVP]

    #2
    Re: Variable Declaration Problem

    * "=?Utf-8?B?amNyb3VzZQ= =?=" <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
    > I have a project with a startup (Sub Main) module and two forms. Based on the logic in the Sub Main it calls one of the two forms. The two forms are almost identical. They both make calls into the same INI file using the following code:
    >
    > Private Declare Auto Function GetPrivateProfi leString Lib "kernel32" _
    > (ByVal lpAppName As String, _
    > ByVal lpKeyName As String, _
    > ByVal lpDefault As String, _
    > ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
    > ByVal lpNsize As Integer, _
    > ByVal lpFileName As String) As Integer
    >
    > Howerer, in one of the form’s code windows all of the following variables are underlined and it says their “undeclared. In the other form’s code window everything is fine. I’m lost here. It appears that I have the “Private Declare Auto Function…� at the top of BOTH forms, in the same exact place, right after the “# End Region� line of the “Windows Form Designer Generated Code� section.
    >
    > lpAppName = LookUp
    > lpDefault = ""
    > lpReturnedStrin g = New System.Text.Str ingBuilder(256)
    > lpNsize = 256
    > lpFileName = "C:\CPViewer_C\ Controls.ini"[/color]

    Where did you declare these varialbes?
    [color=blue]
    > What am I missing here? I have Option Explicit turned “On�. As
    > you can Imagine, if I turn it off, the underscores go away since there
    > is no requirement to declare your variables ahead of time.[/color]

    Right. 'Option Strict Off' doesn't require variables to be declared.
    With this option set to 'On', you will have to declare them.

    --
    Herfried K. Wagner [MVP]
    <URL:http://dotnet.mvps.org/>

    Comment

    • Arne Janning

      #3
      Re: Variable Declaration Problem

      jcrouse wrote:[color=blue]
      > I have a project with a startup (Sub Main) module and two forms. Based on the logic in the Sub Main it calls one of the two forms. The two forms are almost identical. They both make calls into the same INI file using the following code:
      >
      > Private Declare Auto Function GetPrivateProfi leString Lib "kernel32" _
      > (ByVal lpAppName As String, _
      > ByVal lpKeyName As String, _
      > ByVal lpDefault As String, _
      > ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
      > ByVal lpNsize As Integer, _
      > ByVal lpFileName As String) As Integer
      >
      > Howerer, in one of the form’s code windows all of the following variables are underlined and it says their “undeclared. In the other form’s code window everything is fine. I’m lost here. It appears that I have the “Private Declare Auto Function…” at the top of BOTH forms, in the same exact place, right after the “# End Region” line of the “Windows Form Designer Generated Code” section.
      >
      > lpAppName = LookUp
      > lpDefault = ""
      > lpReturnedStrin g = New System.Text.Str ingBuilder(256)
      > lpNsize = 256
      > lpFileName = "C:\CPViewer_C\ Controls.ini"
      >
      > What am I missing here? I have Option Explicit turned “On”. As you can Imagine, if I turn it off, the underscores go away since there is no requirement to declare your variables ahead of time.[/color]

      Hi John,

      this cannot work. Look at the following code, it should be clear how to
      use it.

      Private Declare Ansi Function GetPrivateProfi leString _
      Lib "kernel32.d ll" Alias "GetPrivateProf ileStringA" _
      (ByVal lpApplicationNa me As String, _
      ByVal lpKeyName As String, ByVal lpDefault As String, _
      ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
      ByVal nSize As Integer, ByVal lpFileName As String) _
      As Integer


      Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String
      ' Returns a string from your INI file
      Dim intCharCount As Integer
      Dim objResult As New System.Text.Str ingBuilder(256)
      intCharCount = GetPrivateProfi leString(Sectio n, Key, _
      [Default], objResult, objResult.Capac ity, strFilename)
      If intCharCount > 0 Then GetString = _
      Left(objResult. ToString, intCharCount)
      End Function

      Another way is to use the IniReader-Library
      Free software components for C#, VB.NET and other .NET languages.


      or a smaller implementation:


      Cheers

      Arne Janning

      Comment

      • jcrouse

        #4
        Re: Variable Declaration Problem

        Where did you declare these variables

        Isn't that what the following code does
        [color=blue]
        > Private Declare Auto Function GetPrivateProfi leString Lib "kernel32"
        > (ByVal lpAppName As String,
        > ByVal lpKeyName As String,
        > ByVal lpDefault As String,
        > ByVal lpReturnedStrin g As System.Text.Str ingBuilder,
        > ByVal lpNsize As Integer,
        > ByVal lpFileName As String) As Intege[/color]

        I beleive this will explain WHERE I have the declaration statement in BOTH forms

        Howerer, in one of the forms code windows all of the following variables are underlined and it says their undeclared. In the other forms code window everything is fine. I'm lost here. It appears that I have the "Private Declare Auto Function" at the top of BOTH forms, in the same exact place, right after the "# End Region" line of the "Windows Form Designer Generated Code" section

        Thanks for the reply
        John

        Comment

        • Armin Zingler

          #5
          Re: Variable Declaration Problem

          "jcrouse" <anonymous@disc ussions.microso ft.com> schrieb[color=blue]
          > I have a project with a startup (Sub Main) module and two forms.
          > Based on the logic in the Sub Main it calls one of the two forms. The
          > two forms are almost identical. They both make calls into the same
          > INI file using the following code:
          >
          > Private Declare Auto Function GetPrivateProfi leString Lib
          > "kernel32" _
          > (ByVal lpAppName As String, _
          > ByVal lpKeyName As String, _
          > ByVal lpDefault As String, _
          > ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
          > ByVal lpNsize As Integer, _
          > ByVal lpFileName As String) As Integer
          >
          > Howerer, in one of the form’s code windows all of the following
          > variables are underlined and it says their “undeclared. In the other
          > form’s code window everything is fine. I’m lost here. It appears that
          > I have the “Private Declare Auto Function…” at the top of BOTH forms,
          > in the same exact place, right after the “# End Region” line of the
          > “Windows Form Designer Generated Code” section.
          >
          > lpAppName = LookUp
          > lpDefault = ""
          > lpReturnedStrin g = New System.Text.Str ingBuilder(256)
          > lpNsize = 256
          > lpFileName = "C:\CPViewer_C\ Controls.ini"
          >
          > What am I missing here? I have Option Explicit turned “On”. As you
          > can Imagine, if I turn it off, the underscores go away since there is
          > no requirement to declare your variables ahead of time.[/color]


          Did you declare the variables in both forms?


          --
          Armin

          How to quote and why:



          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Variable Declaration Problem

            * "=?Utf-8?B?amNyb3VzZQ= =?=" <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
            > Where did you declare these variables?
            >
            > Isn't that what the following code does?
            >[color=green]
            > > Private Declare Auto Function GetPrivateProfi leString Lib "kernel32" _
            > > (ByVal lpAppName As String, _
            > > ByVal lpKeyName As String, _
            > > ByVal lpDefault As String, _
            > > ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
            > > ByVal lpNsize As Integer, _
            > > ByVal lpFileName As String) As Integer[/color]
            >
            > I beleive this will explain WHERE I have the declaration statement in BOTH forms.[/color]

            No, this code doesn't declare any variables, it just declares a function
            with named parameters. If you want to declare the variables, you may
            want to use something like this:

            \\\
            Public lpAppName As String
            ....
            ..
            ..
            ..
            .... = GetPrivateProfi leString(lpAppN ame, ...)
            ///
            [color=blue]
            > Howerer, in one of the forms code windows all of the following
            > variables are underlined and it says their undeclared. In the other
            > forms code window everything is fine. I'm lost here.[/color]

            Are you sure there is no 'Option Explicit Off' on top of the other code
            file?

            --
            Herfried K. Wagner [MVP]
            <URL:http://dotnet.mvps.org/>

            Comment

            • Arne Janning

              #7
              Re: Variable Declaration Problem

              jcrouse wrote:
              [color=blue]
              > Where did you declare these variables?
              >
              > Isn't that what the following code does?
              >[color=green]
              > > Private Declare Auto Function GetPrivateProfi leString Lib "kernel32" _
              > > (ByVal lpAppName As String, _
              > > ByVal lpKeyName As String, _
              > > ByVal lpDefault As String, _
              > > ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
              > > ByVal lpNsize As Integer, _
              > > ByVal lpFileName As String) As Integer[/color]
              >
              > I beleive this will explain WHERE I have the declaration statement in BOTH forms.
              >
              > Howerer, in one of the forms code windows all of the following variables are underlined and it says their undeclared. In the other forms code window everything is fine. I'm lost here. It appears that I have the "Private Declare Auto Function" at the top of BOTH forms, in the same exact place, right after the "# End Region" line of the "Windows Form Designer Generated Code" section.
              >
              >
              > Thanks for the reply,
              > John[/color]

              Hi John,

              look at this implementation of an IniFileHelper-Class in Vb.NET:

              Public Class IniFile
              ' API functions
              Private Declare Ansi Function GetPrivateProfi leString _
              Lib "kernel32.d ll" Alias "GetPrivateProf ileStringA" _
              (ByVal lpApplicationNa me As String, _
              ByVal lpKeyName As String, ByVal lpDefault As String, _
              ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
              ByVal nSize As Integer, ByVal lpFileName As String) _
              As Integer
              Private Declare Ansi Function WritePrivatePro fileString _
              Lib "kernel32.d ll" Alias "WritePrivatePr ofileStringA" _
              (ByVal lpApplicationNa me As String, _
              ByVal lpKeyName As String, ByVal lpString As String, _
              ByVal lpFileName As String) As Integer
              Private Declare Ansi Function GetPrivateProfi leInt _
              Lib "kernel32.d ll" Alias "GetPrivateProf ileIntA" _
              (ByVal lpApplicationNa me As String, _
              ByVal lpKeyName As String, ByVal nDefault As Integer, _
              ByVal lpFileName As String) As Integer
              Private Declare Ansi Function FlushPrivatePro fileString _
              Lib "kernel32.d ll" Alias "WritePrivatePr ofileStringA" _
              (ByVal lpApplicationNa me As Integer, _
              ByVal lpKeyName As Integer, ByVal lpString As Integer, _
              ByVal lpFileName As String) As Integer
              Dim strFilename As String

              ' Constructor, accepting a filename
              Public Sub New(ByVal Filename As String)
              strFilename = Filename
              End Sub

              ' Read-only filename property
              ReadOnly Property FileName() As String
              Get
              Return strFilename
              End Get
              End Property

              Public Function GetString(ByVal Section As String, _
              ByVal Key As String, ByVal [Default] As String) As String
              ' Returns a string from your INI file
              Dim intCharCount As Integer
              Dim objResult As New System.Text.Str ingBuilder(256)
              intCharCount = GetPrivateProfi leString(Sectio n, Key, _
              [Default], objResult, objResult.Capac ity, strFilename)
              If intCharCount > 0 Then GetString = _
              Left(objResult. ToString, intCharCount)
              End Function

              Public Function GetInteger(ByVa l Section As String, _
              ByVal Key As String, ByVal [Default] As Integer) As Integer
              ' Returns an integer from your INI file
              Return GetPrivateProfi leInt(Section, Key, _
              [Default], strFilename)
              End Function

              Public Function GetBoolean(ByVa l Section As String, _
              ByVal Key As String, ByVal [Default] As Boolean) As Boolean
              ' Returns a boolean from your INI file
              Return (GetPrivateProf ileInt(Section, Key, _
              CInt([Default]), strFilename) = 1)
              End Function

              Public Sub WriteString(ByV al Section As String, _
              ByVal Key As String, ByVal Value As String)
              ' Writes a string to your INI file
              WritePrivatePro fileString(Sect ion, Key, Value, strFilename)
              Flush()
              End Sub

              Public Sub WriteInteger(By Val Section As String, _
              ByVal Key As String, ByVal Value As Integer)
              ' Writes an integer to your INI file
              WriteString(Sec tion, Key, CStr(Value))
              Flush()
              End Sub

              Public Sub WriteBoolean(By Val Section As String, _
              ByVal Key As String, ByVal Value As Boolean)
              ' Writes a boolean to your INI file
              WriteString(Sec tion, Key, CStr(CInt(Value )))
              Flush()
              End Sub

              Private Sub Flush()
              ' Stores all the cached changes to your INI file
              FlushPrivatePro fileString(0, 0, 0, strFilename)
              End Sub

              End Class


              At the beginning you "declare" the API-functions, e.g.
              Private Declare Ansi Function GetPrivateProfi leString _
              Lib "kernel32.d ll" Alias "GetPrivateProf ileStringA" _
              (ByVal lpApplicationNa me As String, _
              ByVal lpKeyName As String, ByVal lpDefault As String, _
              ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
              ByVal nSize As Integer, ByVal lpFileName As String) _
              As Integer

              Afterwards you write a "wrapper"-function around that:

              Public Function GetString(ByVal Section As String, _
              ByVal Key As String, ByVal [Default] As String) As String
              ' Returns a string from your INI file
              Dim intCharCount As Integer
              Dim objResult As New System.Text.Str ingBuilder(256)
              intCharCount = GetPrivateProfi leString(Sectio n, Key, _
              [Default], objResult, objResult.Capac ity, strFilename)
              If intCharCount > 0 Then GetString = _
              Left(objResult. ToString, intCharCount)
              End Function

              The client is supposed to use the GetString-method.

              You cannot access the variables lpApplicationNa me or lpKeyName as if
              they were declared in a function:

              This:
              Private Declare Ansi Function GetPrivateProfi leString _
              Lib "kernel32.d ll" Alias "GetPrivateProf ileStringA" _
              (ByVal lpApplicationNa me As String, _
              ByVal lpKeyName As String, ByVal lpDefault As String, _
              ByVal lpReturnedStrin g As System.Text.Str ingBuilder, _
              ByVal nSize As Integer, ByVal lpFileName As String) _
              As Integer

              is different from that:

              Private Function GetPrivateProfi leString _
              (ByVal lpApplicationNa me As String, _
              ByVal lpKeyName As String,
              ...

              'here you have access to lpApplicationNa me!!!

              End Function


              Cheers

              Arne Janning

              Comment

              Working...