How to pass datetime parameter to a condition method? - c#

I'm studying object oriented programming. I have a class Dob, it reads the date of birth from main. If the date of birth from main was from 10 years ago until now, it says "error", else "verify".
Here is my class
public DateTime _Dob;
private DateTime dob {
get {
return _Dob;
}
set {
_Dob = value;
}
}
public Student(DateTime dob_) {
_Dob = dob;
}
public void checkdob(DateTime dob) {
DateTime local = DateTime.Now;
if (dob.Year >= local.Year - 10)) {
Console.WriteLine("error");
} else {
Console.WriteLine("verify");
}
Now I want to pass the birth date from main but I don't know how to do that. Here is what I have in main (It has errors and I don't know how to solve it):
Student dob = new Student(new DateTime(23/02/2010));
dob.checkdob( 02/12/2010);
Console.ReadLine();

First and foremost, you have the property just the wrong way around. The idea is that you expose the value of a private field through a public property, not the other way around:
private DateTime _dob;
public DateTime Dob
{
get { return _dob; }
set { _dob = value; }
}
But if you do not use the public property anyway, don't expose it.
Now, it seems strange to pass the date to the constructor of your class, and then pass it again when you want to check it. Use the value you already have.
public void CheckDdob()
{
if (_dob.Year >= DateTime.Now.Year -10))
{
Console.WriteLine("error");
}
else
{
Console.WriteLine("verify");
}
}
Then, to create a new DateTime for your constructor, just use the following:
Student dob = new Student(new DateTime(2010, 02, 23));
And finally, you may want to review your logic, because you do not actually check correctly if a date is more than 10 years ago. Then again, what you have may fit your requirements.

You will need to use a proper constructor parameter for DateTime. There are plenty of them listed here.
You can start with using this one,
new DateTime(2010,02,23)

Just an example on how to solve this:
using System;
public class Student
{
public Student(string name, DateTime dateOfBirth)
{
Name = name;
DateOfBirth = dateOfBirth;
}
public string Name { get; private set; }
public DateTime DateOfBirth { get; private set; }
}
public static class Helpers
{
public static bool IsOlderThen(this DateTime date, TimeSpan age)
{
var now = DateTime.UtcNow;
return now - date > age;
}
}
public class Program
{
public static void Main()
{
var adult = TimeSpan.FromDays(365 * 18);
var studentOld = new Student("Alice", DateTime.Parse("1998/04/17"));
var studentYoung = new Student("Bob", DateTime.Parse("2015/04/17"));
Console.WriteLine("isAdult: " + studentOld.DateOfBirth.IsOlderThen(adult));
Console.WriteLine("isAdult: " + studentYoung.DateOfBirth.IsOlderThen(adult));
}
}

Related

How to get Member.Age from Member.Birthday? [duplicate]

