Trying to print my LS array to the console. Please help

Hi there:

I have the following code in an agent which gets all department names from a view and places them into a dynamic array. The code seems to run well. At this point, I just want to verify that the department names are in fact in the array, so I’m trying to print the array to the console. I’m getting the right number of entries in the array, but I can’t seem to print the department names to the console. Here is my code:

Sub GetDepartments

Dim DeptView As NotesView

Dim DeptViewEntryColl As NotesViewEntryCollection

Dim DeptViewEntry As NotesViewEntry

Dim Depts() As Variant						'*** Array which holds the department names

Dim i As Integer

Dim n As Integer

Set DeptView = dbGlobal.GetView("EngDepartmentLookup")

Set DeptViewEntryColl = DeptView.AllEntries

Set DeptViewEntry = DeptViewEntryColl.GetFirstEntry

i = 0

While Not DeptViewEntry Is Nothing

	Redim Depts(i)

	Depts(i)=Cstr(DeptViewEntry.ColumnValues(0))

	i = i + 1

	Set DeptViewEntry = DeptViewEntryColl.GetNextEntry(DeptViewEntry)

Wend

n=0

While n < Ubound(Depts)

	Msgbox "Department name is " + Depts(n)

	n=n+1

Wend

Msgbox "Number of departments is " + Cstr(Ubound(Depts)+1)

End Sub

Subject: Trying to print my array to the console. Please help.

Gerald,

You are right, the total number of entries is correct, but the entries are not, because you are erasing the elements everytime you redim it.

Try to use Preserve between Redim and Depts(i).

ie

Redim Preserve Depts(i)

Regards,

Fadi Kiwan

Subject: RE: Trying to print my array to the console. Please help.

Of course!!!

Thanks, Fadi! I’m glad you’re awake today!

Gérald

Subject: Why not just do this?

You don’t say what ouput you’re getting, are you sure the column value is what you expect? Also probably the real reason, you are not preserving your array.

I’d try this:

Sub GetDepartments

Dim DeptView As NotesView

Dim DeptViewEntryColl As NotesViewEntryCollection

Dim DeptViewEntry As NotesViewEntry

Dim Depts() As Variant						'*** Array which holds the department names

Dim i As Integer

Dim n As Integer

Set DeptView = dbGlobal.GetView("EngDepartmentLookup")

Set DeptViewEntryColl = DeptView.AllEntries

Set DeptViewEntry = DeptViewEntryColl.GetFirstEntry

i = 0

While Not DeptViewEntry Is Nothing

	Redim Depts(i) preserve

	Depts(i)=Cstr(DeptViewEntry.ColumnValues(0))

	Msgbox "Cstr(DeptViewEntry.ColumnValues(0)) is " + Cstr(DeptViewEntry.ColumnValues(0))

	Msgbox "Department name in array is " + Depts(n)

	i = i + 1

	Set DeptViewEntry = DeptViewEntryColl.GetNextEntry(DeptViewEntry)

Wend

Msgbox "Number of departments is " + Cstr(Ubound(Depts)+1)

End Sub

Subject: RE: Why not just do this?

I would like to add to this that, in this case, it might be slightly faster at runtime to do this:

Dim DeptView As NotesView

Dim DeptViewEntryColl As NotesViewEntryCollection

Dim DeptViewEntry As NotesViewEntry

Dim i As Integer

Dim n As Integer

Set DeptView = dbGlobal.GetView(“EngDepartmentLookup”)

Set DeptViewEntryColl = DeptView.AllEntries

Set DeptViewEntry = DeptViewEntryColl.GetFirstEntry

ub = DeptViewEntryColl.count

i = 0

Dim Depts(ub - 1) As Variant

'*** Array which holds the department names. By getting the count, which by the code below, will be the correct size of the array, we save having to redim. Might be a bit faster. We subtract 1 because we are beginning with i = 0 and the count in the column will start with 1, so there will be an empty element at the end of the array if we do not compensate. Either method will work, but this way does avoid that multiple redim.

While Not DeptViewEntry Is Nothing

Depts(i)=Cstr(DeptViewEntry.ColumnValues(0))

Msgbox "Cstr(DeptViewEntry.ColumnValues(0)) is " + Cstr(DeptViewEntry.ColumnValues(0))

Msgbox "Department name in array is " + Depts(n)

i = i + 1

Set DeptViewEntry = DeptViewEntryColl.GetNextEntry(DeptViewEntry)

Wend

Msgbox "Number of departments is " + Cstr(Ubound(Depts)+1)

End Sub