[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Help in Inheritance, I am new to it.

Nigil

2/21/2008 11:09:00 AM

Hi Guy,

I am having issues casting my _Propery back to the _User. Can someone kindly
advise me.

Following is the code.

using System;

class Property
{
private string _Name="";
public string Name
{
get {return _Name;}
set {_Name=value;}
}
}
class User:Property
{
private string _ID="";
public string ID
{
get {return _ID;}
set {_ID=value;}
}
}

class App
{
public static void Main(string[] args)
{
User _User=new User();

Property _Property=new Property();
_Property.Name="Lavey";

_User=(Property)_Property; // Here is where the compiler throws error.

Console.WriteLine(_User.Name);
Console.Read();
}
}


4 Answers

Jon Skeet

2/21/2008 11:21:00 AM

0

Nigil <nchua@mfglobal.com.sg> wrote:
> I am having issues casting my _Propery back to the _User. Can someone kindly
> advise me.

You're trying to cast it to Property, but the type of _User is User,
which is more specific than Property - hence the compilation issue. You
potentially should be casting it to User.

However, then it would fail at execution time - because the actual
object isn't a User, it's just a Property because you created it with
new Property().

Casting doesn't change the actual type of an object (at least not when
it's a simple cast like this, rather than one which invokes a user-
defined conversion).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.... Blog: http://www.msmvps.com...
World class .NET training in the UK: http://iterativetrai...

Patrice

2/21/2008 1:00:00 PM

0

This is the other way round. That is when you declare a variable it can hold
also inherited types here you try to use an ancestor type.

That is in a variable declared as a Property : you'll be able to store an
instance of Property or User (because User inherits from Property and so
have the same minimal guaranteed members set).

On the other hand if you declare a User variable you won't be able to store
a Property instance because User could have added new members that make no
sense for a Property instance.


"Nigil" <nchua@mfglobal.com.sg> a écrit dans le message de news:
uvXbyoHdIHA.2688@TK2MSFTNGP06.phx.gbl...
> Hi Guy,
>
> I am having issues casting my _Propery back to the _User. Can someone
> kindly advise me.
>
> Following is the code.
>
> using System;
>
> class Property
> {
> private string _Name="";
> public string Name
> {
> get {return _Name;}
> set {_Name=value;}
> }
> }
> class User:Property
> {
> private string _ID="";
> public string ID
> {
> get {return _ID;}
> set {_ID=value;}
> }
> }
>
> class App
> {
> public static void Main(string[] args)
> {
> User _User=new User();
>
> Property _Property=new Property();
> _Property.Name="Lavey";
>
> _User=(Property)_Property; // Here is where the compiler throws error.
>
> Console.WriteLine(_User.Name);
> Console.Read();
> }
> }
>
>


Nigil Chua

2/21/2008 1:18:00 PM

0

Hi Guys,

Thanks for the prompt replies.

Patrice, will be nice if you could write me an simple example regarding your
workaround.

best regards,
Nigil Chua

"Patrice" <http://www.chez.com/s... wrote in message
news:OYf$dmIdIHA.5160@TK2MSFTNGP05.phx.gbl...
> This is the other way round. That is when you declare a variable it can
> hold also inherited types here you try to use an ancestor type.
>
> That is in a variable declared as a Property : you'll be able to store an
> instance of Property or User (because User inherits from Property and so
> have the same minimal guaranteed members set).
>
> On the other hand if you declare a User variable you won't be able to
> store a Property instance because User could have added new members that
> make no sense for a Property instance.
>
>
> "Nigil" <nchua@mfglobal.com.sg> a écrit dans le message de news:
> uvXbyoHdIHA.2688@TK2MSFTNGP06.phx.gbl...
>> Hi Guy,
>>
>> I am having issues casting my _Propery back to the _User. Can someone
>> kindly advise me.
>>
>> Following is the code.
>>
>> using System;
>>
>> class Property
>> {
>> private string _Name="";
>> public string Name
>> {
>> get {return _Name;}
>> set {_Name=value;}
>> }
>> }
>> class User:Property
>> {
>> private string _ID="";
>> public string ID
>> {
>> get {return _ID;}
>> set {_ID=value;}
>> }
>> }
>>
>> class App
>> {
>> public static void Main(string[] args)
>> {
>> User _User=new User();
>>
>> Property _Property=new Property();
>> _Property.Name="Lavey";
>>
>> _User=(Property)_Property; // Here is where the compiler throws error.
>>
>> Console.WriteLine(_User.Name);
>> Console.Read();
>> }
>> }
>>
>>
>
>


Patrice

2/21/2008 6:00:00 PM

0

This is not really a workaround. The exact thing you are trying to do is
just not possible. (a user is a prototype but a prototype is not a user).

The closer workaround would be that if you wan't to be able to store both
Prototype and User instances in a variable you have to use the Prototype
type.

The idea is that :

- you create a class Prototype
- you create a class User that inherits from Prototype that is you have the
guarantee that it knows to do all what the Prototype class knows to do (and
possibly it could add new members to do more), that is a User *is a*
Prototype but not the other way round

So now :
- if you are using a Prototype variable, you'll be able to store a Prototype
instance or a User instance (as both will use common known members)
- if you are using a User variable, you'll be able to only store a User
instance (as a Prototype could miss new members added in the User class
making no sense to do _User.SomethingNew for a Prototype instance)

It's likely best to give a closer look at an OO introduction to clear out
these concepts...

--
Patrice



"Nigil Chua" <nigil_chua@yahoo.com> a écrit dans le message de news:
O8cm$wIdIHA.3400@TK2MSFTNGP03.phx.gbl...
> Hi Guys,
>
> Thanks for the prompt replies.
>
> Patrice, will be nice if you could write me an simple example regarding
> your workaround.
>
> best regards,
> Nigil Chua
>
> "Patrice" <http://www.chez.com/s... wrote in message
> news:OYf$dmIdIHA.5160@TK2MSFTNGP05.phx.gbl...
>> This is the other way round. That is when you declare a variable it can
>> hold also inherited types here you try to use an ancestor type.
>>
>> That is in a variable declared as a Property : you'll be able to store an
>> instance of Property or User (because User inherits from Property and so
>> have the same minimal guaranteed members set).
>>
>> On the other hand if you declare a User variable you won't be able to
>> store a Property instance because User could have added new members that
>> make no sense for a Property instance.
>>
>>
>> "Nigil" <nchua@mfglobal.com.sg> a écrit dans le message de news:
>> uvXbyoHdIHA.2688@TK2MSFTNGP06.phx.gbl...
>>> Hi Guy,
>>>
>>> I am having issues casting my _Propery back to the _User. Can someone
>>> kindly advise me.
>>>
>>> Following is the code.
>>>
>>> using System;
>>>
>>> class Property
>>> {
>>> private string _Name="";
>>> public string Name
>>> {
>>> get {return _Name;}
>>> set {_Name=value;}
>>> }
>>> }
>>> class User:Property
>>> {
>>> private string _ID="";
>>> public string ID
>>> {
>>> get {return _ID;}
>>> set {_ID=value;}
>>> }
>>> }
>>>
>>> class App
>>> {
>>> public static void Main(string[] args)
>>> {
>>> User _User=new User();
>>>
>>> Property _Property=new Property();
>>> _Property.Name="Lavey";
>>>
>>> _User=(Property)_Property; // Here is where the compiler throws error.
>>>
>>> Console.WriteLine(_User.Name);
>>> Console.Read();
>>> }
>>> }
>>>
>>>
>>
>>
>
>