This question already has answers here:
How do I calculate someone's age based on a DateTime type birthday?
(74 answers)
Closed 7 months ago.
I have a class as below:
class Member
{
public string Name { get; set; }
public DateTime Birthday { get; set; }
private int _age;
public int Age
{
get { return _age; }
private set { _age = DateTime.Now.Year - Birthday.Year; }
}
}
In the Main method, I assign member values and I want to get each member's age, but every result is zero, Why? How to solve this problem? Thanks!
class Program
{
static void Main(string[] args)
{
List<Member> memberList = new List<Member>(); //using System.Collections.Generic;
memberList.Add(new Member() { Name = "Andy", Birthday = new DateTime(1971, 7, 26)});
memberList.Add(new Member() { Name = "Mike", Birthday = new DateTime(1982, 1, 17)});
memberList.Add(new Member() { Name = "Lucy", Birthday = new DateTime(1993, 9, 28)});
foreach (var m in memberList)
{
Console.WriteLine(m.Age); //m.Age = 0
}
}
}```
You need to update your class like this because the setter are not doing the operation since youre returning the variable on the get and thats why you got a 0 :
public class Member
{
public string Name { get; set; }
public DateTime Birthday { get; set; }
public int Age
{
get { return DateTime.Now.Year - Birthday.Year; }
}
}
after that change you can get the agre,btw you can calculate the age easier with a timespan to be more exact.
Please return the age from getter method like this.
class Member
{
public string Name { get; set; }
public DateTime Birthday { get; set; }
private int _age;
public int Age
{
get { return DateTime.Now.Year - Birthday.Year; }
private set { _age = DateTime.Now.Year - Birthday.Year; }
}
}
You can remove the calculation from setter method.
Please see the output-

Having trouble using methods in custom classes in C#

I've created a class called "Person" and I'm trying to write a method called Birthday that would increase the attribute "Age" by one. I know it's dumb to try and define a variable using Age, but I can't figure out how to pass a variable from Main into Person.Birthday. I also probably don't need that while loop, but I was just trying a bunch of things. Anyway, help would be appreciated.
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.Name = "Frank";
p1.Age = 30;
p1.Gender = "Male";
p1.Birthday();
Console.WriteLine(p1.Age);
Console.ReadLine();
}
class Person
{
public string Name { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
public int Birthday()
{
int newAge = Age;
while (Age > 0)
{
newAge += 1;
break;
}
return newAge;
}
}
You have a couple issues with your code.
First you shouldnt be storing the "Age" of a person, you should be storing the Date they were born so that you can always calculate the Age.
public DateTime BirthDate { get; set; }
Second; consider the creation of a Person. In your code you create a new Person, then assign their Name, Gender and BirthDate properties. Is that really the best way of doing it? A Person has to have a Name, Gender and BirthDate right? Plus these properties don't change (or at least they shouldn't easily change). So make your Person class have a constructor accepting these arguments, and then make the setting of these properties private:
public string Name { get; private set; }
public string Gender { get; private set; }
public DateTime BirthDate { get; private set; }
public Person(string name, string gender, DateTime birthDate)
{
Name = name;
Gender = gender;
BirthDate = birthDate;
}
Then to calculate someone Age you can just compare the dates in a GetCurrentAge() method, then you can use your existing Age property but make it "read only" by just returning the value from GetCurrentAge():
public int Age => GetCurrentAge();
private int GetCurrentAge()
{
DateTime today = DateTime.Today;
int age = today.Year - BirthDate.Year;
if (BirthDate > today.AddYears(-age))
{
age--;
}
return age;
}
The code and calculation you are doing does not make sense but the bigger problem is that you should not store a calculated field. Instead store the date of birth and calculate the age. See also https://stackoverflow.com/a/1404/1260204 for how to calculate.
public class Person
{
public string Name { get; set; }
public string Gender { get; set; }
public DateTime BirthDate { get; set; }
public int Age
{ get {
// copied from https://stackoverflow.com/a/1404/1260204
var today = DateTime.Today;
var age = today.Year - BirthDate.Year;
if (birthdate > today.AddYears(-age)) {
age--;
}
return age;
}}
}
Calling the method
static void Main(string[] args)
{
Person p1 = new Person();
p1.Name = "Frank";
p1.Age = 30;
p1.Gender = "Male";
p1.BirthDate = new DateTime(1986, 5, 12);
Console.WriteLine(p1.Age);
Console.ReadLine();
}

Constructor should have specified parameter

Hello i am doing a school assignment. Everything in my program works fine but the teacher wants me to add a parameter of type DateTime in one of my constructors. I am a bit confused since i think that i already have a parameter of this type:
using System;
using System.Windows.Forms;
namespace Assignment4
{
class Task
{
private string time = string.Empty;
private string date = string.Empty;
private DateTime dateTime = new DateTime();
private string description = string.Empty;
private object priorityType;
private string priority;
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public DateTime DateTime
{
set
{
dateTime = value;
time = dateTime.TimeOfDay.ToString();
date = dateTime.Date.ToString("d");
}
}
public string Time
{
get
{
return time;
}
}
public string Date
{
get
{
return date;
}
}
public object PriorityType
{
set
{
priorityType = value;
priority = priorityType.ToString();
}
}
public string Priority
{
get
{
return priority;
}
}
}
}
Is dateTime = value not a parameter of type DateTime?
The constructor of a C# class is a method without a return type and with the same name of the class. The constructor is called everytime you create an instance of the class (new YourClass).
You can have many constructors with different kind of parameters passed to these methods, even one without parameters (it is the default constructor).
The correct constructor is identified by the parameters that you pass when you create the class....
public class Person
{
private string _name;
private DateTime _dob;
public Person(string name, DateTime dateOfBirth)
{
_name = name;
_dob = dateOfBirth;
}
}
..... somewhere in your code .....
Person myself = new Person("Steve", new DateTime(1970,1,1));
Since DateTime is an immutable struct, you can only set its values from the constructor. That means you need to do something like this:
dateTime = new DateTime(2016, 05, 03);
In your case, you can just use this, since you set it somewhere else:
private DateTime dateTime;
(Also, your property needs a get too, you just have a set now)

C# How to return string instead of DateTime using Partial class

In my silverLight project I have a class on web side with few DateTime fields. I want to write Partial class for the class on client side which will return string instead of DateTime. How to write it?
This is what I tried. I added new string variable in Partial class which will get date field's and return string.
here is code:
public partial class abcd
{
DateTime date1;
public DateTime Date1
{
get { return date1; }
set { date1 = value; }
}
DateTime date2;
public DateTime Date2
{
get { return date2; }
set { date2 = value; }
}
}
public partial class abcd
{
string date1Str;
public string Date1Str
{
get { return Date1Str; }
set { date2 = Date1.ToString(MM/dd/yyyy); }
}
}
I guess Date1Str should be readonly and just look like this:
public string Date1Str
{
get { return date1.ToString("MM/dd/yyyy"); }
}

Sort array\list after object is changed

What is the best approach for sorting a generic list when one of its objects property is changed?
I have the following example to help explain what is needed.
public class Sending
{
public Sending(int id, DateTime dateSent)
{
this.Id = id;
this.DateSent = dateSent;
}
public int Id { get; set; }
public DateTime DateSent { get; set; }
}
public class Operation
{
public List<Sending> ItemsSent = new List<Sending>();
public Operation()
{
ItemsSent.Add(new Sending(1, new DateTime(2010, 6, 2)));
ItemsSent.Add(new Sending(2, new DateTime(2010, 6, 3)));
ItemsSent[1].DateSent = new DateTime(2010, 6, 1);
}
}
What is the best way to trigger a sort on the list to sort by date after the DateSent property is set? Or should I have a method to update the property and perform the sort?
You could implement IComparable<Sending> on Sending and call Sort() on the ItemsSent. I would suggest to write a method to update an object and update the list manually.
public class Sending: IComparable<Sending>
{
// ...
public int CompareTo(Sending other)
{
return other == null ? 1 : DateSent.CompareTo(other.DateSend);
}
}
What you can do is you first implement INotifyChanged.
Then do some thing like this;
public class Sending : INotifyChanged
{
private int id;
private DateTime dateSent;
public Sending(int id, DateTime dateSent)
{
this.Id = id;
this.DateSent = dateSent;
}
public int Id { get; set; }
public DateTime DateSent
{
get
{
return this.dateSend;
}
set
{
this.dateSent = value;
OnPropertyChangerd("DateSent");
//CallYou List<Sending> Sort method;
}
}
So whenever a new value will set the sort method will sort the list.

Categories

Resources