I want to do something if the day, and time of the day equal true in a if statement. I have the day part down, just can't figure out the time part out. Let say I wan the time to be 9AM.
Here is what I have so far
var dt_check_monday = DateTime.Now.DayOfWeek;
if (dt_check_monday == DayOfWeek.Monday && time_now = DateTime.Now.Hour==9)
{
//do something
}
I can't use this I get an error:
Operator '&&' cannot be applied to operands of type 'bool' and 'System.TimeSpan'
Thanks for any help in advance.
= is an assignment. == is the 'equals'
Your second = should be a ==
You should just do this:
if (DateTime.Now.DayOfWeek == DayOfWeek.Monday && DateTime.Now.Hour == 9)
{
}
Your code has an assignment to an undeclared variable time_now and you're doing an assignment time_now = which is what's causing it to fail.
You should also consider revising how you name your variables, dt_check_monday means absolutely nothing if the value inside it is DayOfWeek.Wednesday, consider changing it to something like dt_currentDayOfWeek but that already exists in the form of DateTime.Now.DayOfWeek which is why I dropped the variable from my example.
I think time_now is having TimeSpan datatype. So you can try this
if (dt_check_monday == DayOfWeek.Monday && time_now.Hours == 9)
{
//do something
}
If you want to keep time_now for later use, you have to encase the assignment in the if-statement with paratheses.
var dt_check_monday = DateTime.Now.DayOfWeek;
if (dt_check_monday == DayOfWeek.Monday && (time_now = DateTime.Now.Hour) == 9)
{
//do something
}
Related
I'm trying to assign a boolean a false value if the int value day is the 31st, and the int value month is any of the following, 4,6,9,11.
What I currently have is not working, how should it be structured?
if (day == 31 && month == 4 || month == 6 || month == 9 || month == 11)
{
validity = false;
}
Granted, I could go on about operator precedence (which one goes first), but really, you should never have to worry about that because you should always be very explicit about how you want something to be evaluated. Use your parentheses to do so.
You can use newer features of C# to make it even more clear what you're doing:
if ((day is 31) && (month is 4 or 6 or 9 or 11))
All answers are valid, but here is my example if you want to shorten expression, what if you need to compare more month values, you can put all values in array and search if month value is contained in array.
if(day == 31 && new int[] { 4, 6, 9, 11 }.Contains(month))
{
}
Also you can create extension method name In and check if your month value is contained in array.
public static class Extensions
{
public static bool In<T>(this T value, params T[] array)
{
return array.Contains(value);
}
}
if (day.In(31) && month.In(4, 6, 9, 11))
{
}
But as I said this depends on you, use what is more readable to you or someone else who look at your code :)
I have this var, but I want to change it's content depending on the statement, I can't get it working because when I use it, VS says it has not been declared, even if the statement is true...
if (DateTime.Today.Day > 28 && DateTime.Today.Day < 2)
{
var days = GetDaysLikeMe(DateTime.Today).Take(50).Where(d => d.Date.Day > 28 && d.Date.Day < 2).Take(4);
}
else
{
var days = GetDaysLikeMe(DateTime.Today).Take(50).Where(d => d.Date.Day < 28 && d.Date.Day > 2).Take(4);
}
EDIT:
I've tried to declare the variable outside the box... But can't get it working neither, the function I keep on var days is this
public IEnumerable<DateTime> GetDaysLikeMe(DateTime currentDate)
{
DateTime temp = currentDate;
while (true)
{
temp = temp.AddDays(-7);
yield return temp;
}
}
Declare the variable outside the scope of the if statement:
IEnumerable<DateTime> days;
if (DateTime.Today.Day > 28 && DateTime.Today.Day < 2)
{
days = GetDaysLikeMe(calendario.Value.Date).Take(50).Where(d => d.Date.Day > 28 && d.Date.Day < 2).Take(4);
}
else
{
days = GetDaysLikeMe(calendario.Value.Date).Take(50).Where(d => d.Date.Day < 28 && d.Date.Day > 2).Take(4);
}
Declare the variable outside the if block (without assigning a value - you can't use var in this case though, you'll have to specify the type), and then only assign a value to it inside.
There is nothing wrong with your code as it is now - you can easily define varables with var or explicitly specifying type inside any block, including if.
What likely happens is that you are trying to use this varable outside the block it is defined (i.e. after if statement) which is where days becomes undefined.
Fix: define variable outside the if, but you need explicit type there. If you have ReSharper it allows easily change between var/explicit type. Otherwise you'll have to figure out type yourself (in your case it is liklye IEnumerable<DateTime>).
Trying to write a piece of code to compare two strings. If either are equal to the textbox then it opens a new winform. I know how to do the winform part.
string CreditCard1 = "Some numbers";
string CreditCard2 = "Some Numbers";
string RFIDCard1 = "E00700000330E44C";
string RFIDCard2 = "E00700000338E539";
if(CardScan_txtBx = CreditCard1 || RFIDCard1)`
I get an error from MSVS 2010 saying:
Operator '||' cannot be applied to operands of type 'string' and 'string'
Is there a way to do what I want?
This line is the culprit:
if(CardScan_txtBx = CreditCard1 || RFIDCard1)`
Try:
if(CardScan_txtBx.Text == CreditCard1 || CardScan_txtBx.Text == RFIDCard1)
On a side note, it scares me that you're apparently working with credit card information, but don't know how to compare values in a text box. I really, really hope, for the sake of your customers, that you plan on investigating how to securely manage that information.
There are three problems here:
You cannot compare against multiple values using an OR (||). This is a surprisingly common misconception, but makes no sense to the compiler.
Comparison in C# is done with ==. = is for assignment.
A TextBox is not a string; you need to use its Text property to get or set the text it contains.
So in the end, your if statement should look like this:
if(CardScan_txtBx.Text == CreditCard1 || CardScan_txtBx.Text == RFIDCard1) {
// ...
}
Ok you have 2 issues here, firstly single equals is assignment not comparison and secondly each argument separated by an or needs to be a bool, ie should be
if(CardScan_txtBx == CreditCard1 ||CardScan_txtBx == RFIDCard1)
Could you use else if?
if(CardScan_txtBx == CreditCard1)
{
//Do something
} else if(CardScan_txtBx == RFIDCard1)
{
//Do something
}
The other answers have the correct code, here is an explanation of the why. When you use the || operator, it is expecting an expression on either side to be something that evaluates to a bool (true or false). When you wrote CardScan_txtBx.Text == CreditCard1 || RFIDCard1 you have an statement that evaluates to a bool on the left, CardScan_txtBx.Text == CreditCard1 and you have a statement that evaluates to string on right RFIDCard1 Because a string is not a bool, you get the compile time error. that is why you must repeat the == operator on the right hand side so that you say CardScan_txtBx.Text == RFIDCard1
Try out with following code.....
if (CardScan_txtBx.Equals(CreditCard1) || CardScan_txtBx.Equals(RFIDCard1))
{
//Code
}
Try this:
if (CardScan_txtBx.Text == CreditCard1 || CardScan_txtBx.Text == RFIDCard1)
See here: http://msdn.microsoft.com/en-us/library/53k8ybth%28v=vs.71%29.aspx
Not: =
Right: expr1 == expr2
You can also use a List.
List<string> listOfValidStrings = new List<string>();
//Initialise all valid strings.
if(listOfValidStrings.contains(txtbox.text())
{ do something.}
So my problem is that i am getting an invalid argument error in this section of code. What it is meant to do is take a hand of cards and get the sum of their total value. Then if the value is greater than 21 it checks to see if any of the cards in the hand is an ace(or the type is == ace and it's card totalVal == 11) now my problem is the statement i have written for this will run regardless of if there is an ace in the hand or not and throws an error.
I was wondering if there is any other way i can write the statement below in order to get this to run correctly?
public int HandTotal()
{
int total = CurrentCards.Sum(n => n.Total);
**while ( total > 21 && CurrentCards.First(n => n.Type == 13 && n.Total == 11) !=null)**
{
CurrentCards.First(n => n.Type == 13).Total = 1;
total = CurrentCards.Sum(n => n.Total);
return total;
}
return total;
}
i've tried several different things including changing the != null into > 0 however that throws an invalid argument error saying that > cannot be used with this statement. Is there any other way that i can determine if CurrentCards.First(n => n.Type == 13 && n.Total == 11) is true or false?
Any help or suggestions are greatly appreciated.
Thank You
Instead of First, use Any with the predicate.
while(total > 21 && CurrentCards.Any(n => n.Type == 13 && n.Total == 11))
The First method throws an exception if there are no matching element.
You need to call FirstOrDefault, which can return null.
You should try using FirstOrDefault instead of First.First will throw an error if no elements a returned.
Also check that CurrentCards is not empty. Both First and FirstOrDefault will give an ArguementNullException if the IEnumumerable is empty.
I want to check if two string are of the same length. I tried the following, but it doesn't work.
string passnew = "1233";
string passcnfrm = "1234";
if((passnew.Length&&passcnfrm.Length)>6 ||(passnew.Length&&passcnfrm.Length)<15)
{
// ...
}
Why does it not work? What do I need to change?
if(passnew.Length == passcnfrm.Length &&
passnew.Length > 6 && passnew.Length < 15)
{
// do stuff
}
You are missing some basic syntax lessons. What you write inside of these brackets are conditions. We have unary operators (operating on one thing), binary operators (two) and one tertiary operator (forget about that one).
You cannot construct something like your "boundary test" with those easily.
A possible way:
(passnew.Length > 6) && (passcnfrm.Length > 6)
But you aren't testing if the length is equal anyway, even if you could use a syntax like that. You seem to want to compare if both are longer than 6 chars and shorter than 15 chars. One at 7 and one at 14 would satisfy both conditions..
if(passnew.Length == passcnfrm.Length &&
(passnew.Length < 15 && passnew.Length > 6))
{
// ...
}
Checks both are same length, and either one is more than 6 and less than 15 characters long.
that would be:
if(passcnfrm.Length.Equals(passnew.Length))
{
//do stuff
}
A probably better way to do it is:
if (( passnew != null && passcnfrm != null )
( passnew == passcnfrm )
&& ( passnew.Length > 6 && passnew.Length < 15 ))
{
// do stuff
}
Hides the length check inside the equality check which you'll probably need, it isn't in your question but the variable names make it pretty clear you're doing a password change function there. I added the null check to make sure the length checks don't throw a NullReferenceException, not needed in the example because you assign both manually, but might save some trouble if you're going to convert this to a method later on.
You can use like this:
if (s.Contains(s1[i]))
or:
boolean equalsIgnoreCase(String anotherString);
or use this method:
public static int occurrence(string [] a, string a2)
{
int occ = 0;
for (int i = 0; i < a.Length;i++ )
{
if(a[i].Equals(a2))
{
occ++;
}
}
return occ;
}