Alan
Alan

Reputation: 822

object required error on function call

I made a Class Module and named it Time. I wanted to have a function in it that created an instance of the Time object and returned it. See code below. If you can think of a better way to write the function, I'm all ears. The problem I'm having, as it stands is when I make the following statement:

tsheet.MondayStart = Time.Construct(Item.Value)

A debug.print statement prior to this call shows that Item.Value is "08:30". tsheet is of type TimeSheet and MondayStart property is expecting to be assigned an object of type Time. However, when this statement executes at runtime, I get the object required error. I even tried removing the paranthesis, but all that does is bring up another error "Compile Error: Expected end of statement"

How do I fix this? Please advise. Too bad vba doesn't support the notion of construtors :-(

Alan

'IN TIME CLASS MODULE
Public Function Construct(Value As String) As Time
  'This function expects a string formatted like: 08:30
  'Time.Delimiter returns ":"
  Dim tempTime As Time
  Dim vhours As Integer
  Dim vminutes As Integer
  Dim arrTime() As Time

  arrTime = Split(Value, Time.Delimiter)
  hours = CInt(Trim(arrTime(0)))
  minutes = CInt(Trim(arrTime(1)))
  Set tempTime = New Time
  tempTime.hours = vhours
  tempTime.minutes = vminutes

  Construct = tempTime
End Function

Upvotes: 1

Views: 1507

Answers (2)

Erik Eidt
Erik Eidt

Reputation: 26646

Use the result of the new as follows:

Set MonStart = New TimeFrame
Set tsheet.MondayStart = MonStart.Initialize(MonStart, Item.Value)

(For comparision, here's previous:

Set MonStart = New TimeFrame
Set tsheet.MondayStart = TimeFrame.Initialize(MonStart, Item.Value)

)

Also use Set on function return value assignment.

for the first code example:

...
    Set Construct = tempTime
End Function

and for the other code example:

...
    Set Initialize = Value
End Function

Upvotes: 0

darbid
darbid

Reputation: 2721

Actually I suggest you use the already implemented VBA methods DateValue and TimeValue which will accept a string and give you what you need.

I am not sure you need to re-invent the wheel here. Of course I might have missed something so please let me know.

Upvotes: 1

Related Questions