0

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 + "|";
        }
Amin AmiriDarban
  • 2,031
  • 4
  • 24
  • 32
  • 2
    why not use an array of key-value pairs or a dictionary instead? It looks like you are trying to solve the wrong problem – BrokenGlass Sep 22 '13 at 01:22
  • I want to now how can we access to struct childs one by one – Amin AmiriDarban Sep 22 '13 at 01:26
  • Or perhaps read this [When to use struct in C#?](http://stackoverflow.com/questions/521298/when-to-use-struct-in-c?rq=1) – Prix Sep 22 '13 at 01:26
  • here i have few childs in my struct...but in other case my struct has 75 childs – Amin AmiriDarban Sep 22 '13 at 01:26
  • 1
    "but in other case my struct has 75 childs" - Sounds like that is your problem there. From your description, it sounds like you are thinking of a struct with many fields as being the same as a collection of many objects. – mbeckish Sep 22 '13 at 01:57

2 Answers2

3

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.

Mister Epic
  • 16,295
  • 13
  • 76
  • 147
  • 1
    Or you could use `Type.GetFields`, no? And please use a `StringBuilder`! – Jim Mischel Sep 22 '13 at 01:36
  • Updated with StringBuilder. `GetFields` is an option, but properties are more typical when data is being accessed by other objects. I suggest the OP update those fields to properties. – Mister Epic Sep 22 '13 at 01:40
  • GoldAverage Gold = new GoldAverage(); StringBuilder sb = new StringBuilder(); foreach (var field in typeof(Functions.GoldAverage).GetFields()) { sb.append(field.Name.ToString() + " : " + field.GetValue(Gold).ToString() + " | "); – Amin AmiriDarban Sep 22 '13 at 02:56
-1
GoldAverage Gold = new GoldAverage();
    StringBuilder sb = new StringBuilder();
    foreach (var field in typeof(GoldAverage).GetFields())
    {
        sb.append(field.Name.ToString() + " : " + field.GetValue(Gold).ToString() + " | ");
Amin AmiriDarban
  • 2,031
  • 4
  • 24
  • 32