Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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

C#
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.

—SA
 
Share this answer
 
Comments
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.
—SA
Anderso0on 21-Feb-13 17:22pm    
pleace explain it to me more , i donot understand this
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"?..

—SA
Anderso0on 21-Feb-13 17:26pm    
i think as operator work between any referance type
http://msdn.microsoft.com/en-us/library/cscsdfbt(v=vs.71).aspx
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:

C#
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:

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900