AND statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andyoye
    New Member
    • May 2009
    • 3

    AND statement

    Code:
    public void FormEvents_Loading(object sender, LoadingEventArgs e)
            {
                XPathNavigator root = MainDataSource.CreateNavigator(); 
    
                XPathNavigator C1 = this.CreateNavigator();
                string txtC1 = C1.SelectSingleNode("/my:myFields/my:field76",     this.NamespaceManager).Value;
                 
    XPathNavigator C2 = this.CreateNavigator();
                 string txtC2 = C2.SelectSingleNode("/my:myFields/my:field79", this.NamespaceManager).Value;
    
                 XPathNavigator C3 = this.CreateNavigator();
                string txtC3 = C3.SelectSingleNode("/my:myFields/my:field83", this.NamespaceManager).Value;
                            
                if (!this.New)
               if (txtC1 == "false" && txtC2 == "false" && txtC3 == "false")
                 {
                    e.CancelableArgs.Cancel = true;
                    e.CancelableArgs.Message = "You don't have permission to open the form";
                    return;
                  }
            }
    Even if txtC1, txtC2 & txtC3 values equals false, form opens up. If I just use one condition like: if(txtC1 == "false", code does work (form will not open)

    Looks like AND operator is not working? as each individual check works.
    Last edited by tlhintoq; May 21 '09, 03:26 PM. Reason: [CODE] ... your code here ... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    It really helps if you describe your need/goal and the problem someplace in your post. Yeah, we can infer your intent here but its not to not make the people who are going to help you have to struggle just to figure out the question.

    Line 14 does nothing
    You have not enclosed the block following that "if" condition with braces, so there is no block to execute if this.New is not true

    Remember that comparrisons to strings are case sensitive.
    You really want to check in a manner that bypasses that.
    if (txtC1.ToLower( ) == "false" && txtC2.ToLower() == "false"

    Put a breakpoint on line 15. When code stops there hover your mouse over the 3 values or look in the watch window to see what the actual values are of txtC1,2,3. I'll bet you find they are not "false". All it would take is an extra space for example and they would not == "false". A more forgiving check might be
    if (txtC1.ToLower( ).Contains("fal se") && txtC1.ToLower() .Contains("fals e")

    Comment

    • andyoye
      New Member
      • May 2009
      • 3

      #3
      Sorry for not mentioning what i am looking to achieve.

      Goal: If the form is Not new AND txtC1 AND txtC2 AND txtC3 == "false", dont open the form.

      I put a line break on line 15 and looks like all values are "false".

      As menetioned in original post, if I try

      txtC1 == "false" or txtC2 == "false" or txtC3 == "false" individually, I get the desired result (form doesn't open)

      thanks

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I guess I"m a little confused as to what conditions you could create where you would being opening a form what wasn't "new". I'm not really sure what you mean by new? Are you trying to create a form that can only open one instance?

        If so I'm not so sure I would try to put that code in the form. You have to create the second instance in order to have a .Loading() which is where you check exists. That means you allocate time, memory, resources, objects to the creation of a second instance whether you intend to use it or not. Why create all that just to delete it again? If your CALLING code checks to see if it already exists it can keep a second instance from every being created.

        Had you considered only creating one instance of the form then using the ".Show()" and ".Hide()" methods? Thus it always exists, it just may not be visible.

        As menetioned in original post, if I try

        txtC1 == "false" or txtC2 == "false" or txtC3 == "false" individually, I get the desired result (form doesn't open)
        Right, because at least one really is equal to "false" EXACTLY. You said that if you only checked txtC1 that it worked fine. This would indicate that txtC2 or txtC3 is not really an EXACT match for "false".

        Another way to diagnose it might be to output those three variables to the console just before you check them.

        Or nest your 'if' statements so you can better find the mismatach

        Code:
        if (txtC1.Text.ToLower().Contains("false"))
        {
           console.Writeline("1 matched");
           if (txtC2.Text.ToLower().Contains("false"))
           {
              console.Writeline("2 matched");
                 if (txtC3.Text.ToLower().Contains("false))
                 {
                    //  This can only be reached if all three match
                 }
              }
           }
        }

        Comment

        • andyoye
          New Member
          • May 2009
          • 3

          #5
          No, If I check ANY of the three (txtC1, txtC2, txtC3) alone, it works ...meaning they all equates to "false"

          if(txtC1 =="false") .... this statement works

          if(txtC2== "false)".....th is statement works

          if(txtC3 == "false") .... this statement also works.


          Problem is when I try to AND them, then it doesnt work.
          (if txtC1 == "false" && txtC2 =="false" && txtC3 == "false")

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Originally posted by andyoye
            No, If I check ANY of the three (txtC1, txtC2, txtC3) alone, it works ...meaning they all equates to "false"

            if(txtC1 =="false") .... this statement works

            if(txtC2== "false)".....th is statement works

            if(txtC3 == "false") .... this statement also works.


            Problem is when I try to AND them, then it doesnt work.
            (if txtC1 == "false" && txtC2 =="false" && txtC3 == "false")
            Code:
                        string txtC1 = C1.SelectSingleNode("/my:myFields/my:field76",     this.NamespaceManager).Value;
            These values are only being set one time, at creation.
            Perhaps they should become properties so they get updated every time you check them. Sometimes thing don't happen as fast or exactly in the sequence we think they do. They may not equal "false" when created, but do equal "false" millisenconds later at the time you check them, because their values have finally returned.

            Code:
            string txtC1
            {
            get
            {
            return  C1.SelectSingleNode("/my:myFields/my:field76",   this.NamespaceManager).Value;
            }
            }

            Comment

            Working...