【问题标题】:Cannot implicitly convert type 'string' to 'System.DateTime' - with t无法将类型 'string' 隐式转换为 'System.DateTime' - 使用 t
【发布时间】:2026-02-12 21:55:01
【问题描述】:

如下所示,错误源于我认为从 Visual Basic 转换到 C# 之间的错误。我不知道这是否是与 VB 链接的不正确/过时的链接,或者我正在“转换”为时间值的“字符串”。在我是否必须更改 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
                {
                    
                }
            }
        }
    }
}

【问题讨论】:

  • 我猜 app.AppointmentDate 是一个字符串值。除非您有 VB Option Strict On,否则 VB 会将值隐式转换为相同的数据类型。因此,将字符串与 DateTime 进行比较,VB 将在进行比较之前将字符串转换为 DateTime。似乎您的 AppointmentDate 字符串包含一个无法转换为正确 DateTIme 的值。 docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/…docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/…
  • 这有点难以理解。你有app.AppointmentDate,我猜是DateTime。但我对app 的了解是,当您枚举doAction.AppointmentSets 时,您会得到它。查看doAction 表明它是ProgramFunctionsDataContext,但这是未定义的。再说一次,你有if (app.AppointmentDate = DateTime.Today.ToLongDateString())。使用一个等号,这是if 中的一个赋值。您的意思是进行比较 (==)?

标签: c# vb.net vb.net-to-c#


【解决方案1】:

这是因为您使用的是=,这是分配值的运算符

相反,您需要使用==if 语句进行比较

更新:由于@Llama 担心以前的解决方案会导致转换错误并且我误解了错误消息,因此可以使用以下两种解决方案:

方案一:直接根据日期比较

if (app.AppointmentDate.Date == DateTime.Today.Date)

解决方案 2:比较 string 格式的日期和 ToLongDateString() 格式的日期

if (app.AppointmentDate.ToLongDateString() == DateTime.Today.ToLongDateString())

解决方案 1 将是首选,因为它更简单并且与解决方案 2 完全相同

在进行比较时,如果您将其转换为 String 格式,请确保使用的两个值必须是相同的数据类型并且是 Date 字符串格式

【讨论】:

  • 这一定是错的。尽管有=== 的错误,但提及DateTimestring 的错误不会发生,除非其中一种类型是DateTime。由于我们将DateTime 转换为string,因此AppointmentDate 一定是DateTime。即使比较编译,它也行不通。
  • OP 会得到的新错误是这样的:"Operator '==' cannot be applied to 'DateTime' and 'string'类型的操作数"
  • 我们从错误信息中知道app.AppointmentDate的类型是什么。如果= 的两边都是string,我们会收到string 无法转换为bool 的错误消息。 Example。它告诉我们string 不能转换为DateTime 的事实告诉我们AppointmentDateDateTime
  • 好的,请注意,将再次为我的答案重新更正。谢谢。
【解决方案2】:

假设您只想比较日期组件,您可以像这样重写您的语句:

if (app.AppointmentDate.Date == DateTime.Today)

.Date 会将时间分量归零,在这种情况下,您可以将其与今天的日期进行比较。

注意从=== 的变化。单等号是赋值,双等号是相等比较。

【讨论】:

    最近更新 更多