Solved

MyObject obj1 = obj as MyObject;

Posted on 2014-03-03
1
190 Views
Last Modified: 2014-03-11
Can someone tell me what this line of code mean.... MyObject obj1 = obj as MyObject;

class MyObject : IComparable
{
public int ID{ get; set; }
public int CompareTo(object obj)
{
MyObject obj1 = obj as MyObject;
return this.ID.CompareTo(obj1.ID);
}
}

Open in new window

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
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39902123
It's a cast. Ordinarily, you use parentheses and a type name to perform a cast:

e.g.

MyObject obj1 = (MyObject)obj;

Open in new window


This has the consequence that if obj is not actually of type MyObject, or descended from it, then you will encounter an exception. The as operator also performs casts, but it has the added benefit that if the cast will not succeed--that is, if the thing being cast isn't of the type being cast to (or descended from it)--then as will return null. It's a good way to confirm a cast will succeed before working with the casted reference.

A word of warning:  Since as can return null you should be checking for null any time you use as:

e.g.

MyObject obj1 = obj as MyObject;

if (obj1 != null)
{
    // Cast succeeded
}

if (obj1 == null)
{
    // Cast failed
}

Open in new window

0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB: Convert 2 dates to specific format 24 55
Please explain the parts of these 2 LINQ expressions 3 83
abstract class C# 1 37
Save json data from URL using SSIS 1 24
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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