Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning an object from a function?

Status
Not open for further replies.

davidd31415

Programmer
Jan 25, 2006
154
US
I'm trying to return a user-defined class object from a different user-defined class function and am getting an "object variable or with block variable not set" error after the object initializes.

I've tried using both Property Get and Functions, both in this form:


Public Function GetDateTime() As DataPointClass
GetDateTime = New DataPointClass
GetDateTime = DateTime
End Function

(or Public Property Get GetDateTime() As DataPointClass)

Where DateTime is a local DataPointClass Object. If I leave out the GetDateTime = New DataPointClass line, the same error is raised. When stepping through, the DataPointClass Initialize() method is executed and the error is raised when that function ends.

Any ideas how I can do this? I believe in C++ I'd need to use pointers, this is the first time I've ever tried something like this with VBA.

Thanks,

Dave
 
I just realized I left out "Set"...

Trying
Set GetDateTime = New DataPointClass
or
Set GetDateTime = DateTime

stops the previous error from coming up but I am now getting an "Object doesn't support this property or method" error when the GetDateTime function/property ends.

 



Hi,

Does your new DataPointClass has properties and methods beyond a simple DateTime value?

What is the purpose of this new class?

Skip,

[glasses] [red][/red]
[tongue]
 
Yes, there are other properties and methods. I'm new to object programming so this may sound like I'm shooting myself in the foot but here's my problem/solution:

Data is acquired through a test system (DAQ). A single file is created for each test. Each file contains 92 data points. Data points such as date/time, voltage, test number consist of one field only (value). Other data points, such as UpDnAngularSpeed and LeftRightRunningCurrent consist of 4 data points (Value, PassOrFail, UpperLimit, LowerLimit).

The VBA program I am writing tabulates the data in a user-customizeable report. A Position field is stored with each data point to keep track of what order the user would like to have the data reported in (adjustable through a userForm). This calls for 2 required fields and 3 optional fields for each data point.

Three classes exist:

DataPointClass
Each object contains the data associated with a single DataPoint, such as TimeDate or UpDnAngularSpeed. This allows me to work with the data using the field name (i.e. TimeDate).

DataPointGroupClass
Each object contains the data associated with a single file. This object creates all the DataPointClass objects.

DataClass
One object only. This object creates and manages all the DataPointGroupClass objects.

DataPointGroupClass creates DataPointClass objects (using Property Let). All the fields are passed to the DataPointClass object using a single Property Let method that looks like this:

Public Property Let SetUDAngularSpeed(aVal As String, aPos As Integer, aPassFail As String, aUSL As String, aLSL As String)
Call UDAngularSpeed.AddPoint(aPos, aVal, aPassFail, aUSL, aLSL)
End Property

Each DataPointClass object holds only 1 datapoint of data (I'm thinking there may be a better way to do this; I'm new to object-oriented programming and code my way in circles at times yet). "AddPoint" is misleading in this sense (there is functionality to add more points, I didn't implement it this way though).

My problem is arising when I try to get a datapoint from the DataPointClass object. I could add in 5 propert get statements for each datapoint handled in DataPointGroupClass but I thought it would be nicer to return a DataPointClass object, which would allow me to work with only the property get statements of the DataPointClass object.

Hope that made sense.

In shorter words, the purpose of the class is to group information associated with a single datapoint together.
 
I think I found a solution in using user-defined types to return the data instead of objects. I'm thinking it is going to make sense to replace that whole class with the user-defined types instead. I'd still like to know why I am unable to return an object from a propety get or function in this case.

Thanks,

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top