Trying to convert DateTime into int [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 last year.
Improve this question
so i have been trying to use both date time and int in the same if statement.
my code is like this:
public int wantedHours, wantedMinutes;
public int sysHour = System.DateTime.Now.Hour;
public int sysMinutes = System.DateTime.Now.Minute;
void Update()
{
if (sysHour == wantedHours && sysMinutes == sysMinutes)
{
sendTheNotif == true;
} else
{
sendTheNotif == false;
}
}
but it doesnt work. unity is giving me this error:
error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Integers are set correctly.
You're using the equality operator in your if statements which is throwing the error
Suggest reading the microsoft doc
Try this
(note, you are checking equality between sysMinutes == sysMinutes in your if statement, is that what you're wanting to do?)
public int wantedHours, wantedMinutes;
public int sysHour = System.DateTime.Now.Hour;
public int sysMinutes = System.DateTime.Now.Minute;
void Update()
{
if (sysHour == wantedHours && sysMinutes == sysMinutes)
{
sendTheNotif = true;
} else
{
sendTheNotif = false;
}
}

In your code, you did not assign a value to wantedHours, and wantedMinutes.
also sysMinutes will always equal sysMinutes if there is no option to change its value in your code before comparison.
if (sysHour == wantedHours && sysMinutes == sysMinutes)
is the major problem. When you are comparing variables with == it means it should evaluate true if it is equal in type and value.
So wantedHours has no value assigned according to your code block above.
And sysMinutes is always equal to sysMinutes.

you have to use wantedTime instead
var sysTime = System.DateTime.Now;
If( (int)wantedTime.Subtract(sysTime).TotalMinutes ==0) sendTheNotif = true;
else sendTheNotif = false;

Related

error CS5001: Program `main.exe' does not contain a static `Main' method suitable for an entry point [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
using System;
class Program {
static string Main(){
string Input = Console.ReadLine();
return Input;
}
static int Health () {
int Health = 100;
return Health;
}
static int EnemyHealth(){
int EnemyHealth = 100;
return EnemyHealth;
}
static int y(){
int y = 0;
if (Main() == "d"){
y++;
if (y == 11){
y=10;
}
}
if (Main() == "a"){
y--;
if (y == -1){
y=0;
}
}
return y;
}
static int x(){
int x = 0;
if (Main() == "w"){
x--;
if (x == -1){
x=0;
}
}
if (Main() == "s"){
x++;
if (x == 11){
x=10;
}
}
return x;
}
}
I am trying to make a game that moves your character on repl with y and x cordinates but i get this error error CS5001: Program main.exe' does not contain a static Main' method suitable for an entry point. Btw this was made in replit. Please help!!!!!!
The Main() method has an expected signature (its name, arguments, and return type ect..). In your case you're using static string Main() which is not an allowable signature yet.
Per MSDN pertaining the Main() signature
Main can either have a void, int, or, starting with C# 7.1, Task, or Task return type.
If and only if Main returns a Task or Task, the declaration of Main may include the async modifier. Note that this specifically excludes an async void Main method.

FizzBuzz test case pass [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am wondering can someone please tell me where I am going wrong with this FizzBuzz.
I get an error "not all code returns a value and struggling to find out exactly how to fix it.
My code is below:
for (int i = 0; i < input; i++)
{
if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else
{
Console.WriteLine(i);
}
}
And the test case is this:
[Test]
public void Test1()
{
var solution = new Solution();
Assert.AreEqual(solution.PrintFizzBuzz(15), "FizzBuzz");
}
First of like other people have said, not all the code is present so it's difficult for us to help. But I'll try anyway.
When testing with Assert.AreEqual the function PrintFizzBuzz should return something, in this case the string "FizzBuzz". But if you are using the Console.WriteLine method it will return nothing. The Console.Writeline method will print the string to the console, but the console is not actually the 'program', it's just for visualising some things like logging or debugging.
So to fix your issue you should use return instead of Console.Writeline and let your method PrintFizzBuzz return a string instead of voiding and printing it to the console. It's probably also better to rename your method in this case, because it doesn't print FizzBuzz anymore.
Your code has also another issue and that's when you input 15 it will print out "Fizz", because the check you do is modulo 3 and 15 % 3 = 0. You should order your check the otherway around, from specific to less specific.
An example for returning a string would be:
string SomeMethod()
{
return "Some String"
}
not all code returns a value
Means your method declared having a return type and not void but you are not returning anything from your method. One example case:
int Add(int a, int b)
{
int c = a + b;
// no return specified would through the same error
}

trouble with if statement and the > operator [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'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.

Need approach for Setting value to a property based on numerous conditions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have many properties spread across a number of classes. Values to these properties need to be assigned based on a number of conditions (around 5 to 8 for each property). I am looking for an alternative for numerous 'if else' conditions.
I have come across 'Rule Engine' off late but AFAIK it can be used for validating the rules.
Any design suggestion would be of great help.
I'm not sure whether this is a "better" solution for you, but I'll try to explain.
Value to these properties need to be assigned based on a number of conditions (around 5 to 8 for each property).
I think you mean that you always need to write this, which is annoying:
if (condition1 && condition2 && condition3 && condition4 && condition5) {
Property1 = Value1;
}
if (condition1 && condition2 && condition3 && condition4 && condition5) {
Property2 = Value2;
}
// ...
I think maybe this method can solve your problem?
public static void SetValueForPropertyIf<T>(Predicate<object>[] conditions, ref T property, T value) {
foreach (var predicate in conditions) {
if (!predicate(null)) {
return;
}
}
property = value;
}
And you can just call the method with a list of lambda expressions, ignore the argument (because it's always null), a variable to be passed by reference, and a value to set if all the conditions are met.
However, this only works for variables because I'm pretty sure properties cannot be passed by reference (with the ref keyword). So you have to declare your properties like this:
private int someVariable;
public int SomeVariable {
get {return someVariable;}
set {someVariable = value;}
}
And if you don't like the parameter of the Predicate delegate not being used, define your own delegate!
public delegate bool MyDelegate();
Here is an example of how to use this method, in case you didn't understand what I meant.
class MyClass {
private int someVariable;
public int SomeVariable {
get {return someVariable;}
set {someVariable = value;}
}
public MyClass() {
someVariable = 10;
MyDelegate[] conditions = {
(() => 7 < 10),
(() => 77 == 77),
(() => "Sweeper is awesome".Contains("Sweeper")),
(() => String.IsNullOrEmpty(""))
};
SetValueForPropertyIf(conditions, ref someVariable, 20);
}
}
In this class's constructor, I first created some conditions, which are all true. Then I call the method with these conditions. Note that I used someVariable (The field) instead of SomeVariable (The property) as the ref parameter.
And then you can print SomeVariable:
MyClass mc = new MyClass();
Console.WriteLine(mc.SomeVariable);
The output is 20. Hooray!

A Beginner Trying to Use Else Statements 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 8 years ago.
Improve this question
I'm a student sitting a course in computing for the very first time and I've been trying to 'crack' this one for ages (I'm very new to this... sorry!). I can't seem to build a simple if/else statement in which if the conditions are met, it writes a positive response, and if not, a negative response. But I'm getting the errors:
Invalid expression term 'else'
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Sorry if this makes no sense, I'm very confused! :(
static void Main(string[] args)
{
Console.WriteLine("Do you like bananas?");
Console.ReadLine();
if (Console.ReadLine() == "Yes") ;
{
Console.WriteLine("Thanks for your time!");
}
else;
{
Console.WriteLine("Oh okay.");
}
}
}
}
you need to remove the semicolon that you have after the else.
generally its
if (condition)
{
//code
}
else
{
//code
}
and if you have more than one condition to meet you can have
if (condition)
{
//code
}
else if (condition 2)
{
//code
}
else
{
//code
}
the last else is used if none of the conditions are met above, its like a default behavior
Further, you don't need braces for only one line of code
if (condition) DoInterestingStuff();
else DoNotDoInterestingStuff();
A few example conditions(with unnessesary if-statements for clarification):
if (bananas == true)
if (bananas) // same as above
if (bananas != false) // not equal to false, thus true
if (totalBananas >= 10)
No semi-colons:
if (Console.ReadLine() == "Yes")
{
Console.WriteLine("Thanks for your time!");
}
else
{
Console.WriteLine("Oh okay.");
}
Conditional Logic though is simple, basic, it is incredibly important to learn correctly. Your forgetting quite a bit of items:
Incorrect placement of ;.
You did:
if(Console.ReadLine() == "yes");
else;
The ; at the end of that particular portion of the statement is invalid. If your using Visual Studio or another IDE don't fight the tools, they try to help you with issues such as this. For instance a squiggly line should appear under the ; in that statement.
Your if else should be formatted in this manner:
if(value == "Yes")
{
Console.WriteLine("Thank you...");
}
else if (value == "No")
{
Console.WriteLine("No!");
}
else
{
Console.WriteLine("Ah...");
}
You'll want to familiarize yourself with the if else and switch statement early on. They're key piece in the foundation. You can find some more detail on the Microsoft Developer Network. Really good examples an explination of the entire process.

Categories