Passing Int8 ByRef through a Variant — is it possible?

I have a method, let’s call it Foo(bar as Variant).

Up to now I’ve only been passing objects (controls) as bar. These objects are of course always passed by reference.

Now I need to pass an Int8 to this method, but I still need this integer to be passed ByRef. I’m a bit out of it right now: is that even possible, and if so, how would I do it?

no

objects ARE reference types
they are not pass by reference

there is a difference and I can illustrate if you want

Arrays are also reference types

Variants are VALUE objects
To change one you must use BYREF

To answer this threads question

Passing Int8 ByRef through a Variant — is it possible?
no
you MUST use BYREF

But, how do I use ByRef through a Variant?

You dont use it THROUGH a variant

Simply write your methods as

Foo(byref bar as Variant)

then assign to bar in the method

Reference types & value types are important to understand
Then BYREF makes more sense

IF you dont want to do that the only other way I can think of it to make your own “Variant” class and then its a reference type which would probably let you do what you intend

And that class could likely be made to support easy conversions just like variants do so your code doesnt need to change a lot

but before I say this WILL 100% work I’d need to know the use case as there MAY be other alternatives or gotchas I dont know

Ahh, I see where my missunderstanding was.

When having this method:
foo(bar as Variant)

this Code is valid:

Dim myVar as Integer = 1

But with:
foo(ByRef bar as Variant)
it is not.

Then I have to do:

myVariantVar as Variant = myVar

before calling foo.

Yes
BYREF requires an allocated variable since you COULD in Foo do something like

bar = “this”

which replaces bar with a new variant