-7

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
user2262511
  • 101
  • 10

4 Answers4

4

Please take a look at the piece of code, resulting in an error:

if(obj is sortDateTime)
    sortDateTime sDT = (sortDateTime) obj; //here ERROR

return m_stDate.CompareTo(sDT.m_stDate);

What you are saying is this:

if the object is of type 'sortDateTime'
    Allocate memory for variable 'sDT'
    Cast 'obj' to type 'sortDateTime' 
    Store the result in variable 'sDT'

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:

// Variable for remembering the "cast result" after the cast
sortDateTime sDT = null;

if (obj is sortDateTime)
    sDT = (sortDateTime)obj;  // Cast the object.
else
    return 0;                 // "obj" is not an "sortDateTime", so we can't compare.

// Return the comparison result, if we can compare.
return m_stDate.CompareTo(sDT.m_stDate);

The compiler notices that you cannot do such an operation and throws you an error. However this would compile:

if (obj is sortDateTime)
{
    sortDateTime sDT = (sortDateTime)obj;
}

but it would not make sense either, and lead to an compiler error at

m_stDate.CompareTo(sDT.m_stDate);  // sDT is not a variable in scope.

This is how I would implement the method:

sortDateTime sDT = obj as sortDateTime;  // 'as' leads to an casted object, or null if it could not cast

if (sDT == null)
    throw new NotSupportedException("The object is not an sortDateTime");
else
    return m_stDate.CompareTo(sDT.m_stDate);

Cheers!

Carsten
  • 11,287
  • 7
  • 39
  • 62
2

Without checking your logic, I'll fix the syntax error.

This:

public int CompareTo(object obj)
{
    if(obj is sortDateTime)
        sortDateTime sDT = (sortDateTime) obj; //here ERROR

    return m_stDate.CompareTo(sDT.m_stDate);
}

Should be:

public int CompareTo(object obj)
{
    if (obj is sortDateTime)
    {
        sortDateTime sDT = (sortDateTime) obj;
        return m_startDate.CompareTo(sDT.m_startDate);
    }
    else
    {
        throw new ArgumentException("object is not a sortDateTime ");   
    }
}

Look more closely at the page you linked. You didn't follow it properly.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

Just do like this:

if(obj is sortDateTime) {
     sortDateTime sDT = (sortDateTime) obj; //here ERROR
}

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 change sortDateTime -> SortDateTime

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • shouldn't you include the return statement within the if? – Julián Urbano Apr 11 '13 at 10:45
  • @caerolus: don't think so. Have no idea what is a workflow for this app. – Tigran Apr 11 '13 at 10:45
  • @Tigran your code is basically the same as his. Look at the return statement and you'll see that he needs to keep the casted object – Julián Urbano Apr 11 '13 at 10:46
  • @voo: the error reported by OP is the error that appears when you declare the varible inside single line statement without using curly braces. For understanding why, follow the ink provided and read Skeet's asnwer. – Tigran Apr 11 '13 at 10:49
  • @downvoter: any **sane** reason ? – Tigran Apr 11 '13 at 10:50
  • @Tigran Not my downvote, thanks for clarification – Alex Apr 11 '13 at 10:54
  • @Tigran your solution still wouldn't solve the OP's problem and it would still give a compile error because `sDT` is lost after the `if`. – Julián Urbano Apr 11 '13 at 11:02
  • @caerolus: the **problem** in the question is: "Embedded statement error", so my answer targets resolve exact problem was asked. If there is a lack of other code, have no idea, but what I'm sure about is that problem reported is resoled by code provided in my answer. – Tigran Apr 11 '13 at 11:06
  • @Tigran great, so you knowingly provide an answer that does not solve the OP's **actual** problem...even produce another compile error that you don't even care to mention and that is cool apparently...thumbs up. I'll remove your -1 so you're happy – Julián Urbano Apr 11 '13 at 11:10
0

Try this:

public int CompareTo(object obj)
{
    sortDateTime sDT = null;
    if(obj is sortDateTime)
        sDT = (sortDateTime) obj; //here ERROR

    if(sDT != null)
    {
        return m_stDate.CompareTo(sDT.m_stDate);
    }
    else
    {
        throw new ArgumentException("object is not a sortDateTime type.");
    }
}
Azhar Khorasany
  • 2,712
  • 16
  • 20