This question already has an answer here:
I have a simple custom list class and I am trying to implement IComparable
to it, but it's not working to be honest. I tried MSDN and other blogs and still same.
public class sortDateTime : IComparable
{
protected DateTime m_startDate, m_endDate;
public DateTime startDate
{
get { return m_startDate; }
set { m_startDate = startDate; }
}
public DateTime endDate
{
get { return m_endDate; }
set { m_endDate = endDate; }
}
public int CompareTo(object obj)
{
if(obj is sortDateTime)
sortDateTime sDT = (sortDateTime) obj; //here ERROR
return m_stDate.CompareTo(sDT.m_stDate);
}
}
Followed this example, but getting the error:
Embedded statement cannot be a declaration or labeled statement
Just do like this:
and it will be gone.
For a more concrete explanation on why the compiler behaves in this way, please look on: Why this compile error
Following
C#
standard naming convention: do not name your types starting with non uppercase letters, so changesortDateTime
->SortDateTime
Try this:
Please take a look at the piece of code, resulting in an error:
What you are saying is this:
And then you are leaving the scope - the variable is not needed anymore (it's allocated on the 'Stack' and get's released). This does not make sense. It's an operation, that get's executed for nothing. What you want to do is the following:
The compiler notices that you cannot do such an operation and throws you an error. However this would compile:
but it would not make sense either, and lead to an compiler error at
This is how I would implement the method:
Cheers!
Without checking your logic, I'll fix the syntax error.
This:
Should be:
Look more closely at the page you linked. You didn't follow it properly.