473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cast From Abstract to decendant

hi all,

if I have a class:
CODE

public abstract class Person
{
public string firstname;
public string lastname;

Person(string fname,string lname)
{
firstname=fname;
lastname=lname;
}
}
and I use it like this:
CODE

public class Employee : Person
{
public string ID;

public Employee(string fname,string lname,string
myid):base(fname,lname)
{
ID=myid;
}
}

Now I have a method that I pass the abstract class to
CODE
public void SendingMethod()
{
Employee emp = new Employee("biff","henderson","123");
mymethod(emp);
}
public void mymethod(Person p)
{
//code goes here
}

How do I access the ID on the employee?
I have tried:
p.ID,
(Employee)p.ID

when I watch the variable p i can see that it is of the type Employee
and that the ID is there.

I am sure it is a simple one but I havent used abstraction a lot and am
at a loss as to what to even search for.

Thanks in advance

Bassguy

Nov 16 '05 #1
5 1264
Bassguy,

Well, in essence, you don't, not with what you have now. The ID
property (which should be Id, not ID), is on the Employee class. The method
you pass the instance to takes a Person instance. If you need the Id
property, then you should move the definition of the Id to the abstract
class, so that all classes deriving from Person have it. Either that, or
have your method take an Employee instance, since it obviously expects that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tal_mcmahon" <ta*********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
hi all,

if I have a class:
CODE

public abstract class Person
{
public string firstname;
public string lastname;

Person(string fname,string lname)
{
firstname=fname;
lastname=lname;
}
}
and I use it like this:
CODE

public class Employee : Person
{
public string ID;

public Employee(string fname,string lname,string
myid):base(fname,lname)
{
ID=myid;
}
}

Now I have a method that I pass the abstract class to
CODE
public void SendingMethod()
{
Employee emp = new Employee("biff","henderson","123");
mymethod(emp);
}
public void mymethod(Person p)
{
//code goes here
}

How do I access the ID on the employee?
I have tried:
p.ID,
(Employee)p.ID

when I watch the variable p i can see that it is of the type Employee
and that the ID is there.

I am sure it is a simple one but I havent used abstraction a lot and am
at a loss as to what to even search for.

Thanks in advance

Bassguy

Nov 16 '05 #2
maybe my model is the problem.

I am trying to implement a method that can accept a class derived from
my abstract class. and then I would sniff the class and operate on it
differently for each kind of derived class I use.

As I was trying to think of a response for your help (thanks for the
speed btw) It occurs to me that I should just overload the method and
pass in the derived classes. This would mean a new overload for each
drived class not an extension of a giant switch.

Here are my thoughts:
Old Idea:

class A{}

class B:A{}

class C:A{}

method(A a)
{

switch(a.GetType().Name)

case "B":
do B stuff
break;

case "C"
do C Stuff
break;
}
}

the new model would be

class A{}

class B:A{}

class C:A{}

method(B b)
{
do B stuff
}

method(C c)
{
do C stuff
}
any flaws with this??

Thx again

Nov 16 '05 #3
Nope, that's exactly how I would do it. Much, MUCH cleaner, IMO.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tal_mcmahon" <ta*********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
maybe my model is the problem.

I am trying to implement a method that can accept a class derived from
my abstract class. and then I would sniff the class and operate on it
differently for each kind of derived class I use.

As I was trying to think of a response for your help (thanks for the
speed btw) It occurs to me that I should just overload the method and
pass in the derived classes. This would mean a new overload for each
drived class not an extension of a giant switch.

Here are my thoughts:
Old Idea:

class A{}

class B:A{}

class C:A{}

method(A a)
{

switch(a.GetType().Name)

case "B":
do B stuff
break;

case "C"
do C Stuff
break;
}
}

the new model would be

class A{}

class B:A{}

class C:A{}

method(B b)
{
do B stuff
}

method(C c)
{
do C stuff
}
any flaws with this??

Thx again

Nov 16 '05 #4
On 21 Jan 2005 07:58:18 -0800, tal_mcmahon wrote:
(Employee)p.ID


This is close. You could do ((Employee)p).ID. Or the following:

Employee emp = p as Employee;
string id = emp.ID;
--
Tom Porterfield
Nov 16 '05 #5
Tom,
Thanks I went with your method. works great!

Nicholas, your method only took the step away from the method I was
calling and placed the switch in the method that was doing the calling.
I am trying to always pass the abstract and then use the methods to
"sniff" what type the inherited item is to avoid recodes as new
decendants are created.

tal

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
2
by: Ulrich Heinen | last post by:
Let's say I have two classes A and B with similar goals but completely different implementations. In my application code I want to use class A exclusively, but instances of B should also be...
6
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has...
8
by: CA | last post by:
Hi, I have a function where I would like to test whether an object is of a certain type. Here is my code so far. public bool HasValidType(Type t, object val) { try { if (t==typeof(double))
5
by: Daniel Bass | last post by:
I've got some class which contains loads of static const int values, is there a way that, given an int, i can quickly cast the int back to a string representation of the const? for eg. ...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
3
by: Néstor Sánchez | last post by:
Hi, i'm trying to solve the next problem ... public class BaseType {...} public class DerivedType : BaseType { ... } public abstract class BaseClass { public abstract Type getGenericType();...
4
by: sklett | last post by:
I'm revisiting an old series of overloaded methods I have and I would like to convert them to a single generic method. Here is the method in it's current, overloaded implementation: <code>...
4
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. The question is about this method GetCows() {...} which is a member in the generic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.