Results 1 to 4 of 4

Thread: Class <T> adapt a method based on T

  1. #1
    Join Date
    Jun 2009
    Posts
    1,518

    Class <T> adapt a method based on T

    I work on a component that allows you to define periods. The input parameters are:

    Property Debut (either an int or a DateTime: T)
    Property Duration always an int

    Output was a property End (either an int or a DateTime: T)

    where is my generic type int

    End = Start + Duration
    in the case of DateTime
    End = Debut.AddDays (Duration)

    I'm trying to wire up my end of public ownership and that's where I'm running out of gray matter

    if I
    Code:
    public T End {
                               get {
                                        if ( typeof ( T ) == typeof ( DateTime ) ) {
                                                 //End = ((DateTime)Debut).AddDays(Duration); NOT WORK
                                        }
                                   ;}
             }
    It is possible to access the methods of proceeding according to type?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Class <T> adapt a method based on T

    You can make a class of non-generic base that does that as integers, and the client code retrieve a DateTime structure from its numerical representation:

    Code:
    // multiplied by 1000 (or more), imagining that the date is stored in units / / 1000 Ticks, because of the small size of int versus long
     var myDate = DateTime. FromFileTime ((long) myInstance. End * 1000));
    Or, in the same spirit, create a base class that exposes non-generic property end of int, and a method to override to perform the addition of the term.

    Or even two different classes. For any event, what shocks me a bit is to have a generic class which accepts only two types conceptually, while technically you can not restrict the type parameters in this way (at best constrain struct, or interface type IComparable IEquatable etc. ...).

    Your example is illustrative: when you find yourself testing the value of T in generic class is that it is a design problem (I think).

  3. #3
    Join Date
    Jun 2009
    Posts
    1,518

    Re: Class <T> adapt a method based on T

    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.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Class <T> adapt a method based on T

    If you test the generic to the specific function of this type, you lose the interest of generic. In addition what happens if I create a <MySuperClassNotPlanned> Period? You better make 2 separate classes, it would be clearer and safer

Similar Threads

  1. How to call a method in another class in Java
    By kannudhal in forum Software Development
    Replies: 4
    Last Post: 28-10-2010, 09:29 AM
  2. Static method in Abstract class
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:22 AM
  3. Method or Class initialization
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 08-03-2010, 12:58 PM
  4. Set a class with method get()
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 01:12 AM
  5. Abstract class method overloading problem
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 10:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,689,468.73773 seconds with 16 queries