-
February 23rd, 2010, 12:09 PM
#1
Looping through an Array of UDTs - Object Variable Not Set Error
I am seeking to loop through an array of UDTs at some other point in my code from where the UDT is initialized. I am trying to split my code along functional lines and need some UDT methods to be executed at different points within the code - for example, I would like the UDT methods to calculate some values when a "calculate values" subroutine is run, then print them when I call a print subroutine. I have included the following generic snippets of what I am attempting to accomplish:
this works:
For Each rw In Range(namedRange).Rows
i = i + 1
Set ArrayOfUDTs(i) = New UDT
Application.StatusBar = "Calling Initialization..."
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2)
Application.StatusBar = "Calling UDT Method..."
Call ArrayOfUDTs(i).UDTMethod(Argument1)
Next rw
This does not:
For Each rw In Range(namedRange).Rows
i = i + 1
Set ArrayOfUDTs(i) = New UDT
Application.StatusBar = "Calling Initialization..."
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2)
Next rw
For i = LBound(ArrayOfUDTs) To UBound(ArrayOfUDTs)
Application.StatusBar = "Calling UDT Method..."
Call ArrayOfUDTs(i).UDTMethod(Argument1)
Next i
The line:
"Call ArrayOfUDTs(i).UDTMethod(Argument1)"
generates a "Object variable or With variable block not set" error
Oddly enough, if I add each of my Array elements into a collection:
Collection.add ArrayOfUDTs(1)
then loop through each of the UDTs using the collection's functions
using the following command:
Call Collection.CollectionSubroutine1(Argument1)
where:
Public sub CollectionSubroutine1(Argument1)
For i = t to UDT.count
UDT.item(i).UDTMethod(Argument1)
next i
end sub
So this works:
For Each rw In Range(namedRange).Rows
i = i + 1
Set ArrayOfUDTs(i) = New UDT
Application.StatusBar = "Calling Initialization..."
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2)
Application.StatusBar = "Calling UDT Method..."
Collection.add ArrayOfUDTs(1)
Next rw
Call Collection.CollectionSubroutine1(Argument1)
this does exactly what I was looking to do initially: loop through the contents of an array of UDTs (of unknown size - the size of the array is established at run time) and execute a UDT method - in other sections of my code - not necessarily when I am initially creating the array of UDTs
Can anyone shed some light as to why I am getting the "Variable not set" error when I attempt to loop through the ArrayOfUDTs in a separate loop?
-
February 24th, 2010, 09:58 AM
#2
Re: Looping through an Array of UDTs - Object Variable Not Set Error
Is this VBA?
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2)
is no valid VB6 syntax and doesn't look valid for VBA either.
Also you seem to be talking about objects you have called UDT, because a real VB UDT (User Defined Type) does not support methods and is not initialized with the New keyword.
"Object not set" will indicate an object variable which is set to Nothing, when being not initialized yet.
Code:
If Not ArrayOfUDTs(i) Is Nothing then Call ArrayOfUDTs(i).UDTMethod(Argument1) Else debug.print "Element " & i & " is Nothing"
For debugging you can insert an if statement to test the array element before using it:
-
March 2nd, 2010, 05:40 PM
#3
Re: Looping through an Array of UDTs - Object Variable Not Set Error
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2) <---------?
If this is a class module, then create an object first and set this class to it, Example:
Code:
Dim xxx as ClassModule1 '--- much better if you name it according to its purpose
then before using, have this statement
Code:
Set xxx = New ClassModule1
then, finally...
Code:
xxx.initializationMethod Argument1, ArrayOfUDTS(i)
In addition, this will not work:
Code:
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument2)
.. because you are calling the object method while at the same time, defning the variable type of the last argument. You should define the variable type inside the class module, at a time when youre defining the methods or properties -- not during the call.
Marketing our skills - please participate in the survey and share your insights
-
-
March 3rd, 2010, 01:17 AM
#4
Re: Looping through an Array of UDTs - Object Variable Not Set Error
Originally Posted by robV
this works:
Code:
For Each rw In Range(namedRange).Rows
i = i + 1
Set ArrayOfUDTs(i) = New UDT
Application.StatusBar = "Calling Initialization..."
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2)
Application.StatusBar = "Calling UDT Method..."
Call ArrayOfUDTs(i).UDTMethod(Argument1)
Next rw
This does not:
Code:
For Each rw In Range(namedRange).Rows
i = i + 1
Set ArrayOfUDTs(i) = New UDT
Application.StatusBar = "Calling Initialization..."
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2)
Next rw
For i = LBound(ArrayOfUDTs) To UBound(ArrayOfUDTs)
Application.StatusBar = "Calling UDT Method..."
Call ArrayOfUDTs(i).UDTMethod(Argument1)
Next i
I think that you make a mistake with "i" variable in first loop. VB/VBA arrays begin from 0 if another base isn't specified.
So you can use "i = LBound(ArrayOfUDTs)-1" before ForEach or "For i = LBound(ArrayOfUDTs)+1 To UBound(ArrayOfUDTs)"
With best wishes,
Vita
-----------------------
Russian Software Development Network -- http://www.rsdn.ru
-
March 3rd, 2010, 08:42 AM
#5
Re: Looping through an Array of UDTs - Object Variable Not Set Error
I would be really interested in what kind of syntax is this:
Call ClassModule1.initializationMethod (Argument1, ArrayOfUDTS(i) as Argument 2) ???
You cannot use a declaration like statement in an argument list of a procedure call.
Even if you make that Argument2 (one word).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|