CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Not sure what the best way to handle this task is.

  1. #1
    Join Date
    Jan 2021
    Posts
    3

    Not sure what the best way to handle this task is.

    I have a problem I am attempting to handle. What I have is a 6 Boolean's and I need to check the state of each of those against their current value in another method, and if any of them are different then I need to send them all at once to a different method. I was thinking it would look something like this:

    Code:
    checkTheState()
    {
      
      bool hasChanged = false;
    
      if(boolOne != boolOneCurrentState())
      {
        hasChanged = true;
      }
      if(boolTwo != boolTwoCurrentState())
      {
        hasChanged = true;
      }
    ...
    ...
    ...
      if(boolSix != boolSixCurrentState())
      {
        hasChanged = true;
      }
    
      if(hasChanged == true)
      {
        acceptNewParams(boolOne, boolTwo, boolThree ... boolSix)()
      }
    }
    which I believe would work but it seems rather elementary, and I am guessing there is probably a better way of doing it. Is this method elementary? Is there a more concise, and "nicer" way of doing this kind of operation?
    Last edited by Circuits; January 27th, 2021 at 12:51 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,043

    Re: Not sure what the best way to handle this task is.

    Just add "else" to all but the first "if"!
    And don't compare bool value with true.
    like
    Code:
    checkTheState()
    {
      
      bool hasChanged = false;
    
      else if(boolOne != boolOneCurrentState())
      {
        hasChanged = true;
      }
      else if(boolTwo != boolTwoCurrentState())
      {
        hasChanged = true;
      }
    ...
    ...
    ...
      else if(boolSix != boolSixCurrentState())
      {
        hasChanged = true;
      }
    
      if(hasChanged)
      {
        acceptNewParams(boolOne, boolTwo, boolThree ... boolSix)()
      }
    }
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2017
    Posts
    608

    Re: Not sure what the best way to handle this task is.

    Quote Originally Posted by Circuits View Post
    Is there a more concise, and "nicer" way of doing this kind of operation?
    I would use one of these, probably the first,
    Code:
    void checkTheState() {
    	const bool hasChanged =
    		(boolOne != boolOneCurrentState()) ||
    		(boolTwo != boolTwoCurrentState()) ||
    			...
    		(boolSix != boolSixCurrentState()); 
    
    	if (hasChanged) {
    		acceptNewParams(boolOne, boolTwo, ..., boolSix);
    	}
    }
    
    void checkTheState() {
    	if ((boolOne != boolOneCurrentState()) ||
    		(boolTwo != boolTwoCurrentState()) ||
    			...
    		(boolSix != boolSixCurrentState())) 
    	{ 
    		acceptNewParams(boolOne, boolTwo, ..., boolSix);
    	}
    }
    The || is the logical-OR operator.
    Last edited by wolle; January 28th, 2021 at 04:27 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,506

    Re: Not sure what the best way to handle this task is.

    The first fundamental question is - why 6 individual bool variables? Why not have say an uint8_t variable where each bit represents a specific state. It's easy to check/set a particular bit and to test if two are equal etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.10.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)