Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Enumerating Resources

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Resolved [RESOLVED] [2005] Enumerating Resources

    Hi all,
    I've begun playing with resources in .NET. Makes sense so far getting to the resources via the My.Resources namespace, but I have a question about looping through all my resources that are stored in the default Resources.resx file.

    Let's say, just for example, I have multiple icons in there and I want the user to select one for some reason at runtime. I have figured out that I can loop through them like this, but is there a more "VB 2005 way"? Is there some way to do this using the My.Resources namespace?

    vb.net Code:
    1. Dim assem As Assembly = Assembly.GetExecutingAssembly()
    2. Dim rr As New ResourceReader(assem.GetManifestResourceStream("MyApplication.Resources.resources"))
    3. Dim de As IDictionaryEnumerator = rr.GetEnumerator()
    4. Do While de.MoveNext()
    5.     ' Do something.
    6. Loop
    Last edited by nmadd; Aug 1st, 2007 at 02:02 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Enumerating Resources

    You should basically never be calling GetEnumerator. The IEnumerable interface exists basically to support the For Each syntax. That's what you should be using.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Enumerating Resources

    That is, of course, the correct way. I was blindly following MSDN that mentioned the IDictionaryEnumerator in the ResourceReader documentation. Thanks for straightening me out jmc. If there isn't any sort of this thing for the My.Resources namespace then this is what I'm going with.

    vb.net Code:
    1. Dim assem As Assembly = Assembly.GetExecutingAssembly()
    2. Dim rr As New ResourceReader(assem.GetManifestResourceStream("MyApplication.Resources.resources"))
    3. For Each de As DictionaryEntry In rr
    4.     ' Do something.
    5.     MessageBox.Show( _
    6.         de.Key.ToString() & _
    7.         Environment.NewLine & _
    8.         de.Value.ToString())
    9. Next de

    Thanks again.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] Enumerating Resources

    Select your project in the Solution Explorer and press the Show All Files button. Expand the My Project node, then expand the Resources.resx node, then double-click the Resources.Designer.vb node. You should see before you the auto-generated code file for My.Resources. You'll see that My.Resources is indeed a namespace and it contains a module named Resources. That module just contains a bunch of normal property declarations, one for each of your resources plus a couple of others. This is no different to any other type you might create yourself. How would you enumerate the properties of one of your own types? You couldn't, unless you used reflection. It's the same case here. You're hardly going to do that so what you've suggested is the best way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [RESOLVED] [2005] Enumerating Resources

    Great. As always thanks for the help.

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