1

I am currently setting some strings via this method:

string marketlabel = [email protected]();

I would like to set the market label dynamically by having a string for the actual market choice.

string currentMarketSelected= this.marketTextBox.Text; // Specific market: COLXPM

string marketlabel=allmarketdata.@return.markets.currentMarketSelected.label.ToString();

I have been searching for a few hours and probably am not explaining correctly. I tried some stuff with reflections with no success. Basically what I want to do is have a textbox or list which contains all the market names and based on which one is selected start setting the data.

Above is the best type of example of what I want to do even though it is not syntactically possible to use a variable in place.

public class Markets
{
    public COLXPM COLXPM { get; set; }
    //Lots of markets below here
}

public class COLXPM
{
    public string marketid { get; set; }
    public string label { get; set; }
    public string lasttradeprice { get; set; }
    public string volume { get; set; }
    public string lasttradetime { get; set; }
    public string primaryname { get; set; }
    public string primarycode { get; set; }
    public string secondaryname { get; set; }
    public string secondarycode { get; set; }
    public List<Recenttrade> recenttrades { get; set; }
    public List<Sellorder> sellorders { get; set; }
    public List<Buyorder> buyorders { get; set; }
}
public class Return
{
    public Markets markets { get; set; }
}

public class RootObject
{
    public int success { get; set; }
    public Return @return { get; set; }
}

The proposed solution below that worked

string currentMarketSelected = "DOGEBTC"; // Just selecting one of the markets to test it works
var property = [email protected]().GetProperty(currentMarketSelected);
dynamic market = property.GetMethod.Invoke([email protected], null);
string marketlabel = market.label.ToString(); //Gets all my selected market data
4
  • We can't help you without knowing what classes you're using. Those look totally unfamiliar. Commented Jan 26, 2014 at 0:54
  • "[email protected]", will this compile? Commented Jan 26, 2014 at 0:57
  • 1
    @Voice stackoverflow.com/questions/429529/… Commented Jan 26, 2014 at 1:00
  • All I want to do is replace one of the class names with a string variable. The Class name I want my code to use will change from time to time based on which UI element is selected. Commented Jan 26, 2014 at 1:10

2 Answers 2

1

Here is a solution using reflection.

string currentMarketSelected= this.marketTextBox.Text; // Specific market: COLXPM

var property = [email protected]().GetProperty(currentMarketSelected);
dynamic market = property.GetGetMethod().Invoke([email protected], null);
string marketlabel=market.label.ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

Just to test that it works I tried one of the other markets in the structure of data and it worked great. I had to move it over one from allmarketdata.@return to [email protected]
I fixed the posted solution accordingly.
0

You need something like this:

public class Markets
{
    public COLXPM this[string key]
    {
        get
        {
            COLXPM colxpm;

            switch (key)
            {
                // TODO : use "key" to select instance of COLXPM;
                case "example1":
                    colxpm = ...;
                    break;

                default:
                    throw new NotSupportedException();
            }

            return colxpm;
        }
    }
}

Then you can do something like:

string [email protected][currentMarketSelected]label.ToString();

This is an indexed property.

3 Comments

I will give this a go and reply back as soon as I can test it out.
I am sure this method works, only reason I avoided it was I would have to update about 92 definitions to include alot of the logic you added above.
I am glad you found a solution that works. Best of luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.