Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

c# - GetEnumerator Error with Custom Type

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

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

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...