I have a problem with 'Resources'.

Poppa Mintin

Well-known member
Joined
Jan 4, 2018
Messages
45
Programming Experience
10+
Hi, after using this form of declaration for many years...
VB.NET:
 Private fred_1 As Image = My.Resources.ResourceManager.GetObject("Fred")
Today I get an error message; -
'Resources' is not a member of 'Our_Fred.My'.

I find it odd that the above code was copied and pasted from a test project, which I've just finished working on, and which works perfectly well.

What's happened ?

Earlier today I installed the latest VS 2017 update, might that have something to do with the problem ?

Microsoft Visual Studio Community 2017
Version 15.6.0
VisualStudio.15.Release/15.6.0+27428.1
Microsoft .NET Framework
Version 4.7.02556


Installed Version: Community

Poppa.


PS. I have the same problem with; -
VB.NET:
Private fred_1 As Image = DirectCast(My.Resources.ResourceManager.GetObject("Fred"), Image)

Pop.
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
Resources works for me with latest VS also. Perhaps Clean Solution and Rebuild ?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Once you get that issue sorted out, there should be no need for you to use ResourceManager. That first code snippet should be able to be changed to this:
Private fred_1 As Image = My.Resources.Fred

By the way, if your original code compiled then you must have Option Strict Off. I strongly recommend changing that in the project properties and also in the IDE options, so that it is On by default for all future projects.

As for the issue, what happens if you open the Resources page of the project properties? Can you see your resource there? Can you add a new one? If you click Show All Files in the Solution Explorer and expand the My Project node, can you see the Resources code file containing the My.Resources.Resources module?
 

Poppa Mintin

Well-known member
Joined
Jan 4, 2018
Messages
45
Programming Experience
10+
By the way, if your original code compiled then you must have Option Strict Off. I strongly recommend changing that in the project properties and also in the IDE options, so that it is On by default for all future projects.

Ah... The test project I was using I started several years ago, I just clear out what I don't need and build my test with what's left, adding what I need to for whatever I'm trialing.

It could well be that that project was started before I realised I didn't have Option Strict on by default.

I've just scrapped it! Next time I need a test bed I'll start a new one.


Poppa.
 

Poppa Mintin

Well-known member
Joined
Jan 4, 2018
Messages
45
Programming Experience
10+
Resources works for me with latest VS also. Perhaps Clean Solution and Rebuild ?

Yeah, I started a new project and just copied everything from the previous one, everything works ok now.

I'm just going to try out John Mc's advice. I'm sure it'll work.


Poppa.
 

Poppa Mintin

Well-known member
Joined
Jan 4, 2018
Messages
45
Programming Experience
10+
I'm just going to try out John Mc's advice. I'm sure it'll work.

Poppa.

Yep... It works just fine.

Can I do something similar with: -
VB.NET:
 Private Sub Play(ByVal track As String)
        Dim Sound As MemoryStream
        Sound = DirectCast(My.Resources.ResourceManager.GetObject(track), MemoryStream)
        My.Computer.Audio.Play(Sound, AudioPlayMode.WaitToComplete)
    End Sub

Where I could declare a few variables with an individual name of their own, then call them when needed? If so, the question would be... declare them 'As' what ?

My guess would be: -

VB.NET:
Private track_1 As MemoryStream = My.Resources.AllYouNeedIsFred

Poppa.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
My.Resources.AllYouNeedIsFred is just a property, like any other property. It has a data type, just like any other property. Just as you would for any other property, if you want to assign the property value to a variable then you would declare that variable to be the same type as the property or, if it's more appropriate, a type that that type inherits. If that property is type MemoryStream or some type that inherits MemoryStream then you can assign it to a variable of type MemoryStream.

By the way, if you check the implementation of that property, you'll find that it does much as you were doing yourself, i.e. uses ResourceManager and casts.
 
Top Bottom