Click here to Skip to main content
15,039,714 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have:

public class rowEntry
{
    public int idx;
    public string descr;
    public string param;

    rowEntry(DataRow dr)
    {
        int.TryParse(dr[0].ToString(), out idx);
        descr = dr[1].ToString();
        param = dr[2].ToString();
    }
}


and, as part of IEnumerable, want to return:

        public rowEntry Current
...
return (rowEntry)tbl.Rows[posn];
...


but I get cannot convert type DataRow to rowEntry

What I have tried:

I've tried searching for an answer but was unable to find anything that seemed close enough
Posted
Updated 10-May-17 9:57am
Comments
[no name] 10-May-17 15:54pm
   
You cannot cast Rows as its collection of rows, not a class you have. Instead you can pass the Row/Rows to any method and then build your object of class rowEntry

1 solution

So, this does at least compile:

public rowEntry Current
{
    get
    {
        try
        {
            rowEntry xre = new rowEntry(tbl.Rows[posn]);
            return xre;
        }
        catch (IndexOutOfRangeException)
        {
            throw new InvalidOperationException();
        }
    }
}


is it the best way?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900