Code:
public class Period<T>
{
public T Debut { get; set; }
public int Duration { get; set; }
public T End
{
get
{
if (typeof(T) == typeof(DateTime))
{
T fin = (T)CalculFin(Debut as DateTime?, Duration);
return fin;
}
else if (typeof(T) == typeof(int))
{
T fin = (T)CalculFin(Debut as int?, Duration);
return fin;
}
else
{
throw new Exception("Type not supported");
}
}
}
private object CalculFin(int? debut, int duration)
{
if (debut.HasValue)
{
return debut.Value + this.Duration;
}
else
{
return null;
}
}
private object CalculFin(DateTime? debut, int duration)
{
if (debut.HasValue)
{
return debut.Value.AddDays(this.Duration);
}
else
{
return null;
}
}
public Period(T debut,int duration)
{
this.Debut = debut;
this.Duration= duration;
}
}
Code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdOk_Click(object sender, EventArgs e)
{
CultureInfo Culture = new CultureInfo("en-US");
int intDeb = -1;
int duration = 0;
int.TryParse(txtDuration.Text, out duration);
DateTime dteDeb = new DateTime();
if (int.TryParse(txtDebut.Text, out intDeb))
{
Period<int> Period = new Period<int>(intDeb, duration);
txtFin.Text = Period.Fin.ToString();
}
else if (DateTime.TryParse(txtDebut.Text,out dteDeb)) {
Period<DateTime> Period = new Period<DateTime>(dteDeb, duration);
txtFin.Text = Period.Fin.ToString("d", Culture);
};
}
}
HTML Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Models._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Debut<asp:TextBox ID="txtDebut" runat="server"></asp:TextBox><br />
Duration<asp:TextBox ID="txtDuration" runat="server"></asp:TextBox><br />
fin<asp:TextBox ID="txtFin" runat="server" ReadOnly="true"></asp:TextBox><br />
<asp:Button ID="cmdOk" runat="server" Text="Button" onclick="cmdOk_Click" />
</form>
</body>
</html>
If you enter an int, that calculates the sum with the term if you enter a date that adds time in days.
Bookmarks