[英]Cannot implicitly convert type 'string' to 'System.DateTime' - with t
As shown below, the error originates from what I believe is an error between translation over from Visual Basic to C#.如下所示,错误源于我认为从 Visual Basic 转换到 C# 之间的错误。 I don't know whether this is an incorrect/outdated linked with VB, or what 'string' I'm "converting" into the time values.我不知道这是否是与 VB 的不正确/过时链接,或者我正在“转换”为时间值的“字符串”。 Need assistance on whether I must change the the structure of the if-then statement.在我是否必须更改 if-then 语句的结构方面需要帮助。
using System;
using System.Media;
using System.Windows.Forms;
namespace NotificationDemo
{
public partial class NotificationDemo : Form
{
public NotificationDemo()
{
InitializeComponent();
}
// Variables //
public int hour;
public int minute;
// Messaging System //
public void Alert(string msg)
{
NotifyForm frmn = new NotifyForm();
frmn.showAlert(msg);
}
private void BtnNotify_Click(object sender, EventArgs e)
{
this.Alert("Default Message");
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Windows\Media\Alarm03.wav";
player.Play();
}
// Basic Schedule SysFunction
public string currenttime;
public string messagetime;
private void timer1_Tick(object sender, EventArgs e)
{
currenttime = DateTime.Now.ToString("hh:mm:ss tt");
label1.Text = currenttime;
}
private void timer2_Tick(object sender, EventArgs e)
{
messagetime = maskedTextBox1.Text + " " + comboBox1.Text;
if (currenttime == messagetime)
{
timer2.Stop();
this.Alert(textBox1.Text);
button1.Enabled = true;
button2.Enabled = false;
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Windows\Media\Alarm03.wav";
player.Play();
}
}
private void button1_Click(object sender, EventArgs e)
{
timer2.Start();
button1.Enabled = false;
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer2.Stop();
button1.Enabled = true;
button2.Enabled = false;
}
// Advanced Schedule SysFunction
public ProgramFunctionsDataContext doAction = new ProgramFunctionsDataContext();
private void NotificationDemo_Load(object sender, EventArgs e)
{
// // Basic Component
int hour = 1;
int minute = 1;
for (this.hour = hour; hour <= 12; hour++)
{
cbhour.Items.Add(hour);
}
for (this.minute = minute; minute <= 59; minute++)
{
cbminute.Items.Add(minute);
}
// Appointment Check
foreach (var app in doAction.AppointmentSets)
{
///LINE OF ERROR
if (app.AppointmentDate = DateTime.Today.ToLongDateString())
///LINE OF ERROR
{
}
}
}
}
}
This is due to you are using =
which is the operator to assign value这是因为您使用=
这是分配值的运算符
Instead, you need to use ==
to do the comparison for the if
statement相反,您需要使用==
对if
语句进行比较
UPDATED : As @Llama concerns that the previous solution will lead to casting error and I misinterpret the error message, so these 2 solutions can be used:更新:由于@Llama担心以前的解决方案会导致转换错误并且我误解了错误消息,因此可以使用以下两种解决方案:
Solution 1: Directly compare based on Date方案一:直接根据Date比较
if (app.AppointmentDate.Date == DateTime.Today.Date)
Solution 2: Compare Date in string
format with on ToLongDateString()
解决方案 2:将string
格式的日期与 on ToLongDateString()
进行比较
if (app.AppointmentDate.ToLongDateString() == DateTime.Today.ToLongDateString())
Solution 1 will be preferred as it is more simple and does exactly as Solution 2解决方案 1 将是首选,因为它更简单并且与解决方案 2 完全相同
While doing the comparison, be sure that both values used must be the same datatype and in Date
string format if you cast it to String
format.在进行比较时,如果将其转换为String
格式,请确保使用的两个值必须是相同的数据类型和Date
字符串格式。
Assuming you only want to compare the date component, you can rewrite your statement like this:假设您只想比较日期组件,您可以像这样重写您的语句:
if (app.AppointmentDate.Date == DateTime.Today)
.Date
will zero the time component, and in this case allows you to compare it to today's date. .Date
会将时间分量归零,在这种情况下,您可以将其与今天的日期进行比较。
Note the change from =
to ==
.注意从=
到==
的变化。 A single equals is an assignment, and a double equals is an equality comparison.单等号是赋值,双等号是等式比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.