0

I have a program that takes a spreadsheet as input and, if there are any errors, dumps the errors back out onto a separate spreadsheet with the original data as input by the user and the error message.

To hold the errored records and their error messages I have a Dictionary object that may contain one of two types of objects as a key. It will either have a DataRow object or an Entity Framework model object by the name of ZipCodeTerritory.

Since I might have data input errors (ie 111/2/1980 as a date) I needed to create a separate error class that I use to populate the spreadsheet, and I need to convert the DataRow/ZipCodeTerritory keys into this special error class. However.. before I do that I need to determine which type I am working with. The method I'm using below has not worked, either with a DataRow or a ZipCodeTerritory object. Anyone know a better way to determine the type of a generic object?

C#

//Definition of the Dictionary I'm using to hold the errored records/messages private static Dictionary<object, string> _errors = new Dictionary<object, string>(); //Snippet of code to show how I'm adding one of the ZipCodeTerritory objects //"record" object is of type ZipCodeTerritory if (string.IsNullOrEmpty(record.LastUpdateId)) { //Add to error list _issues++; _errors.Add(record, "Missing last update Id"); continue; } //Section of code that tries to separate key/value and determine type foreach (KeyValuePair<object, string> item in errors) { //Use custom class in case of data errors //resulting from bad input on spreadsheet ZipCodeError zip; //Determine type and separate key and value Type type = item.Key.GetType(); if (type.GetType().Name.Equals("DataRow")) { zip = new ZipCodeError((DataRow)item.Key); } else if (type.GetType().Name.Equals("ZipCodeTerritory")) { zip = new ZipCodeError((ZipCodeTerritory)item.Key); } else { //Code always falls through to this.... zip = new ZipCodeError(); } 

ZipCodeError

public class ZipCodeError { //Create generic object properties just in case of //typos or any other unforseen input errors from the spreadsheet public object ChannelCode { get; set; } public object DrmTerrDesc { get; set; } public object IndDistrnId { get; set; } public object StateCode { get; set; } public object ZipCode { get; set; } public object EndDate { get; set; } public object EffectiveDate { get; set; } public object LastUpdateId { get; set; } public object LastUpdateDate { get; set; } public object ErrorCodes { get; set; } public object Status { get; set; } public object Id { get; set; } public ZipCodeError(){} public ZipCodeError(DataRow row) { this.ChannelCode = row[0]; this.DrmTerrDesc = row[1]; this.IndDistrnId = row[2]; this.StateCode = row[3]; this.ZipCode = row[4]; this.EndDate = row[5]; this.EffectiveDate = row[6]; this.LastUpdateId = row[7]; this.ErrorCodes = row[8]; this.Status = row[9]; this.Id = row[10]; } public ZipCodeError(ZipCodeTerritory master) { this.ChannelCode = master.ChannelCode; this.DrmTerrDesc = master.DrmTerrDesc; this.IndDistrnId = master.IndDistrnId; this.StateCode = master.StateCode; this.ZipCode = master.ZipCode; this.EndDate = master.EndDate; this.EffectiveDate = master.EffectiveDate; this.LastUpdateDate = master.LastUpdateDate; this.LastUpdateId = master.LastUpdateId; this.ErrorCodes = master.ErrorCodes; this.Status = master.Status; this.Id = master.Id; } } 
2
  • What does type.GetType().Name returns then? Commented Feb 7, 2014 at 21:26
  • 2
    Why not use a dictionary of <Type, object>? There are quite a few other issues with what you're doing, but that one really stands out. Really, though, if you have two types of data, I can't see any good reason to keep them in the same collection. There must be a better way. Commented Feb 7, 2014 at 21:27

2 Answers 2

3

Use the is keyword

if ( item.Key is DataRow ) { zip = new ZipCodeError((DataRow)item.Key); } else if ( item.Key is ZipCodeTerritory ) { zip = new ZipCodeError((ZipCodeTerritory)item.Key); } else { ... } 
Sign up to request clarification or add additional context in comments.

Comments

2

You have an extra GetType() in your implementation. You are always getting RuntimeType which is the Type you get when you call GetType on Type

//Determine type and separate key and value Type type = item.Key.GetType(); if (type.Name.Equals("DataRow")) { zip = new ZipCodeError((DataRow)item.Key); } else if (type.Name.Equals("ZipCodeTerritory")) { zip = new ZipCodeError((ZipCodeTerritory)item.Key); } else { ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.