I'm learning C# and it's not clear for me how to access a property in a foreach
loop of FieldsList
class. It was not accessible. I'm try to get a string concatenation with name of FieldData
.
What's wrong?
namespace MyNamesp
{
public class FldData
{
public string Name { get; set; }
public Type FldType { get; set; }
public FldData() { }
public FldData(string name, Type fldType)
{
Name = name;
FldType = fldType;
}
}
class FieldsList<FldData> : List<FldData>
{
public int NumField { get { return this.Count; } }
public string QryFieldList()
{
string _QryFieldList = "";
foreach(FldData fld in this)
{
_QryFieldList += fld.Name +","; //Fld.Name is not accessible
}
return _QryFieldList;
}
}
}
The definition of the FieldsList
class shouldn't have a generic type argument,
just declare it as here below.
When defined as FieldsList<FldData>
, the FldData
part is considered as the generic type argument which is not the FldData
class, although both names are similar.
class FieldsList : List<FldData>
{
public int NumField { get { return this.Count; } }
public string QryFieldList()
{
string _QryFieldList = "";
foreach (FldData fld in this)
{
_QryFieldList += fld.Name + ",";
}
return _QryFieldList;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.