Solved

MyObject obj1 = obj as MyObject;

Posted on 2014-04-20
6
133 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 33

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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