1

I am currently setting some strings via this method:

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

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 = allmarketdata.@return.markets.GetType().GetProperty(currentMarketSelected);
dynamic market = property.GetMethod.Invoke(allmarketdata.@return.markets, null);
string marketlabel = market.label.ToString(); //Gets all my selected market data
juju
  • 13
  • 3
  • We can't help you without knowing what classes you're using. Those look totally unfamiliar. – Kendall Frey Jan 26 '14 at 00:54
  • "allmarketdata.@return.markets", will this compile? – Voice Jan 26 '14 at 00:57
  • 1
    @Voice http://stackoverflow.com/questions/429529/what-does-the-symbol-before-a-variable-name-mean-in-c – L.B Jan 26 '14 at 01: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. – juju Jan 26 '14 at 01:10

2 Answers2

1

Here is a solution using reflection.

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

var property = allmarketdata.@return.markets.GetType().GetProperty(currentMarketSelected);
dynamic market = property.GetGetMethod().Invoke(allmarketdata.@return.markets, null);
string marketlabel=market.label.ToString();
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
  • 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 allmarketdata.@return.markets – juju Jan 26 '14 at 01:41
  • I fixed the posted solution accordingly. – Wagner DosAnjos Jan 26 '14 at 01:44
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 marketlabel=allmarketdata.@return.markets[currentMarketSelected]label.ToString();

This is an indexed property.

Umar Farooq Khawaja
  • 3,925
  • 1
  • 31
  • 52