Finding out if array has been dimensioned??

J

JB

Hi Folks,

Is there any way I can find out if my dynamic array has been dimensioned?

I want to use this array to read from a file but under some conditions
the array might not be dimensioned yet.

Cheers

J
 
H

Helmut Weber

Hi JB,
hm..., I wonder,
with "option explicit" code shouldn't run at all, if a variable
wasn't declared (dim) . Without "option explicit", the variable is
dimensioned (declared) at runtime. What are the conditions, that
prevent the declaration? Or am I missing the point, and you want to
know, if the array, that didn't have an index, was redimensioned?
Then, have a look at this:
' option explicit
-----
Dim MyArray() As String
On Error Resume Next
MsgBox UBound(MyArray)
If Err.Number = 9 Then
MsgBox "no index" & Chr(13) & _
Err.Description
End If
 
J

JB

Helmut said:
Hi JB,
hm..., I wonder,
with "option explicit" code shouldn't run at all, if a variable
wasn't declared (dim) . Without "option explicit", the variable is
dimensioned (declared) at runtime. What are the conditions, that
prevent the declaration? Or am I missing the point, and you want to
know, if the array, that didn't have an index, was redimensioned?
Then, have a look at this:
' option explicit
-----
Dim MyArray() As String
On Error Resume Next
MsgBox UBound(MyArray)
If Err.Number = 9 Then
MsgBox "no index" & Chr(13) & _
Err.Description
End If

---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
Yeah Helmut..Sorry that's what I meant.

I thought there might have been a handy function like IsArray (I know
this is different) that could tell me if the array had an index?

It's the ReDim portion that I'm having trouble with as I'm trying to
write a text file with values from the doc custom styles and populate a
listbox with the custom style values.

Problem is when I come accross a doc that has no custom styles <g>

On Error GoTo CloseFile

count = UBound(aStyArrName()) + 1 'here if no index falls over!!

Open filename For Output As #filenumber

Print #filenumber, "[Setup]"
Print #filenumber, "TemplateName=" & frmStyles.txtTemplateName
Print #filenumber, "Count=" & count
Print #filenumber, ""

For i = 0 To count - 1
Print #filenumber, "[Style" & " " & i + 1 & "]"
Print #filenumber, "Name=" & aStyArrName(i)
Print #filenumber, "Size=" & aStyArrSize(i)
Print #filenumber, "FontType=" & aStyArrFont(i)
Print #filenumber, "StyleType=" & aStyArrStyleType(i)
Print #filenumber, ""
Next
CloseFile:
Close #filenumber

frmStyles.lstStyleNames.Clear
count = UBound(aStyArrName()) + 1
For i = 0 To count - 1
frmStyles.lstStyleNames.AddItem aStyArrName(i)
Next

etc.........

Any pointers?

J
 
H

Helmut Weber

Hi JB,
a real name would be much nicer, I think,
one day I might run into you and not know you :)
---
Public Function HasIndex(Arr As Variant) As Boolean
Dim l As Long
HasIndex = False
On Error GoTo ende
' "ende(german for end)" it's handy to know more than one language
l = UBound(Arr)
ende:
If Err.Number = 0 Then HasIndex = True
End Function
---
Sub Test09()
Dim MyArray() As String
' MyArray = Split("test,test,test", ",")
MsgBox HasIndex(MyArray)
End Sub
 
J

JB

Helmut said:
Hi JB,
a real name would be much nicer, I think,
one day I might run into you and not know you :)
---
Public Function HasIndex(Arr As Variant) As Boolean
Dim l As Long
HasIndex = False
On Error GoTo ende
' "ende(german for end)" it's handy to know more than one language
l = UBound(Arr)
ende:
If Err.Number = 0 Then HasIndex = True
End Function
---
Sub Test09()
Dim MyArray() As String
' MyArray = Split("test,test,test", ",")
MsgBox HasIndex(MyArray)
End Sub
---
Greetings from Bavaria, a european region
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
Hi Helmut,
Thanks for that and sorry for taking so long to reply (pressures of
modern living :) )

John
 
H

Howard Kaikow

You have to redim the array to the appropriate size before you start reading
the file.
But how do you know the number of elements in the file?
If you cannot redim in advance and you will have to do a redim preserves as
many times as is necessary.

--
http://www.standards.com/; See Howard Kaikow's web site.
JB said:
Helmut said:
Hi JB,
hm..., I wonder,
with "option explicit" code shouldn't run at all, if a variable
wasn't declared (dim) . Without "option explicit", the variable is
dimensioned (declared) at runtime. What are the conditions, that
prevent the declaration? Or am I missing the point, and you want to
know, if the array, that didn't have an index, was redimensioned?
Then, have a look at this:
' option explicit
-----
Dim MyArray() As String
On Error Resume Next
MsgBox UBound(MyArray)
If Err.Number = 9 Then
MsgBox "no index" & Chr(13) & _
Err.Description
End If

---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
Yeah Helmut..Sorry that's what I meant.

I thought there might have been a handy function like IsArray (I know
this is different) that could tell me if the array had an index?

It's the ReDim portion that I'm having trouble with as I'm trying to
write a text file with values from the doc custom styles and populate a
listbox with the custom style values.

Problem is when I come accross a doc that has no custom styles <g>

On Error GoTo CloseFile

count = UBound(aStyArrName()) + 1 'here if no index falls over!!

Open filename For Output As #filenumber

Print #filenumber, "[Setup]"
Print #filenumber, "TemplateName=" & frmStyles.txtTemplateName
Print #filenumber, "Count=" & count
Print #filenumber, ""

For i = 0 To count - 1
Print #filenumber, "[Style" & " " & i + 1 & "]"
Print #filenumber, "Name=" & aStyArrName(i)
Print #filenumber, "Size=" & aStyArrSize(i)
Print #filenumber, "FontType=" & aStyArrFont(i)
Print #filenumber, "StyleType=" & aStyArrStyleType(i)
Print #filenumber, ""
Next
CloseFile:
Close #filenumber

frmStyles.lstStyleNames.Clear
count = UBound(aStyArrName()) + 1
For i = 0 To count - 1
frmStyles.lstStyleNames.AddItem aStyArrName(i)
Next

etc.........

Any pointers?

J
 

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