Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following Class...

class gridRecord
{
    //Constructor
    public gridRecord()
    {
        Quantity = new quantityField();
        Title = new titleField();
        Pages = new pagesField();
    }
    private quantityField quantity;
    private titleField title;
    private pagesField pages;



    internal quantityField Quantity
    {
        get { return quantity; }
        set { quantity = value; }
    }

    internal titleField Title
    {
        get { return title; }
        set { title = value; }
    }

    internal pagesField Pages
    {
        get { return pages; }
        set { pages = value; }
    }
}

I want to be able to get the name of each field as a string so I can later create a datable with out having to specify each column.

List<gridRecord> lgr = new List<gridRecord>();
lgr = populatedList();

foreach(gridField gf in lgr[0])
    MessageBox.Show(gf.ToString());

But I get this error:

Error 1 foreach statement cannot operate on variables of type 'XML__Console.gridRecord' because 'XML__Console.gridRecord' does not contain a public definition for 'GetEnumerator'

I assume I need to inherit form and interface or something but not sure how or which one.

Grid Field Added...

class gridField : Validateit
{
    public gridField()
    {
        Value = "---";
        isValid = false;
        message = "";
    }
    private string value;
    protected bool isValid;
    private string message;

    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }
    public bool IsValid
    {
        get { return isValid; }
        set { isValid = value; }
    }
    public string Message
    {
        get { return message; }
        set { message = value; }
    }
    public override void Validate()
    {

    }
}

quantityField added below the other fields are much the same

class quantityField : gridField
    {

        public void validate()
        {            
            if (isQuantityValid(Value) == false) { Value = "Invalid";}           
        }

        public static bool isQuantityValid(string quantity)
        {

            if (quantity.Length > 3)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

From what i understood, you want to get the name of the properties from the gridRecord class (from your example: "Quantity", "Title", "Pages")?

For that, you need to use Reflection:

var properties = typeof(gridRecord).GetProperties();
foreach (PropertyInfo propInfo in properties) {
    MessageBox.Show(propInfo.Name);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...