471,306 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Calling ancestor method

I know that you can do this, I can't find the answer. I try the following code

((company) base).newCompany("ACME", "vendor");

Of course company is the ancestor and newCompany is a public method. The
compiler tells me that "use of keyword 'base' not valid in this context"

What am I missing?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the audience.

Animadverto est verus
Jan 10 '06 #1
7 2070
Rik Brooks wrote:
I know that you can do this, I can't find the answer. I try the following code

((company) base).newCompany("ACME", "vendor");

Of course company is the ancestor and newCompany is a public method. The
compiler tells me that "use of keyword 'base' not valid in this context"

What am I missing?


You don't need to cast to company - just call

base.newCompany("ACME", "vendor");

Jon

Jan 10 '06 #2
Nope, the compiler tells me

Error 1 'object' does not contain a definition for 'newCompany'
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the audience.

Animadverto est verus


"Jon Skeet [C# MVP]" wrote:
Rik Brooks wrote:
I know that you can do this, I can't find the answer. I try the following code

((company) base).newCompany("ACME", "vendor");

Of course company is the ancestor and newCompany is a public method. The
compiler tells me that "use of keyword 'base' not valid in this context"

What am I missing?


You don't need to cast to company - just call

base.newCompany("ACME", "vendor");

Jon

Jan 10 '06 #3
Rik Brooks wrote:
Nope, the compiler tells me

Error 1 'object' does not contain a definition for 'newCompany'


In that case you're not calling it from a class derived from company.

Perhaps it would be clearer if you gave a short but complete example of
what you're trying to do. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
this.

Jon

Jan 10 '06 #4
OOPS! I forgot to make vendor inherit from company. I thought for sure I had
done that.

Never miiiiiind
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the audience.

Animadverto est verus


"Rik Brooks" wrote:
I know that you can do this, I can't find the answer. I try the following code

((company) base).newCompany("ACME", "vendor");

Of course company is the ancestor and newCompany is a public method. The
compiler tells me that "use of keyword 'base' not valid in this context"

What am I missing?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the audience.

Animadverto est verus

Jan 10 '06 #5
"Rik Brooks" <rb*****@hhcc.com.donotspam> a écrit dans le message de news:
54**********************************@microsoft.com...

| OOPS! I forgot to make vendor inherit from company. I thought for sure I
had
| done that.

Heheh, easily done.

BTW, you have derived Vendor from Company; what would you do if a Company
was also a Supplier ?

Many people have fallen into this trap of trying to use inheritance when it
isn't always wise. Just for your and other's information, this situation can
be overcome by using the concept of Roles :

class Role
{
...
}

class Vendor : Role
{
...
}

class Supplier : Role
{
...
}

class Company
{
public List<Role> Roles {...}
public bool FulfilsRole(Role role) {...}
...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 10 '06 #6

Joanna Carter [TeamB] wrote:
"Rik Brooks" <rb*****@hhcc.com.donotspam> a écrit dans le message de news:
54**********************************@microsoft.com...

| OOPS! I forgot to make vendor inherit from company. I thought for sure I
had
| done that.

Heheh, easily done.

BTW, you have derived Vendor from Company; what would you do if a Company
was also a Supplier ?

Many people have fallen into this trap of trying to use inheritance when it
isn't always wise. Just for your and other's information, this situation can
be overcome by using the concept of Roles :


How about plain interfaces?

--
Larry Lard
Replies to group please

Jan 10 '06 #7
"Larry Lard" <la*******@hotmail.com> a écrit dans le message de news:
11**********************@f14g2000cwb.googlegroups. com...

| How about plain interfaces?

Well, they can be useful, but that would mean that, unless you have a list
of IRole, you have to tie a class like Company to implement IVendor and/or
ISupplier. This then means that you can't add a role without altering the
Company class to include a new role. Neither can the Company change roles or
drop a role. You have to consider the lifetime of a class and plan for the
possible changes in role without tying the class to a fixed set of roles.

You could declare an interface :

interface IRoleAdopter
{
void AddRole(Role role);
void RemoveRole(Role role);
bool Implements Role(Role role);
...
}

and then implement that in any class that needs it. But that may not really
make any difference from designing the classes with that support built in,
apart from the fact that you could build a delegate class that implements
the IRoleAdopter and that can be instantiated inside any class.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Partridge | last post: by
2 posts views Thread by Jesper | last post: by
11 posts views Thread by John Bailo | last post: by
2 posts views Thread by werD | last post: by
3 posts views Thread by Luis P. Mendes | last post: by
3 posts views Thread by Valvalis | last post: by

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.