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;
}
}
}
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:
If you want to get name of each field you should use reflection.
PropertyInfo class should be helpful
You are attempting to iterate over one element of the collection.
If your goal is to retrieve the name of each property within the class, then do this.