CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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?

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    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...

  4. #4
    Join Date
    Jun 2008
    Posts
    154

    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!

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Single Public Bool affecting all Instanced Classes

    Quote Originally Posted by bixel View Post
    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);

  6. #6
    Join Date
    Jun 2008
    Posts
    154

    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.

  7. #7
    Join Date
    Dec 2009
    Posts
    596

    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.

  8. #8
    Join Date
    Jun 2008
    Posts
    154

    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

    Code:
    bool thisHere;
    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?

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  10. #10
    Join Date
    Jun 2008
    Posts
    154

    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?

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Single Public Bool affecting all Instanced Classes

    Quote Originally Posted by bixel View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured