Dim v as variant=Kitchen.Sink
but only if the class Kitchen with a property of Sink has been previously defined.
Sink is a shared property of Kitchen, of course.
I’d say more like
dim v as variant = CauseLotsOfPainSufferingAndConfusion()
where the global method returns a random value each time you call it
Variants are evil in so many ways with all the implicit conversions
They cause no end of bugs because people forget or do not realize that they are relying on these implicit conversions like
listbox1.addrow rs.field("name")
and all manner of issues
We’ve been getting rid of their use in the IDE as much as humanly possible for exactly this reason
Anyone using VCP formats will know their symptoms VERY well as things would flip flip from 1 to “1” and back
How does this behavior interact with IS vs ISA ? Any gotchas there?
What issues would you expect or fear?
Sorry, I meant to say the difference between “=” and “isa”. No idea, other than “if v = nil” may work differently than “if v isa nil” ?
“=” wouldn’t be a substitute for IsA, but I think you meant “is”. In any event, I’ve tried a number of ways and can’t “fool” the framework with “=” or “Is” with Variants.
we do try to make it hard to blow your feet off
some folks still achieve it though
Variants are evil. Except in dictionaries, and even so, they can bite…
Variants are great storage containers.
Just my opinion.
As a container they work fine.
But they do a fair amount of magic in their implicit conversions from one type to another - say when you shove in a string then ask for an integer etc.
That’s usually where the trouble starts and where people get surprised or weird hard to find bugs.
Honestly I’d REALLY like something like the C++ templates so you could create a dictionary like
dim d as new Dictionary( String, Integer )
or
dim d as new Dictionary( String, MyClass )
and thereby have a dictionary where you can strongly type they keys & values
Of course there is still a need for a chameleon type (like variant) BUT perhaps one that doesn’t do all the implicit conversions that Variant does
Couldn’t this be handled with an If statement as I’m pretty sure a Variant can be Nil? Not at a computer with Xojo so I’m not sure
Dim v As Variant = Nil
If v = Nil then
Else
End If
Variant are not evil if used in the right way.
But this applies in general to everything.
I use Variants a lot and they works very well, except for some unwanted special features
I should rephrase.
The “variants are evil” comment is because of the implicit conversions variants do.
That’s whats evil as thats often the cause of bugs and mistakes because people don’t realize whats going on and why it can be problematic.
Like in your code that started this thread
[quote=129998:@Massimo Valle]Variant are not evil if used in the right way.
But this applies in general to everything.
I use Variants a lot and they works very well, except for some unwanted special features :P[/quote]
Everything is usable when done right. But non typed variables are at least dangerous.
Types and declarations protect the integrity of a program just like scope. Doing away from that, like it happens in Php, is inherently adding to the program potential sources of errors. Like in this which runs without error :
dim v as variant = "100"
v = v+1
msgbox v
Easy to spot on three lines, much thornier in hundreds of lines. And downright maligne when the variant is global…