How can I compare two string values for equality? [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 1 year ago.
Improve this question
I need to find the klas of 5TIF2 and dont now how to compare it to the class
private void BtnFind_Click(object sender, RoutedEventArgs e)
{
Leerling oleerling = new Leerling();
oleerling.MijnId = TxtId.Text;
oleerling.Voornaam = TxtVoornaam.Text;
oleerling.Naam = TxtNaam.Text;
oleerling.Klas = TxtKlas.Text;
if (oleerling.Klas = "5TIF2")
{
LstLeerlingen.Items.RemoveAt(2);
LstLeerlingen.Items.RemoveAt(3);
}
else
{
LstLeerlingen.Items.RemoveAt(0);
LstLeerlingen.Items.RemoveAt(1);
}
}

You need to use the operator == to check if a statement is true or false, instead you have used =, which is for assigning values.
You need:
if (oleerling.Klas == "5TIF2")

Related

Why IEqualityComparer doesn't work as expected? [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 months ago.
Improve this question
Why EqualityComparer MyEqComp doesn't work?
It should left in arr only 2 elements with equal Values - myDic["1"]and myDic["3"] with Value="456". But instead it consists of myDic["1"] and myDic["2"] but their Values are not equal.
Function GetHashCode returns the same hash code for 1st and 3rd elements!
public class Program
{
public static Dictionary<string,string> myDic = new Dictionary<string,string>();
public static void Main()
{
myDic.Add("1","456");
myDic.Add("2","567");
myDic.Add("3","456");
var arr=myDic.Distinct(new MyEqComp());
foreach (var n in arr)
{
Console.WriteLine("key="+n.Key+",value="+n.Value+"\n");
}
}
class MyEqComp:IEqualityComparer<KeyValuePair<string,string>>{
public bool Equals(KeyValuePair<string,string> pair1, KeyValuePair<string,string> pair2){
Console.WriteLine("pair1="+pair1+",pair2="+pair2);
return pair1.Value == pair2.Value;
}
public int GetHashCode(KeyValuePair<string,string> pair){
var code = pair.Value.GetHashCode();
Console.WriteLine("code="+code);
return code;
}
}
}
And the output
code=-121858068
key=1,value=456
code=437991364
key=2,value=567
code=-121858068
pair1=[1, 456],pair2=[3, 456]
Answer: Code is correct and works as expected.

C# using an IF statement to check if user enters correct DateTime date which is a birthday [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 1 year ago.
Improve this question
I have a Customer class, and I used the DatTime constructor and set the Date of Birth(bday).
In the main program I have an instance of the Customer class and would like to check if user enters the same birthday that was was originally set in the class.
public class Customer
{
public string Name;
public int PinNumber = 2321;
public double AccBalance = 250.00;
public DateTime bday = new DateTime(1875,05,22);
main program
person1.bday = Convert.ToDateTime(Console.ReadLine());
if (person1.bday == 1875,05,22)
{
Console.WriteLine("");
}
I get an error saying ''==' operator cannot be be applied to operands of type 'DateTime' and 'int'
You need to convert the three integers in your if statement to a DateTime:
if (person1.bday.equals(new DateTime(1875,05,22)) {
Console.WriteLine("");
}
When doing a comparison the data type on both end should be same
You should create a date object when doing comparison in your code
if (person1.bday == new DateTime(1875,5,22))
{
Console.WriteLine("");
}

C# Operator '+' cannot be applied to operand of type '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 1 year ago.
Improve this question
I have searched for the answer to this question. I found similar sorts of questions, but those do not resolve the issue I have.
The last line of code is where I get the error: Operator '+' cannot be applied to operand of type 'bool' Here is the code:
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
employee oEmployee = new employee();
oEmployee.LoginEvent += new LoginEventHandler(oEmployee_LoginEvent);
oEmployee.Login(txtName.Text, txtPassword.Text);
}
void oEmployee_LoginEvent(string loginName, bool status)
{
MessageBox.Show("Login Status: ", + status); Here is where the issue is
}
Because of the comma, you are printing two values, the second of which ( + status ) is a unary prefixed + operator applied to a bool value. There is no such operator, so the error.
You can get rid of the comma ("Login Status: " + status.ToString()) in which case the + is a concatenation operator, or you can get rid the plus "Login Status: ", status.ToString()) and print two strings.

Can't print Stack<T> C# [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 I have a question about my code. Everything should work without a problem, but when I try to print on the console, the stack gives me the following
input:
4
Output:
System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.
Stack1 [System.UInt64]
my question is if i need to add a new library because even with int it gives me the same.
using System;
using System.Collections.Generic;
namespace ConsoleApp29
{
class Program
{
static void Main(string[] args)
{
Stack<ulong> nm = new Stack<ulong>();
ulong p = ulong.Parse(Console.ReadLine());
for(ulong i = 0; i < p; i++)
{
nm.Push(i);
}
foreach(int i in nm)
{
Console.Write(nm);
}
}
}
}
nm, which you are printing, is the entire Stack object; you want to be printing i, the current element of nm.

"A namespace cannot directly contain members such as fields or methods" [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 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

Categories

Resources