Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MyObject obj1 = obj as MyObject;

Posted on 2014-04-20
6
Medium Priority
?
152 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
Independent Software Vendors: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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