Home > Archive > Visual Basic > April 2006 > why the array always returns nothing( about ini file)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
why the array always returns nothing( about ini file)
|
|
| zy@xinhuanet.com 2006-04-26, 3:56 am |
| I am newbie for VB
I want to read all values in the section of "Database"
but this array named sArray() always returns nothing
Any suggestions on what to do greatly appreciated.
Thanks
ZY
-------code----
Private Declare Function GetPrivateProfileSection Lib "KERNEL32" Alias
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal
lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As
String) As Long
Private Sub Command1_Click()
Dim sarray1() As String
Dim path1 As String
path1 = App.Path & "/xj.ini"
sarray1 = GetInfoSection("Database", path1)
jk = UBound(sarray1)
ReDim sarray1(1 To jk)
For i = 1 To jk
uu = uu & sarray1(i)
Next i
Text1 = uu
End Sub
Public Function GetInfoSection(strSection As String, strIniFile As
String) As String()
Dim strReturn As String * 32767
Dim strTmp As String
Dim nStart As Integer, nEnd As Integer, i As Integer
Dim sArray() As String
path1 = App.Path & "/xj.ini"
Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn),
path1)
strTmp = strReturn
i = 1
Do While strTmp <> ""
nStart = nEnd + 1
nEnd = InStr(nStart, strReturn, vbNullChar)
strTmp = Mid$(strReturn, nStart, nEnd - nStart)
If Len(strTmp) > 0 Then
ReDim Preserve sArray(1 To i)
sArray(i) = strTmp '¡ö sArray(i) is null
i = i + 1
'Text2 = strReturn
'Text2 = Replace(strReturn, Chr(0), " ")
'Text2 = strTmp
End If
Loop
GetInfoSection = sArray
End Function
-------code----
| |
| Norm Cook 2006-04-26, 7:56 am |
| Xref: TK2MSFTNGP01.phx.gbl microsoft.public.vb.general.discussion:589922
Try Karl's Ini class:
http://vb.mvps.org/samples/project.asp?id=kpIni
<zy@xinhuanet.com> wrote in message
news:tl8u429v1vuca2c6k1vj9havq75snnhs53@
msnews.microsoft.com...
> I am newbie for VB
> I want to read all values in the section of "Database"
> but this array named sArray() always returns nothing
>
> Any suggestions on what to do greatly appreciated.
> Thanks
> ZY
>
> -------code----
> Private Declare Function GetPrivateProfileSection Lib "KERNEL32" Alias
> "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal
> lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As
> String) As Long
>
>
>
>
> Private Sub Command1_Click()
> Dim sarray1() As String
> Dim path1 As String
>
> path1 = App.Path & "/xj.ini"
> sarray1 = GetInfoSection("Database", path1)
>
> jk = UBound(sarray1)
> ReDim sarray1(1 To jk)
>
> For i = 1 To jk
> uu = uu & sarray1(i)
> Next i
>
> Text1 = uu
> End Sub
>
>
>
> Public Function GetInfoSection(strSection As String, strIniFile As
> String) As String()
> Dim strReturn As String * 32767
> Dim strTmp As String
> Dim nStart As Integer, nEnd As Integer, i As Integer
> Dim sArray() As String
>
>
> path1 = App.Path & "/xj.ini"
>
> Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn),
> path1)
>
> strTmp = strReturn
> i = 1
> Do While strTmp <> ""
> nStart = nEnd + 1
> nEnd = InStr(nStart, strReturn, vbNullChar)
> strTmp = Mid$(strReturn, nStart, nEnd - nStart)
> If Len(strTmp) > 0 Then
> ReDim Preserve sArray(1 To i)
>
> sArray(i) = strTmp '¡ö sArray(i) is null
> i = i + 1
> 'Text2 = strReturn
> 'Text2 = Replace(strReturn, Chr(0), " ")
> 'Text2 = strTmp
>
> End If
> Loop
>
> GetInfoSection = sArray
> End Function
> -------code----
>
>
>
>
| |
| zy@xinhuanet.com 2006-04-26, 9:56 pm |
| sorry, I make a mistake obviously
ReDim sarray1(1 To jk) ==>
ReDim Preserve sarray1(1 To jk)
then ok!
It has been ok
but I get a message "walk off an array" in debug mode,when
mouse move over "sArray(i)" (the breakpoint is below)
---------------
<snip>
Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn),
path1)
strTmp = strReturn
i = 1
Do While strTmp <> ""
nStart = nEnd + 1
nEnd = InStr(nStart, strReturn, vbNullChar)
strTmp = Mid$(strReturn, nStart, nEnd - nStart)
If Len(strTmp) > 0 Then
ReDim Preserve sArray(1 To i)
sArray(i) = strTmp
i = i + 1
'Text2 = sArray(i) '¡ñ walk off an array too when add this statement
End If '¡ñ a breakpoint
Loop
GetInfoSection = sArray
End Function
on Wed, 26 Apr 2006 08:24:04 -0500 , "Norm Cook"
<normcookNOSPAM@cableone.net> wrote:
--
>Try Karl's Ini class:
>http://vb.mvps.org/samples/project.asp?id=kpIni
| |
| Mike Williams 2006-04-27, 3:56 am |
| <zy@xinhuanet.com> wrote in message
news:b0d052pg9e3k8n1u6bi6nul8jo1s1m3l48@
msnews.microsoft.com...
I get message Walk off Array in Debug mode:
> ReDim Preserve sArray(1 To i)
> sArray(i) = strTmp
> i = i + 1
> Text2 = sArray(i) 'walk off an array when add this statement
I don't know for sure what "walk off array" means because there is no such
error message in English versions of VB. I assume that you are in China
because Outlook Express keeps asking me if I want to download the Chinese
language pack whenever I click one of your messages. I assume that "walk off
array" in your version of VB means the same as "subscript out of range".
Basically it means you are attempting to access an element of an array that
does not actually exist. If you have a look at the extract of your code
above you will see that you are resizing your array so that it contains
elements numbered from 1 to i in the ReDim statement. The next line of code
then puts ther contents of the strTemp variable into element i of sArray
(which is the last element in the array). Then you are adding 1 to i (i = i
+ 1). The next line attempts to display the contents of element i of sArray
in a text box, but that element does not exist (because at that point i
contains a number that is one more than the number of the last element in
your array). That is why you are getting the error. If you move the line i =
i + 1 so that it comes after the Text2 line then all will be okay.
One other thing, although it is not part of your problem, is you are using
Text2 = something. That is not a good idea generally. You should always
explicitly tell VB which property you want to change instead of simply
relying on that property being the default property. In other words, you
should use Text2.Text = something. By the way, what version of VB are you
using? If it is VB6 then you can use the Split function to do all the above
job in a single line of code. A|lso, one other thing, your code is going
through a string splitting it up into substrings at points where it finds a
vbNullChar. Is that what you really want to do? Or do you perhaps want to
look for "space" characters, in which case you should use Space$(1) instead
of vbNullChar.
Post again if you still have problems.
Mike
| |
| zy@xh.com 2006-04-27, 9:56 pm |
| I'm Sorry for my poor english.
"subscript out of range" is really what I want to say
I wondered why there is a error of "subscript out of range" in
debugging mode, meanwhile it work well.
Is there a certain difference between debugging mode and Running
mode?
---Modified code----
<snip>
Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn),
path1)
strTmp = strReturn
i = 1
Do While strTmp <> ""
nStart = nEnd + 1
nEnd = InStr(nStart, strReturn, vbNullChar)
strTmp = Mid$(strReturn, nStart, nEnd - nStart)
If Len(strTmp) > 0 Then
ReDim Preserve sArray(1 To i)
sArray(i) = strTmp '¡ö I get a message "subscript out of
range" in debugging mode,when mouse move over "sArray(i)" ¡ö
i = i + 1
End If
Loop '¡ñ a breakpoint
on Thu, 27 Apr 2006 09:56:08 +0100 , "Mike Williams"
<Mike@WhiskyAndCoke.com> wrote:
--
>I don't know for sure what "walk off array" means because there is no such
>error message in English versions of VB. I assume that you are in China
>because Outlook Express keeps asking me if I want to download the Chinese
>language pack whenever I click one of your messages. I assume that "walk off
>array" in your version of VB means the same as "subscript out of range".
>
>Basically it means you are attempting to access an element of an array that
>does not actually exist. If you have a look at the extract of your code
>above you will see that you are resizing your array so that it contains
>elements numbered from 1 to i in the ReDim statement. The next line of code
>then puts ther contents of the strTemp variable into element i of sArray
>(which is the last element in the array). Then you are adding 1 to i (i = i
>+ 1). The next line attempts to display the contents of element i of sArray
>in a text box, but that element does not exist (because at that point i
>contains a number that is one more than the number of the last element in
>your array). That is why you are getting the error. If you move the line i =
>i + 1 so that it comes after the Text2 line then all will be okay.
>
>One other thing, although it is not part of your problem, is you are using
>Text2 = something. That is not a good idea generally. You should always
>explicitly tell VB which property you want to change instead of simply
>relying on that property being the default property. In other words, you
>should use Text2.Text = something. By the way, what version of VB are you
>using? If it is VB6 then you can use the Split function to do all the above
>job in a single line of code. A|lso, one other thing, your code is going
>through a string splitting it up into substrings at points where it finds a
>vbNullChar. Is that what you really want to do? Or do you perhaps want to
>look for "space" characters, in which case you should use Space$(1) instead
>of vbNullChar.
>
>Post again if you still have problems.
>
>Mike
>
| |
| Mike Williams 2006-04-28, 3:56 am |
| <zy@xh.com> wrote in message
news:fp03521fll85mc68ircofu0fqgegghnamh@
msnews.microsoft.com...
> I wondered why there is a error of "subscript out
> of range" in debugging mode, meanwhile it work well.
I'm not quite sure whether that means your problem is fixed, or if perhaps
you still have a problem? Meanwhile, you have not answered any of the
questions that I asked my previous post!
Mike
| |
| Mike Williams 2006-04-29, 6:56 pm |
| <zy@xh.com> wrote in message
news:7gn652d30m6ocat16vdun8dbqqg80oufja@
msnews.microsoft.com...
> perhaps, The error I mentioned in previous post have relations
> with mechanism of debugging mode?
I'm still not quite sure what you mean. Are you getting an error in
debugging mode that you are not getting when you run the program as a
compiled exe? If so then check that your ini file is actually in the correct
path in both cases.
There are various things that need attention in your code, but if I were you
I would definitely start by setting up your app so that VB forces you to
explicitly declare variables before you are allowed to use them. You should
always declare your variables before use (for all sorts of good reasons) and
if you place the line "Option Explicit" (without the quotes, of course) as
the first line of your Form code, immediately before your Private Declare
Function GetPrivateProfileSection . . . line, then VB will tell you when it
comes across variables you have not declared. That will pinpoint all sorts
of possible problems for you. In fact you would be better off making VB
automatically put the "Option Explicit" in your code for you each time you
start a new project. You can do this by using the Tools / Options menu and
clicking the Editor tab and place a tick in the little box against "Require
variable declaration".
By the way, the GetPrivateProfileSection API is really only provided for
backwards compatibility for 16 bit applications. These days the preferred
place to store data such as your initialisation data is in the Windows
registry instead of in an ini file. However, quite a lot of people still
prefer to use ini files so I suppose it's okay if you would personally
prefer it that way. Since you're using VB6 you can get the data returned by
GetPrivateProfileSection (which signifies the end of the returned data with
more than one vbNullChar) into a VB string array with a "one liner", which
you can use instead of your current loop code. So, instead of your current
code (as follows) . . .
Do While strTmp <> ""
nStart = nEnd + 1
nEnd = InStr(nStart, strReturn, vbNullChar)
strTmp = Mid$(strReturn, nStart, nEnd - nStart)
If Len(strTmp) > 0 Then
ReDim Preserve sArray(1 To i)
sArray(i) = strTmp
i = i + 1
End If
Loop
.. . . you can use this . . .
sArray = Split(Left$(strTmp, InStr(strTmp, _
String$(2, Chr$(0))) - 1), Chr$(0))
This will return a zero based array, so your code which loops through the
resultant array should be changed to the following (which is a good thing to
do anyway):
For i = LBound(sarray1) To UBound(sarray1)
Also, you are passing a path and file (your ini file) to your GetInfoSection
function as its strIniFile parameter and then you are failing to actually
use that parameter in your function code. Instead, you are creating the path
and file name again inside the function, which really doesn't make sense and
will of course be a source of error if the two strings don't match. If you
pass a function a parameter then you really should use that parameter
instead of doing what you are currently doing. Also, when creating a path by
appending a filename to the string returned by App.Path you should really
check whether or not it already contains a final backslash before deciding
to add one yourself (as you are doing in your App.Path & "/xj.ini" code.
This is because if your App's path happens to be a root directory ("c:\" for
example) then it will already end with a backslash and if you add another
one then your path will be incorrect. Admittedly it is unlikely that your
app's path is in a root directory, but it's worht checking for. By the way,
you should really be using a backslash (\.xj.ini) instead of the forward
slash (/xj.ini).
There are other things about your code that need mentioning, but first I
think you should use Option Explicit and fix the problems it finds, and
attend to the other things I have mentioned here, and then post again if you
still have problems.
Mike
| |
| zy@xh.com 2006-04-30, 3:56 am |
| Grateful thanks for your detailed instructions :-)
>By the way, the GetPrivateProfileSection API is really only provided for
>backwards compatibility for 16 bit applications.
>Also, when creating a path by appending a
>filename to the string returned by App.Path
> you should really check whether or not it
>already contains a final backslash before
>deciding to add one yourself (as you are
>doing in your App.Path & "/xj.ini" code.
>This is because if your App's path happens
>to be a root directory ("c:\" for
>example) then it will already end with a
>backslash and if you add another
>one then your path will be incorrect.
on Sat, 29 Apr 2006 18:48:52 +0100 , "Mike Williams"
<Mike@WhiskyAndCoke.com> wrote:
--
<zy@xh.com> wrote in message
> perhaps, The error I mentioned in previous post have relations
> with mechanism of debugging mode?
I'm still not quite sure what you mean. Are you getting an error in
debugging mode that you are not getting when you run the program as a
compiled exe? If so then check that your ini file is actually in the
correct
path in both cases.
There are various things that need attention in your code, but if I
were you
I would definitely start by setting up your app so that VB forces you
to
explicitly declare variables before you are allowed to use them. You
should
always declare your variables before use (for all sorts of good
reasons) and
if you place the line "Option Explicit" (without the quotes, of
course) as
the first line of your Form code, immediately before your Private
Declare
Function GetPrivateProfileSection . . . line, then VB will tell you
when it
comes across variables you have not declared. That will pinpoint all
sorts
of possible problems for you. In fact you would be better off making
VB
automatically put the "Option Explicit" in your code for you each time
you
start a new project. You can do this by using the Tools / Options menu
and
clicking the Editor tab and place a tick in the little box against
"Require
variable declaration".
By the way, the GetPrivateProfileSection API is really only provided
for
backwards compatibility for 16 bit applications. These days the
preferred
place to store data such as your initialisation data is in the Windows
registry instead of in an ini file. However, quite a lot of people
still
prefer to use ini files so I suppose it's okay if you would personally
prefer it that way. Since you're using VB6 you can get the data
returned by
GetPrivateProfileSection (which signifies the end of the returned data
with
more than one vbNullChar) into a VB string array with a "one liner",
which
you can use instead of your current loop code. So, instead of your
current
code (as follows) . . .
Do While strTmp <> ""
nStart = nEnd + 1
nEnd = InStr(nStart, strReturn, vbNullChar)
strTmp = Mid$(strReturn, nStart, nEnd - nStart)
If Len(strTmp) > 0 Then
ReDim Preserve sArray(1 To i)
sArray(i) = strTmp
i = i + 1
End If
Loop
.. . . you can use this . . .
sArray = Split(Left$(strTmp, InStr(strTmp, _
String$(2, Chr$(0))) - 1), Chr$(0))
This will return a zero based array, so your code which loops through
the
resultant array should be changed to the following (which is a good
thing to
do anyway):
For i = LBound(sarray1) To UBound(sarray1)
Also, you are passing a path and file (your ini file) to your
GetInfoSection
function as its strIniFile parameter and then you are failing to
actually
use that parameter in your function code. Instead, you are creating
the path
and file name again inside the function, which really doesn't make
sense and
will of course be a source of error if the two strings don't match. If
you
pass a function a parameter then you really should use that parameter
instead of doing what you are currently doing. Also, when creating a
path by
appending a filename to the string returned by App.Path you should
really
check whether or not it already contains a final backslash before
deciding
to add one yourself (as you are doing in your App.Path & "/xj.ini"
code.
This is because if your App's path happens to be a root directory
("c:\" for
example) then it will already end with a backslash and if you add
another
one then your path will be incorrect. Admittedly it is unlikely that
your
app's path is in a root directory, but it's worht checking for. By the
way,
you should really be using a backslash (\.xj.ini) instead of the
forward
slash (/xj.ini).
There are other things about your code that need mentioning, but first
I
think you should use Option Explicit and fix the problems it finds,
and
attend to the other things I have mentioned here, and then post again
if you
still have problems.
Mike
|
|
|
|
|