Reputation: 813
I'm having problems CASTing in a foreach loop.
I have the following code that consists of a basic object:
public class DBFieldMap
{
public String fieldName { get; set; }
public String fieldValue { get; set; }
public DBFieldMap() { }
public DBFieldMap(String fname, String fvalue)
{
fieldName = fname;
fieldValue = fvalue;
}
}
A Dictionary using the above object
public class MappedSQLFields : Dictionary<String, DBFieldMap>
{
public MappedSQLFields()
{
this.Add("firstname", new DBFieldMap());
this.Add("lastname", new DBFieldMap());
this.Add("email", new DBFieldMap());
etc...
}
}
Another object with a property that is based on the above object
public class Audience : IEnumerable
{
...
public MappedSQLFields audienceSQLMap
{
get;
set;
}
}
I would like to be able to loop through the audienceSQLMap property and extract the values for the fieldName and fieldValue properties of the DBFieldMap Dictionary object but I'm receiving the following error though at the "foreach" line. I've tried various explicit casts but still can't seem to be able to get the CASTing to work.
foreach (MappedSQLFields item in audience.audienceSQLMap)
{
//Do something with item.fieldName and item.fieldValue
}
Error 49 Cannot convert type 'System.Collections.Generic.KeyValuePair' to 'MappedSQLFields'
Any help would be greatly appreciated. Thank you.
The SAGA Continues
Okay, now that the above works thanks to everyone's help below, now I'm having problems casting the JSON equivalent of the object back to the original object. I'm receiving the same error I originally was receiving, but have tried the various CAST statements I can think of and it's still gives me grief. I'm obviously missing something regarding this whole topic.
This works fine:
var json = new JavaScriptSerializer().Serialize(audienceSQLMap);
Error on this line:
this.audienceSQLMap = (MappedSQLFields)new JavaScriptSerializer().Deserialize(json,typeof(KeyValuePair<String, DBFieldMap>));
Upvotes: 2
Views: 1210
Reputation: 8805
The key parts of the code you posted are:
public class DBFieldMap
{
public String fieldName { get; set; }
public String fieldValue { get; set; }
// elided
}
public class MappedSQLFields : Dictionary<String, DBFieldMap> { ... }
public class Audience
{
public MappedSQLFields audienceSQLMap { get; set; }
}
but want to iterate over the items in audience.audienceSQLMap
(where I assume audience
is an instance of Audience
).
You can take advantage of the fact that audience.audienceSQLMap
is MappedSQLFields
, which is Dictionary<String, DBFieldMap>
, which is in turn a collection of KeyValuePair<String, DBFieldMap>
.
The thing to do here is:
foreach ( IEnumerable<KeyValuePair<<String, DBFieldMap>> kvp in audience.audienceSQLMap )
{
DBFieldMap map = kvp.Value;
//Do something with map.fieldName and map.fieldValue
}
Upvotes: 1
Reputation: 66449
It expects you to be iterating over a key/value pair.
Modify your code to use a KeyValuePair<string, DBFieldMap>
:
foreach (KeyValuePair<string, DBFieldMap> item in audience.audienceSQLMap)
Or, more succinctly, just use var
:
foreach (var item in audience.audienceSQLMap)
{
//Do something with item.fieldName and item.fieldValue
var name = item.Value.fieldName;
var value = item.Value.fieldValue;
...
}
If all you need is the values of the Dictionary though, you could just iterate over those:
foreach (DBFieldMap map in a.audienceSQLMap.Values)
{
var name = map.fieldName;
var value = map.fieldValue;
...
}
Upvotes: 3