trouble with if statement and the > operator [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to write an if statement and i'm having trouble with my variables. it states operator > can not be applied to type int and string. code located below. both variables are displaying a int.
if (e.CmsData.Skill.InQueueInRing > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
}
else if (e.CmsData.Skill.AgentsAvailable > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Green);
}
else
{
callsWaitingData.Text = "0";
callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
}

This error couldn't really get much more descriptive.
operator > can not be applied to type int and string
if (e.CmsData.Skill.InQueueInRing > "0")
int -----^ ^--- string
Change it to
if (e.CmsData.Skill.InQueueInRing > 0)
Then both sides of the boolean logic is an int.

Related

using strings inside if statement throw error "Operator '||' cannot be applied to operands string and string" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi my c# is not what it used to be and have just come back after using java script for a while.
Essentially I am just trying to do a simple if statement using index of arrays but I receive the error message.
"Operator '||' cannot be applied to operands string and string"
How come this is not allowed as it essentially becoming a bool.
string[] userCustomAnswerArray = {"It needs to be reaplaced", "This could be improved", "I struggle to see this"};
int customResponseindex = rand.Next(0, 3);
string[] questionResponseArray = { "Yes", "No but not a problem", userCustomAnswerArray[customResponseindex] };
int questionResponseIndex = rand.Next(0, 3);
string userAnswer = questionResponseArray[questionResponseIndex];
if (userAnswer = questionResponseArray[0] || userAnswer = questionResponseArray[1])
{
}
Thanks for your help !!!!
userAnswer = questionResponseArray[0] is incorrect.
= is the assignment operator while == is the equality operator

C# If-else Compile Error } [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
i just started out with C# and im confused by a Compile Error.
This is the Code:
namespace Control_Flow_1
{
class Program
{
static void Main(string[] args)
{
string UserInput;
int Number;
Console.WriteLine("Bitte geben sie eine Nummer von 1-10 ein");
UserInput = Console.ReadLine();
Number = Convert.ToInt32(UserInput);
if (Number >= 1 && Number <= 10);
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
}
}
}
The } after the Console.WriteLine; seems to do the Compile Error for some Reason, how to fix it ?
Compile Error:
if (Number >= 1 && Number <= 10); get rid of the semi colon.
With the semi colon after the if you have
if ()
a block of code
else (with no corresponding if)
a block of code
The semi colon ends the if statement and so you have a "floating" else with no previous if

How can I CAST a variable that's passed into a method? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I had this code repeated many times:
if (db2.Query<int>("SELECT 1 FROM CARDCHOICE WHERE CC = ?", (int)CC.JFBP1).Count == 0)
{
var temp10 = Enumerable.Range(0, 10).Select(i => new CardChoice { Cc = (int)CC.JFBP1, Number = i });
db2.InsertAll(temp10);
}
I tried to put this into a method that I called like this
InsertCC(CC.JFBP1, 10);
Here is the method
private static void InsertCC(CC cc, int qty )
{
var choice = int(cc);
if (db2.Query<int>("SELECT 1 FROM CARDCHOICE WHERE CC = ?", choice).Count == 0)
{
var temp = Enumerable.Range(0, qty).Select(i => new CardChoice { Cc = choice, Number = i });
db2.InsertAll(temp);
}
}
However what happens is that it tells me I cannot CAST the cc in the method with (int) and gives me "Error expression term int"
Can someone give me some advice as to how I could cast the cc that's passed in? I realize I could do the casting in the method call but would prefer not to do that as I have a lot of those calls.
This code isn't cast var choice = int(cc);
use var choice = (int)cc;
but you don't using cc parameter inside the method, so just pass int
private static void InsertCC(int choice, int qty )
{
if (db2.Query<int>("SELECT 1 FROM CARDCHOICE WHERE CC = ?", choice).Count == 0)
{
var temp = Enumerable.Range(0, qty).Select(i => new CardChoice { Cc = choice, Number = i });
db2.InsertAll(temp);
}
}
then your call will work
InsertCC(CC.JFBP1, 10);

Unity error CS0029: Cannot implicitly convert type `bool' to `int' but it's bool [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have error
CS0029: Cannot implicitly convert type bool to int.
My declaration:
public bool isBig = false;
If statement with error:
if (player.GetHP() < 6 && player.isBig == false)
I don't understand this. I have also change this bool to return and checked few solutions:
player.GetBig() == false/0 / (player.GetBig()) == false/0 / !(player.GetBig())
but nothing works...
// Edit
public int GetBig()
{ // isBig is bool
return this.isBig;
}
public int GetHP()
{ // HP is int
return this.HP;
}
Simple typo.
public int GetBig()
should be
public bool GetBig()

Condensing this logic with linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I convert the following logic to LINQ?
Flaggedlist is a type of List<string>. request.flagged is a numeric value in a request POCO.
if (request.Flagged == 1)
{
if (!patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
}
else if (request.Flagged == 0)
{
string usrid = flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>();
flaggedList.Remove(usrid);
}
It can be boiled down to two simple IF statements and the 2nd part shortened further still:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
flaggedList.Add(request.UserId);
if (request.Flagged == 0)
flaggedList.Remove(flaggedList.FirstOrDefault(a => a == request.UserId));
This is pretty condensed and clear, not sure why you'd want to make it more condensed.
If you really want to make it shorter, maybe you can do this:
if (request.Flagged == 1 && !patient.UserFlaggedList.Contains(request.UserId))
{
flaggedList.Add(request.UserId);
}
else if (request.Flagged == 0)
{
flaggedList.Remove(flaggedList.Where(a => a == request.UserId).FirstOrDefault<string>());
}

Categories