如果使用“System.Convert.ToInt32”的else语句不起作用

时间:2018-03-07 04:33:28

标签: c# .net string if-statement int32

我正在尝试使用基于日期/时间对象值的if else语句显示特定字符​​串。

我正在使用一个公式编辑器来检查语法,但我没有遇到任何问题,但是当在应用程序中解析公式时,我会得到满足条件的空白。

这是我的公式:

if (System.Convert.ToInt32(rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value)>0)
{
 return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value) + '\n' + "Deceased: " + System.String.Format("{0:dd/MM/yyyy}",rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value);
}
else
{
  return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value);
 }

公式编辑器允许转换为不同的数据类型,我尝试修改公式,返回值为布尔值,但这也不起作用。

由于

3 个答案:

答案 0 :(得分:4)

答案 1 :(得分:0)

这是有用的:

// ListFields();

试 { if(System.Convert.ToInt32(System.String.Format(" {0:yyyy}",rpt.Fields [" AppointmentResourceLink.Appointment.Patient.DateDied"]。Value) )→1) 返回"已故日期:" + System.String.Format(" {0:dd / MM / yyyy}",rpt.Fields [" AppointmentResourceLink.Appointment.Patient.DateDied"]。Value); }

catch(System.Exception e)

{ System.String.Format(" {0:DD / MM / YYYY}",rpt.Fields [" AppointmentResourceLink.Appointment.Patient.DateDied"]值);
}

返回"";

答案 2 :(得分:-1)

请尝试使用此代码,这将符合您的目的,并根据需要为您提供输出。

try
{
    string s = rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value;

    // Please replace your datetime format in ParseExact
    DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
}
catch
{
    return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value);
}


if (dt != DateTime.MinValue)
{
    return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value) + '\n' + System.String.Format("font-style:Bold", "Deceased: ") + System.String.Format("{0:dd/MM/yyyy}",rpt.Fields["AppointmentResourceLink.Appointment.Patient.DateDied"].Value);
}
else
{
    return System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.FirstName"].Value) + " " + System.Convert.ToString(rpt.Fields["AppointmentResourceLink.Appointment.Patient.LastName"].Value);
}