-
November 18th, 2010, 11:23 AM
#1
Single Public Bool affecting all Instanced Classes
is there a way to have a Single Public Bool affecting all Instanced Classes?
Code:
public class SoundPlayer
{
SoundEffect soundToPlay;
public SoundPlayer(SoundEffect sound)
{
this.soundToPlay = sound;
}
public bool Mute;
public bool MuteGlobal;
public void Play()
{
if (!MuteGlobal)
{
if (!Mute)
{
soundToPlay.Play();
}
}
}
}
Here is the scenario, I have an external soundcard and sometimes its unplugged, if I accidentally run my program with the sounds playing it will crash - I want to create a little bool I can use in Main, to say MuteGlobal, and where-ever SoundPlayer is trying to play - mute every instance of it. Is this possible? As it is I have to got through every class that Soundplayer is in and comment it out.
-
November 18th, 2010, 11:35 AM
#2
Re: Single Public Bool affecting all Instanced Classes
You can use a static field or property
Code:
public static bool MuteGlobal { get; set; }
That being said, instead of using this approach why not capture the error condition using a try/catch block to prevent the program from crashing?
-
November 18th, 2010, 02:59 PM
#3
Re: Single Public Bool affecting all Instanced Classes
I will do that thanks! (that is detect for soundcard)
now I just need to figure out how to do that...
-
November 18th, 2010, 03:33 PM
#4
Re: Single Public Bool affecting all Instanced Classes
Code:
[System.Runtime.InteropServices.DllImport("winmm.dll", SetLastError = true)]
public static extern uint waveOutGetNumDevs();
then in main contructor
Code:
if (waveOutGetNumDevs() == 0)
SoundPlayer.MuteGlobal = true;
else
SoundPlayer.MuteGlobal = false;
works perfectly thanks!
-
November 18th, 2010, 03:43 PM
#5
Re: Single Public Bool affecting all Instanced Classes
Originally Posted by bixel
Code:
if (waveOutGetNumDevs() == 0)
SoundPlayer.MuteGlobal = true;
else
SoundPlayer.MuteGlobal = false;
That type of code is redundant, unnecessary, and shows a lack of understanding of the boolean type. You are already evaluating a booean, so just assign it directly:
Code:
SoundPlayer.MuteGlobal = (waveOutNumDevices() == 0);
-
November 18th, 2010, 05:03 PM
#6
Re: Single Public Bool affecting all Instanced Classes
I'm a grumpy bitter man, GRUMPY GRUMPY GRUMPY, I am going to show you how stupid you are. Here ya go - now don't you feel like an idiot. Looks at your sloppy code. SLOPPY SLOPPY SLOPPY. Gee, why are you even posting here? - your code shows a lack of understanding, and its redundant and unnecessary. Why don't you just go back to working at McDonalds, or better yet just kill yourself.
-
November 18th, 2010, 05:45 PM
#7
Re: Single Public Bool affecting all Instanced Classes
That's funny. On a side note many times we can program something slicker than another way but if the other way is easier on the human eyes and easier to maintain then sometimes the easier to maintain code is the way to go. If you can forecast your if else block will grow with some statements then that may be the way to go.
-
November 18th, 2010, 06:08 PM
#8
Re: Single Public Bool affecting all Instanced Classes
Yeh. it was my kneejerk reaction - I was drinking a tun of coffee. There is a lot of stuff I still don't know. But it never would have occurred to me that waveOutNumDevices was ever going to act like a bool. Since it is a uint and therefor a number.. but then somebody would say
Code:
waveOutNumDevices() == 0
is a bool!
but I would have said
is a bool! > and this
Code:
uint waveOutGetNumDevs()
is a number!
Even if light is both a wave and a particle it will take me some time to understand it. And besides the code looks unreadable to me, if week later I stumbled upon that again I would look at it but not understand at all what its doing.
SoundPlayer.MuteGlobal = (waveOutNumDevices() == 0);
I've just turned a bool into a number?
-
November 18th, 2010, 06:17 PM
#9
Re: Single Public Bool affecting all Instanced Classes
Yeah, don't react like a jerk when I try to help you. Boolean operators are a simple concept and you should understand them, which you obviously don't, which is why I posted that.
The logical boolean comparison operators (==, !=, >, <) return... booleans! Imagine that! When I see code like you wrote above I know immediately that this person is a beginner and doesn't really understand boolean logic. That's ok! We all have to start somewhere. You need to be a little less sensitive to constructive criticism though.
As an exercise, create a class and override the boolean operators ==, !=, >, and <. This will help you understand how things work.
-
November 18th, 2010, 06:20 PM
#10
Re: Single Public Bool affecting all Instanced Classes
I think my brain wants to see this
SoundPlayer.MuteGlobal = if (waveOutNumDevices() == 0);
can we be friends?
-
November 18th, 2010, 07:15 PM
#11
Re: Single Public Bool affecting all Instanced Classes
Originally Posted by bixel
I think my brain wants to see this
SoundPlayer.MuteGlobal = if (waveOutNumDevices() == 0);
can we be friends?
I am not upset, but your brain wants to see invalid code, so you should probably just work on understanding boolean logic instead
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|