❔ Need help with datetime
I need to create a class which has two fields; name and date. Then create objects of this class. I need to use datetime for the date field. anyone knows what im doing wrong?
so basically;
static void Main()
{
//create intances of Employee;
Employee emp1 = new Employee("James", 25, new DateTime(2000, 5, 1));
//add employees to list
List<Employee> employees = new List<Employee>(4);
employees.Add(emp1);
//LINQ Questions;
//Q1. Return a List of employees that hired after the year 2005
var queryHiredAfter2005 = from emp in employees
where emp.HireDate.Year > 2005
select emp.Name;
foreach (var res in queryHiredAfter2005)
{
Console.WriteLine(res);
}
class Employee
{
public string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public DateTime hiredate;
public DateTime HireDate { get; set; }
//Constructor;
public Employee(string _name, int _age, DateTime _hiredate)
{
_name = Name;
_age = Age;
_hiredate = HireDate;
}
19 Replies
What problem are you facing? @Guns
You don't seem to have a prop for age, and your constructor is... Wrong
Your naming convention is inverted
Name should be an auto prop
with the datetime prop
yes ill fix later
the main issue is the datetime
i need to have datetime as a prop
how does that work
What is wrong with it? Seems fine.
If you fix your constructor, it should worl
what should i fix in it?
Read it again, and check what is on what side of the equals sign
so is this ok; 'new DateTime(2000, 5, 1))'. thats how i define the date property for an obj?
Sure
ohh
i thought it was wrong
I'm not at a computer right now, but there sure is a date time constructor that looks something like that
'where emp.HireDate.Year > 2005' so this linq query should work?
Yup, but it will look only on year
So 2005 december will not count
ok ill have a look into that
gimme a sec
Couple of things to consider as well
You should refactor
Name
property to be an auto property, also remove hiredate
public fieldAnd the names in the ctor makes me very upset
i fix that
underscores 😅
i always make the dumbest mistakes lol
thanks guys
gg
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.