Reputation: 2068
i have struct like this :
public struct GoldAverages
{
public decimal Sell_GoldOunce;
public decimal Buy_GoldOunce;
public decimal Sell_SilverOunce;
public decimal Buy_SilverOunce;
public int Sell_Mazene;
public int Buy_Mazene;
public int Sell_Gram_18;
public int Buy_Gram_18;
public int Sell_Gram_24;
public int Buy_Gram_24;
}
How can i read my struct items one by one (by name of item and value) and put them into Lable.text
like this
GoldAverages Gold = new GoldAverages();
foreach (var item in Gold)
{
LblSummery.Text += item.name + " : " + item.value + "|";
}
Upvotes: 0
Views: 470
Reputation: 2068
GoldAverage Gold = new GoldAverage();
StringBuilder sb = new StringBuilder();
foreach (var field in typeof(GoldAverage).GetFields())
{
sb.append(field.Name.ToString() + " : " + field.GetValue(Gold).ToString() + " | ");
Upvotes: -1
Reputation: 16723
Can you try this (you'll need to add the System.Reflection
namespace)
GoldAverages Gold = new GoldAverages();
Type gType = Gold.GetType();
IList<PropertyInfo> properties = new List<PropertyInfo>(gType.GetProperties());
StringBuilder sb = new StringBuilder();
foreach (PropertyInfo property in properties)
{
string propertyName = property.Name;
string propertyValue = property.GetValue(Gold, null);
sb.Append(propertyName + " : " + propertyValue + " | ");
}
LblSummery.Text = sb.ToString();
And now that I look at your code again, you will need to update your fields to be properties, which I believe is what you are after. Let me know otherwise.
Upvotes: 3