Tuesday, November 26, 2013

expandoObject: property get

I guess this code is what you're looking for:


Here's my solution, again (a little simpler):



namespace Qwerty
{
public class Asdfg
{
private const int maximumAllowedNumberOfElementsBecauseIHaveOtherThingsToDoOlRightInterrogationMark = 3;
private dynamic xp = new ExpandoObject();
public dynamic Xp { get { return xp; } set { } }

public Asdfg(string[] _arrayOfPropNames, double[] _arrayOfDoubles)
{
System.Diagnostics.Debug.Assert(_arrayOfPropNames.Count() > 0);
System.Diagnostics.Debug.Assert(_arrayOfPropNames.Count() <= maximumAllowedNumberOfElementsBecauseIHaveOtherThingsToDoOlRightInterrogationMark);
System.Diagnostics.Debug.Assert(_arrayOfPropNames.Count() == _arrayOfDoubles.Count());

string s0 = _arrayOfPropNames[0];
xp.s0 = _arrayOfDoubles[0];
string s1 = _arrayOfPropNames[1];
xp.s1 = _arrayOfDoubles[1];
string s2 = _arrayOfPropNames[2];
xp.s2 = _arrayOfDoubles[2];
//
// Clearly, you must adapt the code above if you have less elements than 'maximumAllowedNumberOf...'
//
}
}
}

namespace Fórum_ExpandoObject_property_get
{
class Program
{
static void Main(string[] args)
{
string[] arrayOfPropNames = { "SiO2", "Al2O3", "Fe2O3", };
double[] arrayOfDoubles1 = { -123, 0, 678, };
double[] arrayOfDoubles2 = { -1234, 0.1, 6789, };
double[] arrayOfDoubles3 = { -12345, 0.2, 67890, };

//
// ---
//

List<dynamic> manyAsdfgs = new List<dynamic>
{
new Qwerty.Asdfg(arrayOfPropNames, arrayOfDoubles1).Xp
, new Qwerty.Asdfg(arrayOfPropNames, arrayOfDoubles2).Xp
, new Qwerty.Asdfg(arrayOfPropNames, arrayOfDoubles3).Xp
, /* ridiculously simple */
};

//
// ---
//

Console.WriteLine("\nFor arrayOfPropNames index 0");
string s0 = arrayOfPropNames[0];
manyAsdfgs.Select(x => s0 + " = " + x.s0).ToList().ForEach(Console.WriteLine);

Console.WriteLine("\nFor arrayOfPropNames index 1");
string s1 = arrayOfPropNames[1];
manyAsdfgs.Select(x => s1 + " = " + x.s1).ToList().ForEach(Console.WriteLine);

Console.WriteLine("\nFor arrayOfPropNames index 2");
string s2 = arrayOfPropNames[2];
manyAsdfgs.Select(x => s2 + " = " + x.s2).ToList().ForEach(Console.WriteLine);

Console.WriteLine("\n\nRidiculously simple.");
Console.ReadKey();
}
}
}



Did I say the problem is ridiculously simple?


If not, please consider it said (right ladyJ? I know you're reading this, and I'm still waiting for your demo of C# expertise - :S).





... don't disturb ... life is short, and I have to think what I'll do next ...


No comments:

Post a Comment