The class object of Hero is conflicted with the keyword of C#

Sep 20, 2012 at 7:09 AM
D3API d3api = new D3API(HostName.en_US);
var heroProfile = d3api.getHeroProfile("Battletag#0000", heroID);
string heroClass = heroProfile.class;

"class" is a keyword of C#, so, it reports an error about it.

However, how can I get class info of hero?

Thanks for ur work.
Sep 20, 2012 at 7:36 AM

I've solved this issue. Just replace the "class" with "Class".

private IDictionary<string, object> fixInvalidIndentifiers(IDictionary<string, object> dictionary)
{
	char[] chArray = new char[] { '-', '#' };
	string str = "class";
	IDictionary<string, object> dictionary2 = new Dictionary<string, object>();
	foreach (KeyValuePair<string, object> pair in dictionary)
	{
		string key = pair.Key;
		foreach (char ch in chArray)
		{
			key = key.Replace(ch, '_');
		}
		key = key.Replace(str, "Class");
		dictionary2.Add(key, pair.Value);
	}
	return dictionary2;
}

Sep 20, 2012 at 10:51 PM
Edited Sep 20, 2012 at 10:52 PM

Although that is one way to do this, there is another more elegant way. I've actually already covered this in another discussion question, but here it goes again.

 

If you use the "@" operator in front of any member it forces the literal.

For example:

D3API d3api = new D3API(HostName.en_US);
var hero = d3api.getHeroProfile("Battletag#0000", 0);
string heroClass = hero.@class;

Hope this helps,

Promedeus