I want to set a variable in a class. But I want to dynamically choose which variable to set.
In PHP :
<?PHP
class Foo {
var $Bar="Blah";
}
$ClassVar = new Foo();
$Variable = "Bar";
$ClassVar->$Variable = "Poo";
echo "Output : ".$ClassVar->Bar;
?>
Output : Poo
I want to do the same in VB.NET
In VB you’d use a dictionary to achieve this.
Although this is more reminiscent of the PHP
array. It is actually possible to get the same effect in VB as in your PHP code, using reflection. But VB is a different language and in VB you’d normally not write such a code. Even more strongly: when you find yourself writing such code in VB, it’s almost always a sign of a serious flaw in your code design.But just for completeness’ sake:
However, this only works when
Baris in fact a property, not a variable (it also works with a variable but that’s more difficult still).