Creating generic list of instances of a class.

Posted by Jim Branson on Stack Overflow See other posts from Stack Overflow or by Jim Branson
Published on 2010-05-30T19:35:38Z Indexed on 2010/05/30 19:42 UTC
Read the original article Hit count: 576

Filed under:
|
|

I have several projects where I build a dictionary from a small class. I'm using C# 2008, Visual studio 2008 and .net 3.5 This is the code:

namespace ReportsTest
{
    class Junk
    {
        public static Dictionary<string, string> getPlatKeys()
        {
            Dictionary<string, string> retPlatKeys = new Dictionary<string, string>();
            SqlConnection conn = new SqlConnection("Data Source=JB55LTARL;Initial Catalog=HldiReports;Integrated Security=True");
            SqlDataReader Dr = null;
            conn.Open();
            SqlCommand cmnd = new SqlCommand("SELECT Make, Series, RedesignYear, SeriesName FROM CompPlatformKeys", conn);
            Dr = cmnd.ExecuteReader();
            while (Dr.Read())
            {
                utypPlatKeys rec = new utypPlatKeys(Dr);
                retPlatKeys.Add(rec.Make + rec.Series + rec.RedesignYear, rec.SeriesName);
            }
            conn = null;
            Dr = null;
            return retPlatKeys;
        }
    }
    public class utypPlatKeys
    {
        public string Make { get; set; }
        public string Series { get; set; }
        public string RedesignYear { get; set; }
        public string SeriesName { get; set; }
        public utypPlatKeys(SqlDataReader dr)
        {
            this.Make = dr.GetInt16(dr.GetOrdinal("Make")).ToString("D3");
            this.Series = dr.GetInt16(dr.GetOrdinal("Series")).ToString("D3");
            this.RedesignYear = dr.GetInt16(dr.GetOrdinal("RedesignYear")).ToString();
            this.SeriesName = dr["SeriesName"].ToString();
        }
    }
}

The immediate window shows all of the entries in retPlatKeys and if you hover over retPlatKeys after loading it indicates the number of elements like this: "retPlatKeys| Count = 923 which is correct. I went to create a new project using this pattern only now the immediate window says retPlatKeys is out of scope and hovering over retPlatKeys after loading I get something like retPlatKeys|0x0000000002578900. Any help is greatly appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about list