Help in Inheritance, I am new to it.

  • Thread starter Thread starter Nigil
  • Start date Start date
N

Nigil

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 said:
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).
 
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.
 
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 said:
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 said:
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();
}
}
 
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 said:
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 said:
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 said:
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();
}
}
 
Back
Top