Results 1 to 7 of 7

Thread: GetEnumerator error for constructing Dictionary of Icons from Resouce List

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    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);
    	}

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    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()
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    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 ()?

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    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 ().
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: GetEnumerator error for constructing Dictionary of Icons from Resouce List

    I have a library for this is using .NET Core 7 found here.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    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
  •  



Click Here to Expand Forum to Full Width