Solved

MyObject obj1 = obj as MyObject;

Posted on 2014-04-20
6
131 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
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

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

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 29

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 29

Accepted Solution

by:
anarki_jimbel earned 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now