C
C#2y ago
Elmishh

✅ comparing variable to a number

im very very very new to C# and im trying to recode some basic things i made in python but i cant seem to figure out how to compare a variable like B to a number like 60 im trying to do if (b) == 60; but its giving me: Invalid expression term '=='
71 Replies
Elmishh
ElmishhOP2y ago
how do i compare them if its not ==? or am i doing something wrong
Angius
Angius2y ago
if (b == 60)
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
var b = 60;
if (b == 60)
{
Console.WriteLine("Yes");
}
var b = 60;
if (b == 60)
{
Console.WriteLine("Yes");
}
Console Output
Yes
Yes
Compile: 593.712ms | Execution: 84.465ms | React with ❌ to remove this embed.
Elmishh
ElmishhOP2y ago
also mind if i ask how i make C# wait 1 second?
Angius
Angius2y ago
await Task.Delay(1000) for example Just keep in mind, that you can await only inside of async methods
Elmishh
ElmishhOP2y ago
im confused
Thinker
Thinker2y ago
Or Thread.Sleep(1000);, but this is slightly worse than what ZZZ suggested
Angius
Angius2y ago
class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Wait for me!");
await Task.Delay(5000);
Console.WriteLine("Aight let's go");
}
}
class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Wait for me!");
await Task.Delay(5000);
Console.WriteLine("Aight let's go");
}
}
Notice how the void had to change into async Task
Elmishh
ElmishhOP2y ago
so after if ([var] == 60) how do i make the next few things ONLY happen if thats true and stop happening the moment it stops becoming true (inside of a while (true) since currently it keeps on going
Angius
Angius2y ago
Break out of the loop
Elmishh
ElmishhOP2y ago
is supposed to do [var] -= 60 when i reaches 60 wdym?
Angius
Angius2y ago
Or use a flag boolean instead of hardcoding true in the loop Literally break;
Elmishh
ElmishhOP2y ago
no that would stop everything entirely i want it to loop every time it reaches 60 to go back to 0
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
while (true)
{
Console.WriteLine("Free me!");
if (Random.Shared.Next(0, 10) > 5)
{
break;
}
}
Console.WriteLine("I'm free!");
while (true)
{
Console.WriteLine("Free me!");
if (Random.Shared.Next(0, 10) > 5)
{
break;
}
}
Console.WriteLine("I'm free!");
Console Output
Free me!
Free me!
I'm free!
Free me!
Free me!
I'm free!
Compile: 535.929ms | Execution: 77.994ms | React with ❌ to remove this embed.
Elmishh
ElmishhOP2y ago
using System; ` public class Clock { public static void Main(string[] args) { int second = 0; int minute = 0; bool b = Convert.ToBoolean(second); while (true) { second += 1; Thread.Sleep(1000); if (second == 60) { second -= 0; minute += 1; Console.WriteLine(second) ; } } } } probably a million and a half issues with it but anyways how do i get it so if (second == 60)` will make second reset back to 0 and minute go up by one
Angius
Angius2y ago
Set seconds to 0...?
Elmishh
ElmishhOP2y ago
yeah
Angius
Angius2y ago
You're subtracting 0 from seconds now Instead, set it to 0
Elmishh
ElmishhOP2y ago
oh
Angius
Angius2y ago
a -= b is a = a - b
Elmishh
ElmishhOP2y ago
well nothing works now idk why im confused and tired why did it stop working using System; public class Clock { public static void Main(string[] args) { int second = 0; int minute = 0; while (true) { second += 1; Thread.Sleep(1000); if (second == 60) { second -= 60; minute += 1; Console.WriteLine(second); } } } }
Angius
Angius2y ago
Works for me ¯\_(ツ)_/¯
Elmishh
ElmishhOP2y ago
wait how did you is doing {second} how you display an int as an str
Angius
Angius2y ago
Resets just fine too
Angius
Angius2y ago
String interpolation
Elmishh
ElmishhOP2y ago
well your code is much different
Angius
Angius2y ago
$interpolation
MODiX
MODiX2y ago
String interpolation is the preferred way of building strings in C#. It is easier to read than concatenation. For example:
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
can be written as:
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
Angius
Angius2y ago
using System.Threading;

var minutes = 0;
var seconds = 0;
while (true)
{
Thread.Sleep(1000);
seconds++;
if (seconds >= 60)
{
seconds = 0;
minutes++;
}
Console.WriteLine($"{minutes} minutes and {seconds} seconds elapsed"); }
using System.Threading;

var minutes = 0;
var seconds = 0;
while (true)
{
Thread.Sleep(1000);
seconds++;
if (seconds >= 60)
{
seconds = 0;
minutes++;
}
Console.WriteLine($"{minutes} minutes and {seconds} seconds elapsed"); }
My code, for reference And it mostly does the same things a++ is the same as a += 1
Elmishh
ElmishhOP2y ago
do i not need all of the public class and public static void stuff?
Angius
Angius2y ago
Nowadays, no But you can just put that code inside of the Main() and it'll work
Elmishh
ElmishhOP2y ago
wdym ???
Angius
Angius2y ago
I mean exactly what I said
Elmishh
ElmishhOP2y ago
wdym putting it inside of main()
Angius
Angius2y ago
class Program
{
public static void Main()
{
// put it here
}
}
class Program
{
public static void Main()
{
// put it here
}
}
Elmishh
ElmishhOP2y ago
using System.Threading; public class Clock { public static void Main() { var second = 0; var minute = 0; while (true) { Thread.Sleep(1000); second += 1; if (second == 60) { second = 60; minute += 1; Console.WriteLine($"{minute}:{second}"); } } } }
Angius
Angius2y ago
second = 60; why "If seconds reach 60, set the seconds to 60"?
Elmishh
ElmishhOP2y ago
forgot to remove the 6 i wanted to go from -= to = but i forgot to remove the 6 too
Angius
Angius2y ago
Gotcha
Elmishh
ElmishhOP2y ago
wouldnt matter as it doesnt even give me any output in the first palce
Angius
Angius2y ago
That's weird, it should work perfectly fine Do you have any other code in your project?
Elmishh
ElmishhOP2y ago
nope
Angius
Angius2y ago
That's weird then, because it should work perfectly
Elmishh
ElmishhOP2y ago
if you copy mine does it work error CS0103: The name 'Console' does not exist in the current context
Angius
Angius2y ago
using System
Elmishh
ElmishhOP2y ago
?
Angius
Angius2y ago
Apparently you're using an older version of .NET that doesn't have implicit usings Console is part of the System namespace
Elmishh
ElmishhOP2y ago
ok
Angius
Angius2y ago
The way using System.Threading makes Thread class available, using System will make Console available
Elmishh
ElmishhOP2y ago
so i cant have both threading and system?
Angius
Angius2y ago
You can Just add another using Also, I see what's wrong with your code Since Console.WriteLine() is inside of the if, not outside like in my code, you'll get output only on full minutes
Angius
Angius2y ago
Moving it works just fine
Elmishh
ElmishhOP2y ago
so: using System; using Threading;
Angius
Angius2y ago
y
Elmishh
ElmishhOP2y ago
i clicked enter and not shift inter enter is that what you mean by using another using
Angius
Angius2y ago
Yes
Elmishh
ElmishhOP2y ago
slight problem
TheRanger
TheRanger2y ago
using System.Threading;
Elmishh
ElmishhOP2y ago
ah ok at last it works time to add hours
TheRanger
TheRanger2y ago
is there any reason ur coding that class?
Elmishh
ElmishhOP2y ago
?
TheRanger
TheRanger2y ago
there are built in classes that handle the time way easier
Elmishh
ElmishhOP2y ago
wdym
TheRanger
TheRanger2y ago
i mean u dont need to make this class, C# already has classes that counts the time
Elmishh
ElmishhOP2y ago
well im trying to learn C# and thats the only thing i could think of
TheRanger
TheRanger2y ago
ah ok
Elmishh
ElmishhOP2y ago
how do i close !close
Accord
Accord2y ago
Closed!
Elmishh
ElmishhOP2y ago
that worked but why warning
TheRanger
TheRanger2y ago
its slash close /close
Elmishh
ElmishhOP2y ago
ah
Want results from more Discord servers?
Add your server