|
-
Feb 2nd, 2023, 09:32 AM
#1
Thread Starter
Lively Member
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);
}
-
Feb 2nd, 2023, 09:44 AM
#2
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()
-
Feb 2nd, 2023, 11:52 AM
#3
Thread Starter
Lively Member
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 ()?
-
Feb 2nd, 2023, 01:07 PM
#4
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 ().
-
Feb 2nd, 2023, 07:15 PM
#5
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.
-
Feb 3rd, 2023, 05:20 PM
#6
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
I have a library for this is using .NET Core 7 found here.
-
Feb 8th, 2023, 09:24 AM
#7
Thread Starter
Lively Member
Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List
Okay, thanks. I like that .NET 7 winforms project.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|