Inheritance problem

B

Buddy Ackerman

I bought the source code for an app and was modifying it however instead of
modifying the code directly I renamed a class and then created a new class
with the old name and inhertied the odl (now renamed ) class. The only
issue the I'm having is the the original class has a method in it the calls
a method of another class and passes itself as a parameter (as "this").
Since it's not longer the same class that it was it won't compile. There
are two methods in this calss that do this and they are also called by other
methods in the class (so I can't just delete them and move them to the new
derived class). Here's and example of the problem


public class MyClassBase_Old
{
public string method1()
{
return OtherClass.Method1(this);
}

public string method2 ()
{
return Method1();
}
}


public class MyClassBase: MyClassBase_Old
{
public NewMethod()
{
return "something";
}
}




The OtherClass.Method1 takes a MyClassBase object. Any ideas on how to fix
this?


--Buddy
 
K

Kevin Spencer

I don't think you have the problem you think you have. If a base class
passes an instance of itself to another class method which takes a parameter
of that type, any class inheriting it should also be able to do the same,
since any class inheriting the base class *is* of the same type as the base
class. This is why, for example, System.Object is passed as a parameter to
many methods which do not know what type to expect. All reference types
inherit System.Object. Therefore, any method taking a type of System.Object
will accept any type that inherits System.Object.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
S

SP

Buddy Ackerman said:
I bought the source code for an app and was modifying it however instead of
modifying the code directly I renamed a class and then created a new class
with the old name and inhertied the odl (now renamed ) class. The only
issue the I'm having is the the original class has a method in it the
calls
a method of another class and passes itself as a parameter (as "this").
Since it's not longer the same class that it was it won't compile. There
are two methods in this calss that do this and they are also called by
other
methods in the class (so I can't just delete them and move them to the new
derived class). Here's and example of the problem
public class MyClassBase_Old
{
public string method1()
{
return OtherClass.Method1(this);
}

OtherClass.Method1 is probably expecting MyClassBase. However you are
passing it MyClassBase_Old. MyClassBase is a MyClassBase_Old as it inherits
from it but MyClassBase_Old is not a MyClassBase.

SP
 
S

SP

Buddy Ackerman said:
I bought the source code for an app and was modifying it however instead of
modifying the code directly I renamed a class and then created a new class
with the old name and inhertied the odl (now renamed ) class. The only
issue the I'm having is the the original class has a method in it the
calls
a method of another class and passes itself as a parameter (as "this").
Since it's not longer the same class that it was it won't compile. There
are two methods in this calss that do this and they are also called by
other
methods in the class (so I can't just delete them and move them to the new
derived class). Here's and example of the problem
Assuming my previous post is correct then you need to change these methods
that are expecting a MyClassBase to accept MyClassBase_Old.

SP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top