RaiseEvent / Constructor

It’s probably not possible to raise an event within the Constructor method of an object, so during the creation of the object.
Since I am planning to do some things like establishing a connection to another program right away, I need to have an option to raise an event in case something would get wrong.
If you have a good efficient suggestion, I would be please to read it here.

But the normal controls raise the open event from the constructor.
So this should work.

Define an event, AfterOpen (or something).

Add a Timer property that is initialized in the Constructor with ModeSingle and a short period.

Have the Timer.Action raise your new event.

You are assured that your object has fully formed before you attempt to use it.

1 Like

They don’t actually. The Open event is raised after the Constructor returns.

For controls, but not for Windows.

See The order of events in Xojo

Yes, you can raise an event in the constructor of an object. Consider the most generic case:

Super: <none>

Event opening()
Sub ()

Sub Constructor()
  raiseevent opening
End Sub


Super: Class1

Sub opening()
End Sub


dim c as new Class2

You will reach the breakpoint in Class2.

If you want different code in the event, you will have to create separate subclasses for each.

Forgot to say: it don’t have controls in this project.

Thanks Cristian, good to have this list in mind, but it does not give me the clue yet, so I have to be creative as Kem suggested.

I was thinking this way already, but was hoping for a less cumbersome method. Since I want to have it implemented within the one class (for library) I am creating, I think I have to go this way. Not a big deal, but as said, feels a bit clumsy to escape to the good old everything fixer like a timer.

Don’t use an additional class for the timer, but a property of your class (e.g. MyTimer as Timer). Set it up early in your class and use a delegate so the timer triggers one method in your class.

Yes I know how to do this @Arnaud_N .
What I ment to say is that I don’t want an additional class/object as a replacement of the timer solution which can be implemented within my class of subject.

I understood you didn’t wanted an additional class as in “another item in the project window”, like for distributing all in a single item; that’s why I pointed out you can have all in one single class.
Of course, if you also don’t want an extra object as a property of your class (why, by the way?), this reduces your possibilities.

Have you considered Timer.calllater? I often use Timer.calllater(0, <MethodName>) to push the action into the next event loop run.


you could do something like

var a as Class1 = Class1.Connect()

Shared Method Connect
var o as new Class1
Return o

its not direct a constructor but easy to write.

as i remember a event in xojo is just a method call.