Variable Declaration Problem

G

Guest

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 GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
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 = ""
lpReturnedString = New System.Text.StringBuilder(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
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?amNyb3VzZQ==?= said:
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 GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
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 = ""
lpReturnedString = New System.Text.StringBuilder(256)
lpNsize = 256
lpFileName = "C:\CPViewer_C\Controls.ini"

Where did you declare these varialbes?
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.

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

Armin Zingler

jcrouse said:
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 GetPrivateProfileString Lib
"kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
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 = ""
lpReturnedString = New System.Text.StringBuilder(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.


Did you declare the variables in both forms?


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
A

Arne Janning

jcrouse said:
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 GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
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 = ""
lpReturnedString = New System.Text.StringBuilder(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.

Hi John,

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

Private Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
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.StringBuilder(256)
intCharCount = GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then GetString = _
Left(objResult.ToString, intCharCount)
End Function

Another way is to use the IniReader-Library
http://www.mentalis.org/soft/class.qpx?id=6

or a smaller implementation:
http://www.developer.com/net/csharp/article.php/3287991

Cheers

Arne Janning
 
G

Guest

Where did you declare these variables

Isn't that what the following code does
Private Declare Auto Function GetPrivateProfileString Lib "kernel32"
(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As System.Text.StringBuilder,
ByVal lpNsize As Integer,
ByVal lpFileName As String) As Intege

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
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?amNyb3VzZQ==?= said:
Where did you declare these variables?

Isn't that what the following code does?


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

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
....
..
..
..
.... = GetPrivateProfileString(lpAppName, ...)
///
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.

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

Arne Janning

jcrouse said:
Where did you declare these variables?

Isn't that what the following code does?


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

Hi John,

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

Public Class IniFile
' API functions
Private Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, ByVal lpFileName As String) _
As Integer
Private Declare Ansi Function WritePrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Integer
Private Declare Ansi Function GetPrivateProfileInt _
Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal nDefault As Integer, _
ByVal lpFileName As String) As Integer
Private Declare Ansi Function FlushPrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName 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.StringBuilder(256)
intCharCount = GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then GetString = _
Left(objResult.ToString, intCharCount)
End Function

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

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

Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
' Writes a string to your INI file
WritePrivateProfileString(Section, Key, Value, strFilename)
Flush()
End Sub

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

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

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

End Class


At the beginning you "declare" the API-functions, e.g.
Private Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
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.StringBuilder(256)
intCharCount = GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, 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 lpApplicationName or lpKeyName as if
they were declared in a function:

This:
Private Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, ByVal lpFileName As String) _
As Integer

is different from that:

Private Function GetPrivateProfileString _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String,
...

'here you have access to lpApplicationName!!!

End Function


Cheers

Arne Janning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top