Solved

MyObject obj1 = obj as MyObject;

Posted on 2014-04-20
6
132 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

14 Experts available now in Live!

Get 1:1 Help Now