【发布时间】: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#