?
Solved

MyObject obj1 = obj as MyObject;

Posted on 2014-04-20
6
Medium Priority
?
149 Views
Last Modified: 2014-09-10
I was wondering what this line of code mean?

MyObject obj1 = obj as MyObject;

Open in new window


Original code

class MyObject : IComparable

    {
   
    public int ID { get; set;}

    public int CompareTo(object obj){
MyObject obj1 = obj as MyObject;
return this.ID.CompareTo(obj1.ID);
}
   
    }
0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:ste5an
ID: 40011799
It is called a cast.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 40011824
Yes, this is casting. But I'd like to add.

There are different ways of casting. Say,

1 - MyObj obj1= (MyObj) obj; // most common
2 -MyObj obj1= objas MyObj; // quite rare casting

Open in new window


There is a difference between them.

The first one is called sometimes a "direct" cast. It throws an exception if the cast is invalid. It is also the only way to perform actual type conversion on the object.

The second one with no particular name evaluates to null instead of throwing an InvalidCastException. Kind of "try cast".

No major performance issues for both.

So it is recommended to use the second type of cast only if the result might not be valid.
0
 

Author Comment

by:yguyon28
ID: 40011911
Cool I got it but why do I need to cast to that object?

And what does this line does?

return this.ID.CompareTo(obj1.ID);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 40011924
That is an easy part :).

The method to compare IDs has the following signature:
public int CompareTo(object obj)

Open in new window


with an Object type argument. I.e., most generic type. But as you know the Object type has no properties but only few methods. So - you cannot know its id.
But we suggest that this Object has more specific class type - MyObject. Therefore we cast a generic Object To MyObject. After casting we may access the ID property of MyObject instance.

The CompareTo method checks if passed object has the same id as current instance. Simply by comparing two integers: this.ID and obj1.ID.

Really, the CompareTo method is a method that has to be implemented for IComparable interface.
In this case the implementation is not really good enough. What happens if we pass not MyObject instance to the method but, e.g., a string? This will cause an error, because, as I told before, this cast will return a null object. And an attempt to get an ID for this null object will cause a runtime exception. So, the better solution would be something like:

if (obj1==null)
{
   return -1; // or may be something else but indicating this object and null object are not same
}
else
{
   return this.ID.CompareTo(obj1.ID);
}

Open in new window

0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 40011934
BTW, read about the CompareTo method:
http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(v=vs.110).aspx

It returns 0, in your case,  if first ID equals second ID. But pls read the link for more info.
0
 

Author Comment

by:yguyon28
ID: 40316257
tks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question