GetEnumerator error for constructing Dictionary of Icons from Resouce List
I converted the following from VB.NET code (when Option Strict=On) and am getting red underlined errors from Intellisense on two lines. I need to spend more time on this, which will take a while, but if you can see why these errors are occurring, then please advise on what's wrong.
Code:
public Dictionary<string, object> GetMyResourcesDictionary()
{
Dictionary<string, object> ItemDictionary = new Dictionary<string, object>();
System.Collections.IDictionaryEnumerator ItemEnumerator = null;
var ItemResourceSet = Properties.Resources.ResourceManager.GetResourceSet(new System.Globalization.CultureInfo("en"), true, true);
List<string> ResourceNameList = new List<string>();
//Get the enumerator for My.Resources
ItemEnumerator = ItemResourceSet.GetEnumerator; //******GetEnumerator is underlined in red (Intellisense)
while (ItemEnumerator.MoveNext=true) //******ItemEnumerator.MoveNext is underlined in red (Intellisense)
{
ResourceNameList.Add(ItemEnumerator.Key.ToString());
}
foreach (string resourceName in ResourceNameList)
{
ItemDictionary.Add(resourceName, GetItem(resourceName));
ImageList1.Images.Add((Icon)(GetItem(resourceName)));
}
ResourceNameList = null;
return ItemDictionary;
}
public object GetItem(string resourceName)
{
return Properties.Resources.ResourceManager.GetObject(resourceName);
}
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
C# isn't as forgiving as VB.Net. When calling methods, you must call them with the () suffix.
So this:-
Code:
ItemEnumerator = ItemResourceSet.GetEnumerator
Must become this:-
Code:
ItemEnumerator = ItemResourceSet.GetEnumerator()
This as well:-
Code:
ItemEnumerator.MoveNext
Must become this:-
Code:
ItemEnumerator.MoveNext()
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
Thank you, it works perfectly. I guess that would mean that in VS2022 (i have Pro) methods are colored gold, like ToString, so it needs ()?
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
Yes, ToString is a method too. Not sure about the colour coding thing though. That's configurable and can mean different things depending on Visual Studio settings and whatnot. If methods are coloured gold for you then yes, all of them need to be called with ().
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
I would strongly recommend using parentheses when calling methods in VB too, for clarity. Personally, the only time I don't use parentheses is on constructors, where empty parentheses can make it look like an array is being created. The New keyword makes confusing it with a property very unlikely, where that is easily done for method calls.
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
I have a library for this is using .NET Core 7 found here.
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
Okay, thanks. I like that .NET 7 winforms project.