Solved

Problems with switch statement using Visual C++

Posted on 2004-03-30
16
758 Views
Last Modified: 2013-11-20
Hi,

I am posting some code involving some switch statements.  For some reason this code doesn't run as expected.  Using the debugger I followed it's execution -- it works as expected up to the switch statement.  At which point it jumps over all the case statments -- including the default statement.  I am at a lose to why...  Any help would be appreciated


      if (pScrollBar->GetDlgCtrlID() == IDC_GovernmentSlider)
      {
            switch (nSBCode)
            {
                  case TB_BOTTOM:
                        break;
                  case TB_ENDTRACK:
                        break;
                  case TB_LINEDOWN:
                        break;
                  case TB_LINEUP:
                        break;
                  case TB_PAGEDOWN:
                        break;
                  case TB_PAGEUP:
                        break;
                  case TB_THUMBPOSITION :
                        m_GovValue = nPos/10;
                        m_GovValCtrl.UpdateData(TRUE);
                        break;
                  case TB_THUMBTRACK :
                        m_GovValue = nPos/10;
                        m_GovValCtrl.UpdateData(TRUE);
                        break;
                  case TB_TOP:
                        break;
                  default :
                        break;
            }
      }
      else if (pScrollBar->GetDlgCtrlID() == IDC_ContractorSlider)

      {
            switch(nSBCode)
            {
                  case TB_BOTTOM:
                        break;
                  case TB_ENDTRACK:
                        break;
                  case TB_LINEDOWN:
                        break;
                  case TB_LINEUP:
                        break;
                  case TB_PAGEDOWN:
                        break;
                  case TB_PAGEUP:
                        break;
                  case TB_THUMBPOSITION:
                        break;
                  case TB_THUMBTRACK:
                        break;
                  case TB_TOP:
                        break;
                  default:
                        break;
            }
      }
0
Comment
Question by:nasa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
16 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716331
Are you assigning any value to  'nSBCode' before entering the switch blocks?

-Lakshman
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716402
Or where is this code written, is it in some message handling like OnHScroll or OnVScroll()?

-Lakshman
0
 
LVL 1

Author Comment

by:nasa
ID: 10716420
Thanks for the reply,

And Yes I am using this in OnHScroll.

Nasa
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716516
If you are handling the event OnHScroll for the Scroll Bar the switches should have SB_
TB_ is used for the event handling on the slider control.

-Lakshman
0
 
LVL 1

Author Comment

by:nasa
ID: 10716662

I am handling events for a slider bar control, so TB_ is correct (as you note)

-Nasa
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10716701
Can you plz tell what u r checking here?
pScrollBar->GetDlgCtrlID() == IDC_GovernmentSlider
Is nSBCode belongs to the slider bar control?
0
 
LVL 1

Author Comment

by:nasa
ID: 10716902
sure,

My dialog has 2 slider controls (one name IDC_GovernmentSlider and the other IDC_ContractorSlider) I am checking to see which one is giving me the HScroll event.  

As far as the nSBCode is concerned -- it doesn't "belong" but it does provided the new position(as the following link talks about)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_slider_notification_messages.asp

nasa
0
 
LVL 14

Expert Comment

by:wayside
ID: 10717168
What is pScrollBar->GetDlgCtrlID()  returning?

Are you sure it is equal to either IDC_GovernmentSlider or IDC_ContracterSlider?
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10717184
I think if statement should work

  if( nSBCode == TB_THUMBPOSITION)
                    {
                     m_GovValue = nPos/10;
                    m_GovValCtrl.UpdateData(TRUE);
}

-Lakshman
     
0
 
LVL 1

Author Comment

by:nasa
ID: 10717382
Ok,

tried that with no love... the if statement just reolved to false...

are nSBCode and TB_THUMBPOSITION compatiable (they should be from msdn -- but to make sure....)

Nasa
0
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 100 total points
ID: 10717725
No.The following code works
int x=1;
      
      if(pScrollBar->GetDlgCtrlID() == IDC_SLIDER1)
      {

            switch (nSBCode)
              {
                  case TB_THUMBTRACK:
                        x=2;
                        break;
                  case TB_THUMBPOSITION:
                        x=3;
                        break;
                     default :
                        break;
              }
      }
      else
      if(pScrollBar->GetDlgCtrlID() == IDC_SLIDER2)
      {

            switch (nSBCode)
              {
                  case TB_THUMBTRACK:
                        x=2;
                        break;
                  case TB_THUMBPOSITION:
                        x=3;
                        break;
                     default :
                        break;
              }
      }

Just click the slider on your control and you can see x value changing to 2 and 3 through the debugger..

-Lakshman
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 100 total points
ID: 10717775
What is the value of nSBCode?

Your default: statements have no code, so there would be nothing to step over. That's why you seem to skip over it.

Add some code in there like AfxMessageBox("Shouldn't get here");

andn see what happens.
0
 
LVL 1

Author Comment

by:nasa
ID: 10718056
Cool,

I am not sure how my code was different than  lakshman_ce's -- but ....
Adding the AfxMessageBox caused the switch statement to work as expected.... ,

Now the problem is ....

 m_GovValue = nPos/10;
 m_GovValCtrl.UpdateData(TRUE);

where m_GovValue is a double and m_GovValCtrl is a edit control set to accept doubles -- shouldn't it update the value within the edit box as the slider changes?

Nasa
0
 
LVL 14

Expert Comment

by:wayside
ID: 10718111
> m_GovValCtrl.UpdateData(TRUE);

You can't update just one control using UpdateData(); call

UpdateData(FALSE);

to put all the values back into the dialog. The ones you didn't change will have no visible effect on what you see.





0
 
LVL 14

Expert Comment

by:wayside
ID: 10718137
>Adding the AfxMessageBox caused the switch statement to work as expected.... ,

I would have expected this to make only the default part of it work.

The fact that the whole thing now works tells me you might have had some stringe build problem.

Rule #1 when weird stuff happens in Visual Studio: rebuild the entire project.
0
 
LVL 1

Author Comment

by:nasa
ID: 10718454
And there was much rejoicing!!!

Thanks a lot to both of you for your time and effort

Nasa
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question