Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use variable as key to a reponse object?

Status
Not open for further replies.

Shanondink

Technical User
Sep 9, 2002
29
US
Hi guys,

How can I use myvalue where I currently have ???????.

Code:
string myvalue = "minFinalRent";

foreach (FloorPlanUnitType fput in response.floorPlanUnitType)
{
 Console.WriteLine("Min Final Rent: {0|C}", fput.minFinalRent);
 Console.WriteLine("Max Final Rent: {0|C}", fput.maxFinalRent);

 // Question here
 Console.WriteLine("{0} = {1}", myvalue, fput.????????);

}

What I want is to be able to output either maxFinalRent or minFinalRent depending what is in the tmp string.

I can't seem to find anything googling "string as index", "variable as index", etc:(

response is the response to a SOAP call.

Thanks in advance,
Shanon
 
Code:
var temp = GetValueFromSomewhere();
var  finalRent = fput.minFinalRent;
if(temp == "something else")
{
   finalRent = fput.maxFinalRent;
}
Console.WriteLine("{0} = {1}", myvalue, finalRent);
If I understand your comment about string/variable as index, this is only possible with an IDictionary implementation where the index is stored as an object.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks Jason,

I thought about the if statements (or a switch) but being a lazy programmer and not wanting to type all those conditions, there has to be a more elegant way.

myvalue is going to come from a commandline argument.

In PHP, I can do *something* like this:

Code:
$myvalue = "minFinalRent";
echo "$myvalue = " . fput->$myvalue ;
And it would be the equivalent of:

Code:
$myvalue = "minFinalRent";
echo "$myvalue = " . fput->minFinalrent ;

So really I'm trying figure out how to do this in C#.

Again thanks for the help,
Shanon
 
php is a dynamic language. c# is compiled. why you're looking for is not possible with c#.

you can use Func<>, and Action<> to create expressions for delayed execution, but it's still more code than php.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks Jason,

I guess I'll hand code them:(

Have a good weekend.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top