Eric
Eric

Reputation: 19873

how to use variables inside a delegate function

I'm using a SDK for a usb camera. The SDK specifies that for each frame grabbed, a callback function will be called. The callback function is defined inside the SDK, it gets a data pointer to the image and a structure used to interpret the data.

All of that works correctly.

To make a useful application out of that, I need to access a few variables from my application. Now because the delegate function is static, I can only access static members. I thought of making a singleton out of them because its gonna be static, but is there any "conventionnal way" of accessing other data inside a delegate function?

Upvotes: 0

Views: 305

Answers (3)

Tetsujin no Oni
Tetsujin no Oni

Reputation: 7375

The delegate can be static (though why it would need to be, I'm not clear). The function does not need to be.

Dereferencing a delegate into a method call just doesn't care whether the function called is an instance method or a static method.

Upvotes: 0

Rytmis
Rytmis

Reputation: 32057

Why did you make the delegate a static? If it hurts when you do it, stop doing it. :)

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564551

Why not just use a non-static delegate? You'd have access to the class instance members in that case. Is there something forcing you into the static delegate instead of a delegate on an instance of one of the class where your data and logic resides?

If so, then is there a way to pass data to the callback? If you can, you could pass a reference to a class, and use that in your delegate to get your application data.

If not, then you may be forced to have some static data, or a static reference to a class holding your data. A singleton or similar construct may be the best option in this case...

Upvotes: 1

Related Questions