c# - 从字符串转换为日期时间,然后存储在列表 C#
问题描述
被困在这个任务上一段时间了,任何帮助都将不胜感激。
因此,我从表示层获得了患者 ID、员工 ID、visitType 和 DateTime 的用户输入,我想通过业务层将其添加到列表中。
dateTime 作为字符串输入,我可以将其存储为字符串,但我想做的是将其转换为 DateTime,然后能够将其存储在列表中。这是我遇到错误的地方。
这是我在表示层(MainWindow.cs)中的代码,我在其中提取要存储的信息;
private void BtnAddVisit_Click(object sender, RoutedEventArgs e)
{
txtOutput.Text = "";
try
{
if (healthSystem.addVisit(new int[2] { 1, 3 }, 1, visitTypes.assessment, "01/01/2020 09:00")) //Should be OK
txtOutput.Text += "Visit 1 added.\n";
}
catch (Exception ex)
{
txtOutput.Text += ex.Message;
}
txtOutput.Text += healthSystem.getVisitList();
}
这是我在业务层(HealthFacade)中的代码;
public Boolean addVisit(int[] staff, int patient, int type, string dateTime)
{
//if the number of objects in the visit list is equal to 6, clear the list to avoid repeated output in textbox
if (visit.Count == 6)
{
visit.Clear();
}
//converting from string to dateTime
for (int i = 0; i < visit.Count; i++)
{
//the original task was to store 6 instances so thats why is says < 7
if (visit.Count < 7)
{
DateTime oDate = Convert.ToDateTime(dateTime);
}
}
//adding all the visits, regardless of if the data is valid or not
Visit v = new Visit();
v.Staff = staff;
v.Patient = patient;
v.Type = type;
v.DateTime = oDate;
//adds instance of visit to the visit list
visit.Add(v);
return true;
我的理解是我会写 v.DateTime = oDate; 但它告诉我“oDate”在当前上下文中不存在。
我的访问课程的代码在这里;
using System;
using System.Collections.Generic;
using System.Text;
namespace BusinessLayer
{
class Visit
{
int[] staff;
int patient, type;
string dateTime;
public Visit()
{
}
// Constructor for Staff, using example from week 5 practical
public Visit(int [] aStaff, int aPatient, int aType, string aDateTime)
{
staff = aStaff;
patient = aPatient;
type = aType;
dateTime = aDateTime;
}
public int[] Staff
{
set { staff = value; }
get { return staff; }
}
public int Patient
{
set { patient = value; }
get { return patient; }
}
public int Type
{
set { type = value; }
get { return type; }
}
public string DateTime
{
set { dateTime = value; }
get { return dateTime; }
}
}
}
我尝试这样做的原因是,我可以建立一个医生预约系统,并确保没有两个预约同时进行,因此会发生冲突。
提前感谢您提供的任何帮助!
解决方案
这里的问题是变量 oDate 仅存在于您声明它的范围内。在您的情况下,它只能在您的 if 语句中使用。
您的函数应如下所示,以便您在需要时访问变量:
public Boolean addVisit(int[] staff, int patient, int type, string dateTime)
{
//if the number of objects in the visit list is equal to 6, clear the list to avoid repeated output in textbox
if (visit.Count == 6)
{
visit.Clear();
}
DateTime oDate = Convert.ToDateTime(dateTime);;
//converting from string to dateTime
for (int i = 0; i < visit.Count; i++)
{
//the original task was to store 6 instances so thats why is says < 7
if (visit.Count < 7)
{
//DateTime oDate = Convert.ToDateTime(dateTime);
// Because you definded oDate inside your if clause it is only accessible inside the clause
}
}
//adding all the visits, regardless of if the data is valid or not
Visit v = new Visit();
v.Staff = staff;
v.Patient = patient;
v.Type = type;
v.DateTime = oDate;
//adds instance of visit to the visit list
visit.Add(v);
return true;
推荐阅读
- notepad++ - 找到所有 ”);” 在 "CREATE UNIQUE INDEX IX" 之后将其更改为 ") OPTIONS;"
- python - Selenium 与 Firefox:FirefoxProfile 在第二次使用时停止工作
- wordpress - 特定部分奇怪行为的自定义查询
- flask - login_user() Nonetype 对象没有属性 is_active 和 flask_login
- cytoscape.js - 如何使用 Cytoscape 从命令行打开 M1 Mac 中的点文件?
- angular - 过滤json对象数组
- python - 如何展平df pandas中的行
- graphql - 使用 amplify 和 graphql,更新表中的数组字段会将更新数组附加到现有表数组而不是替换它
- date - 如何使用 amcharts4 在图中获取日期格式 M/d/yyyy?
- javascript - JS Regex 匹配来自字符串的加拿大邮政编码