Click here to Skip to main content
15,895,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a struct DateTime. I want to make a new class named myDateOfBirth. I want to add some functionality in myDateOfBirth class like isTodayBirthDay, WeakDayOfBirthDay etc., but functionalities of DateTime should also be accecible from myDateOfBirth.

some solutions I got but havings flaws.....
1. we can have Has-A relationship between myDateOfBirth and DateTime. But in that way we cann't access functions of DateTime directly by myDateOfBirth or object of it.

2. Use extension mathods.But applying extension mathods will add functionality to DateTime only, and that we cann't import in myDateOfBirth.
Posted

1 solution

You're wrong about extension methods accessibility, but beyond that, you should write a class that provides the functionality you need.

public class Birthday
{
    public DateTime MyBirthday { get; set; }

    public Birthday(DateTime date)
    {
        this.MyBirthday = date;
    }
    
    public bool IsBirthday(DateTime date)
    {
        return (date.Day   == MyBirthday.Day && 
                date.Month == MyBirthday.Month);
    }

    public Weekday GetBirthdayWeekday()
    {
        return MyBirthday.DayOfWeek;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900