0

I have a variablemodewhich accepts values 2,3 and 4. Now my mode="2". Sometimes My mode value changes to 3 and 4. I want to perform same operation when mode = 2 and 3. Now mode="2" but breakpoint not enter inside if condition. Is my code is worong?? My Class

public string resigadd(string mode) //set resignation
    {
        string data = "";

            if (if (mode== "2" || mode== "3"))
            {
                string data1;
            }
            else
            {
              //do something
            }
       rturn data;
      }

Button Click

  protected void Submit_Click(object sender, EventArgs e)
    {
       string str = emp.resigadd(ddmode.SelectedItem.Value)   

     }

empis the object of My Employee class

Semil Sebastian
  • 111
  • 1
  • 5
  • 18
  • question is note clear – Kavindu Dodanduwa Dec 11 '14 at 07:02
  • 1
    Your code is correct. Make sure that you have some code to break on. A single variable declaration might be removed by the compiler. Especially in a Release Build. – Nico Schertler Dec 11 '14 at 07:04
  • Are you sure there are no leading or trailing spaces? – Sriram Sakthivel Dec 11 '14 at 07:04
  • 1
    Looks fine to me, assuming the compile-time type of `mode` is `string` rather than (say) `object`. Please show a short but *complete* program demonstrating the problem. I would note that if a string variable can only have numeric values, you might want to consider parsing the string upfront and using the number for the rest of the code. – Jon Skeet Dec 11 '14 at 07:08
  • @Jon Skeet, why downvote?? – Semil Sebastian Dec 11 '14 at 07:12
  • See I have an else condition before if condition starts.. – Semil Sebastian Dec 11 '14 at 07:16
  • If you cant debug this then I recommend you go back to the books and read some more before you start coding. This is a super basic condition and debugging this should be super basic as well. Your problem can be that you don't enter the first else or that mode has some whitespaces..But without debugging this you can keep on guessing... – Amir Popovich Dec 11 '14 at 07:17
  • 1
    @SaraJohn: What makes you assume the downvote is mine? I've cast a *close* vote as we can't reproduce the problem, but that's not the same thing. If you could provide a short but complete program demonstrating the problem, that would be much, much better. – Jon Skeet Dec 11 '14 at 07:19
  • @SaraJohn, Sorry am not mean its you. I just asked why? if i get the Idea then I can improve my next question... – Semil Sebastian Dec 11 '14 at 07:23
  • Well I've already said how you could improve *this* question: provide a short but complete program demonstrating the problem. I suspect that in coming up with that, you'll find the problem in some of the code you haven't shown us - but if not, we'll be in a *much* better position to help you. – Jon Skeet Dec 11 '14 at 07:28
  • I guess the reason is your desired IF condition doesn't reach in program. Just a guess which you should check for – Kavindu Dodanduwa Dec 11 '14 at 07:31
  • @Jon Skeet, I have added all details . Please check it – Semil Sebastian Dec 11 '14 at 07:35
  • 1
    That's still not a short but complete program, and we don't know what that actual input is. Additionally, as others have said, you should add some diagnostics into the `if` bodies themselves - make them print something out, or whatever is suitable in your app. – Jon Skeet Dec 11 '14 at 07:36
  • what is the type of `ddmode`? – Hamid Pourjam Dec 11 '14 at 07:47
  • @dotctor, ddmode is a dropdownlist it selects the selected value field – Semil Sebastian Dec 11 '14 at 07:50
  • you mean http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist(v=vs.110).aspx – Hamid Pourjam Dec 11 '14 at 07:51
  • @dotctor, yes same thing – Semil Sebastian Dec 11 '14 at 07:57
  • have you tried checking the value of `ddmode.SelectedItem.Value` in `Submit_Click` before passing it to `resigadd`? – Hamid Pourjam Dec 11 '14 at 07:58
  • @dotctor,Yes I checked with Breakpoint its shows correct value and pass the correct value to class – Semil Sebastian Dec 11 '14 at 07:59
  • Is this an ASP.NET WebForms? if yes, can you also show your code in Page_Load? – ken lacoste Nov 28 '15 at 07:10

3 Answers3

0

A reasonable problem could be that mode contains whitespaces. Try the following approach using Trim():

string[] Modes = new string[]{"2", "3", "4"};

string trimmedMode = mode.Trim();
if(Modes.Contains(trimmedMode))
{
    switch(trimmedMode)
    {
        case "2":
        case "3":
           //Handle case 2 and 3
           break;

        case "4":
           //Handle case 4
           break;
    }
}
Dion V.
  • 2,090
  • 2
  • 14
  • 24
  • Why? You haven't explained why the existing code doesn't work - it should be fine already. While there may be reasons to use `switch`, this answer doesn't address the question directly, IMO. (And if this is a diagnostic error on the part of the OP, there's no reason to think that changing correct code for other different correct code will help.) – Jon Skeet Dec 11 '14 at 07:09
  • 1
    Wow @JonSkeet on a rampage? I just wanted to mention `Trim()` and improve the code, but I accidentally clicked on "Post Answer". Figured that the only way this does not pass the `if` is when `mode` is not actually "2" or "3". – Dion V. Dec 11 '14 at 07:14
  • If `Trim()` was your important point, it would have been worth leading with that - that would have avoided posting a bad answer. I still don't think this is a *good* answer - it would have been better to explain that the existing code should already work, give an example with `Trim()` - but ideally an example which only calls `Trim()` once - and then *potentially* introduce the `switch` statement. I wouldn't combine the two changes. (I've undone my downvote, but it's still not worth an upvote, IMO.) – Jon Skeet Dec 11 '14 at 07:17
  • 1
    what's the point of `if(Modes.Contains(trimmedMode))` if you want to enter a switch on `trimmedMode` later? – Hamid Pourjam Dec 11 '14 at 07:28
0

I can't find anything wrong with the code. The code inside the if condition should be executed if mode has got value of "2" or "3". The only reason that this might not be working is that mode is a string variable and it might have got some empty spaces appended at the start or in the end.

Try this code:

if (mode.Trim() =="2"|| mode.Trim()=="3")
{
   string data1;

 }
else
{

} 

and try throwing exception inside the if case in that way you will get to know that the if condition is executed. It might be something with VS or compiler that why the breakpoint is never hit.

Arun Kumar
  • 907
  • 13
  • 33
-1

Plase make sure to rebuild the code and the mode variable you are using doesn't contain trailing and leading spaces. Use trim method to trim the leading and trailing spaces.

 String mode="2";

 if (mode.Trim()=="2" || mode.Trim()=="3")
       {
           //do something
           string data1;
       }
       else
       {
          //do something here.

       } 
Rahul Vishwakarma
  • 996
  • 5
  • 17
  • 35