473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 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

Nov 20 '05 #1
6 1905
* "=?Utf-8?B?amNyb3VzZQ==?=" <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
jcrouse wrote:
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
Nov 20 '05 #3
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
Nov 20 '05 #4
"jcrouse" <an*******@discussions.microsoft.com> schrieb
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

Nov 20 '05 #5
* "=?Utf-8?B?amNyb3VzZQ==?=" <an*******@discussions.microsoft.com> scripsit:
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 Integer
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?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #6
jcrouse wrote:
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 Integer


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
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
3
by: baumann | last post by:
Hi all, in a .h header file i want to declare a var as extern struct mytype a; after I include the header file in a dot c file
7
by: gyan | last post by:
follwing code gives error: 1 #include<iostream.h> 2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 }
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
3
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.