Saturday, May 9, 2009

&& operator in C# ??? can help me plsss...?

protected void btnsearch_Click(object sender, EventArgs e)


{


if (DateTime.Parse(Date1.Text) %26lt; DateTime.Now.Date)


{


lblErrSearch.Visible = true;


lblErrSearch.Text = "The date cannot be eariler than today";


}


else


{


lblErrSearch.Text = "Hello World";


}


}





Can help me to do Validation in C#?


Actually I have 2 textbox for Date (Date1 and Date2). There is a popup calender will appear below each texbox . I want to validate :


%26gt; when the date chosen is ealier than today's date.


%26gt;the code above is for one Date1 textbox - and its working perfectly! but I want the same error message to be displayed when the Date2 is earlier than today's date.


%26gt; I want to check both textbox.





I am not sure how to use the AND OPERATOR in c#


I tried this code(modify the IF..THEN ELSE )..but it doesn't work =(





if (DateTime.Parse(Date1.Text) %26amp;%26amp; DateTime.Parse(Date2.Text)%26lt; DateTime.Now.Date)





i donno whtr to use %26amp;%26amp; or %26amp;

%26amp;%26amp; operator in C# ??? can help me plsss...?
Well, for your expression, you really want an || statement (or operator) - that is, if date1 %26lt; now OR date2 %26lt; now, throw an error. If you use an and (well, if you use it correctly anyways), the condition would only be true when both are true, only capturing a subset of your error cases.


----


if ( (DateTime.Parse( Date1.Text ) %26lt; DateTime.Now.Date) ||


( DateTime.Parse( Date2.Text ) %26lt; DateTime.Now.Date) )


---


Good practice for a conditional this long is to represent the terms with accurately named boolean variables, which would make this whole thing more readable:





bDate1Bad = DateTime.Parse ( Date1.Text ) %26lt; DateTime. Now. Date;





bDate2Bad = DateTime.Parse ( Date2.Text ) %26lt; DateTime. Now. Date;





if ( bDate1Bad || bDate2Bad ) ...
Reply:The difference betwen the '%26amp;' operator and the '%26amp;%26amp;' operator is that when using the single %26amp;, the program will look at the first statement, and if it is false, it will not bother evaluating the second one. When you use the %26amp;%26amp; operator, both sides of the expression are evaluated, even if the first one is false. To be honest, I have never really coded a program where it mattered how the statment was interpreted, so I always use '%26amp;%26amp;' to be safe.


Now, about your issue... if the code snippet above is a direct copy, you have a logic problem : the first statement within the if is merely looking to see if Date.Text exists - you are not actually comparing it to anything. I am thinking you really want





If(DateTime.Parse(Date1.Text) %26gt; DateTime.Parse(Date2.Text)) %26amp;%26amp; ....





First, compare the first date to the second, and then see if either of the dates is earlier than today. In that case, you will need to add another comparison. FYI - I'm sure you have found this out already, but date/time objects are notoriously difficult to work with. To help prevent errors, I never let a user enter a date - I create combo boxes that force then to choose valid choices. That way I can control the input, and don't have to worry about invalid characters. You still need to check to make sure that the dates are not one before the other, but it makes the rest of the process much easier. Good luck.


No comments:

Post a Comment