E
Epson Barnett
Hi,
I'm new to C# and I'd like to be able to reference a field of an
object using a variable instead of literal text.
In the PHP scripting language, you can create a variable:
$var = "name";
and another variable:
$name = "Epson";
Then you can refer to $name, or $$var. They will equal the same thing.
Is there similar functionality in C#?
The reason:
I have a loop inside a function which accepts two parameters and tests
for equality - it tests if an objects property with the name of the
first parameter is equal to the second parameter. This is so I don't
have to hard code all possible properties in advance, or update it as
new properties are added.
string myprop = "Branch"; <-- I'd like to use this instead of the
literal text below:
foreach (string key in keys)
{
Product prod = (Product)collection[key];
if (prod.Branch != val)
{
collection.Remove(key);
}
}
Thanks,
Epson
I'm new to C# and I'd like to be able to reference a field of an
object using a variable instead of literal text.
In the PHP scripting language, you can create a variable:
$var = "name";
and another variable:
$name = "Epson";
Then you can refer to $name, or $$var. They will equal the same thing.
Is there similar functionality in C#?
The reason:
I have a loop inside a function which accepts two parameters and tests
for equality - it tests if an objects property with the name of the
first parameter is equal to the second parameter. This is so I don't
have to hard code all possible properties in advance, or update it as
new properties are added.
string myprop = "Branch"; <-- I'd like to use this instead of the
literal text below:
foreach (string key in keys)
{
Product prod = (Product)collection[key];
if (prod.Branch != val)
{
collection.Remove(key);
}
}
Thanks,
Epson