Click here to Skip to main content
12,756,634 members (31,695 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
class class1
    { }
    class class2
    { }
    class Program
        static void Main(string[] args)
            class2 ob2 = new class2();
            class1 ob1= ob2 as class1; //ERROR IS 
                                       // convert type class2 to class1 
                                       // via reference conversion ,boxing conversion,
                                       // unboxing conversion ,wrapping conversion ,
                                       //or null type conversion .

Posted 21-Feb-13 11:31am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It statically already known (to the compiler) that the operator fails (would return null because class1 is not class2) if it is. Compare:

class Base {}
class Derived : Base {}


Base base = new Derived();
//Derived derived = base; // will not compile
Derived derived = base as Derived; // will compile, return reference to base as Derived, because this is its run-time type

object ob2 = new class2();
class1 ob1= ob2 as class1; // will compile because object (as compile-time) can be a code1 (as run-time) type, and returns null because actually it's not the case

You apparently need to get the concept of run-time and compile-type types and assignment-compatibility related to inheritance. The background is simple enough.
If you use more the object of most derived class through the variable/member of less derived type, you always correct: you operate only with the members of the less derived type, which always present in the derived type due to inheritance. The opposite situation would be dangerous: it would give the code access to members which do not actually exist in run-time type. In .NET, this is protected: there is no direct assignment compatibility (will not compile without down-casting), down-casting with (MyType)myObject syntax will throw exception, down-casting with as will return null.

Maciej Los 21-Feb-13 17:07pm
A 5!
Concept of run-time and compile-time. But first of all, OP needs to know the difference between run-time and cmpile-time types.
Sergey Alexandrovich Kryukov 21-Feb-13 17:16pm
That's right.
Thank you, Maciej.
Anderso0on 21-Feb-13 17:22pm
pleace explain it to me more , i donot understand this
Anderso0on 21-Feb-13 17:26pm
i think as operator work between any referance type
Sergey Alexandrovich Kryukov 21-Feb-13 17:28pm
Well, now it's your turn: please tell me which part you don't understand. "More" is not informative enough.

You see, I don't know your background. I can see that you are only learning about OOP, but I don't know how much you already got. Do you clearly understand inheritance? Type and instances (objects)? References, reference types, value types? Type members (static and instance members)? "this"?..

Sergey Alexandrovich Kryukov 21-Feb-13 17:30pm
Correct, but it either fails to give an instance and returns null, or not. In case it's already known that it won't give you an instance during compilation (can you see why, from your example), compilation fails (because — why compiling something which won't work out anyway?). Is that clear?
Anderso0on 21-Feb-13 17:42pm
ok, what is different between this two line
object ob2 = new class2();
class1 ob1= ob2 as class1;
class2 ob2 = new class2();
class1 ob1= ob2 as class1;
Sergey Alexandrovich Kryukov 21-Feb-13 17:49pm
object type, as you can see. It compile-time type is object, it's potentially can be of the run-time class class1, because object is the base of any other type.
But class2 cannot be possibly be class1; this is immediately seen from the definitions of those classes. Compiler sees it and fails compilation, because — why compiling it?
Do you follow?
Anderso0on 21-Feb-13 18:09pm
yeah, tell me more
Sergey Alexandrovich Kryukov 21-Feb-13 18:20pm
A reply to what comment is this one? What exactly "more"? And please understand that most of the basics you need to learn by yourself, I can only help where you have some confusion.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Obj2 IS a class2, it isn't a class 1 and the compiler doesn't know how to make it one.

It's like saying:

    volcano vesuvius = new volcano();

    dog rover = vesuvius AS dog;

All the compiler is saying is:

"What the $!@#$?

How do you expect me to turn a volcano into dog?

At least give me a hint.

Do you want me to ignore the fact that it isn't really a dog and just throw a stick and wait around and see if it fetches it?

Or do you have some magic method of speeding up evolution that you haven't told me about that will turn a volcano into a dog on the spot?"

What the compiler will be happy to do is:

class mountain { }

class volcano : public mountain { }

    volcano vesuvius = new volcano();

    mountain mt_vesuvius = vesuvius AS mountain;

You've already said that volcano's are a specialized form of mountains.

So the compiler doesn't have a fit if you want to call a volcano a mountain -- after all it already is one.

Or if you had told the compiler a method of converting one class to the other, then it might have done it for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100