[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:
Dim assem As Assembly = Assembly.GetExecutingAssembly()
Dim rr As New ResourceReader(assem.GetManifestResourceStream("MyApplication.Resources.resources"))
Dim de As IDictionaryEnumerator = rr.GetEnumerator()
Do While de.MoveNext()
' Do something.
Loop
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.
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:
Dim assem As Assembly = Assembly.GetExecutingAssembly()
Dim rr As New ResourceReader(assem.GetManifestResourceStream("MyApplication.Resources.resources"))
For Each de As DictionaryEntry In rr
' Do something.
MessageBox.Show( _
de.Key.ToString() & _
Environment.NewLine & _
de.Value.ToString())
Next de
Thanks again.
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.
Re: [RESOLVED] [2005] Enumerating Resources
Great. As always thanks for the help.