M
M
Mykola Franchuk2016-05-04 22:38:13
C++ / C#
Mykola Franchuk, 2016-05-04 22:38:13

How to access resources dynamically (by index or otherwise)?

There is a code in which, depending on the condition, one or another audio file (.wav) should be launched. Also included are 46 .wav files as resources that can be accessed via:

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
///...
player.Stream = Properties.Resources.a; // a -- audio file

The question is how, depending on the state of the program, access different files? I mean by index (and pull as an array element) or some other more dynamic way. Do not write after all 46 times if else if.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2016-05-05
@Sing303

Properties.Resources.ResourceManager
See the GetObject and GetStream methods, you can refer to a resource by name

D
Dmitry Kovalsky, 2016-05-05
@dmitryKovalskiy

Design patterns have been invented for such tasks. Of course 46 if else if is evil. In my opinion, a state-machine may come in handy here, although it’s not very important. The bottom line is - You will make 46 classes that implement the same GetStream method (name for example), and in the code you will replace the class with user actions. Something like

someObject.State = new WarningState()
player.Stream = someObject.State.GetStream();

You can also write 1 wrapper over switch / case, although this is not a very beautiful solution. But it's also possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question