I trying to inherit a class Blah2, but after adding a method it says BlahA doesn't implement that method.
How can I add a method to my new class?
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyNewMethod()
{
}
}
Note: BlahA is an abstract class.
Update
public abstract class BlahA : IBlah
{
}
Update II - the error
Error 3 'Blah.Components.BlahA' does not contain a definition for 'Blah3' and no extension method 'Blah3' accepting a first argument of type 'Blah.Components.BlahA' could be found (are you missing a using directive or an assembly reference?)
Well if it's implementing an interface as you posted in your comments, then the problem is that your BlahA class doesn't satisfy the requirements of the interface. There must be some method in the interface (I'm assuming its the MyNewMethod) that you're not implementing in your abstract BlahA class.
If my assumption is correct, add this to your base class:
public abstract List<int> MyNewMethod();
and in your sub class, add the word override to your method declaration.
Some code:
public interface MyInterface
{
void MyMethod();
}
public abstract class Base : MyInterface
{
public abstract void MyMethod();
}
public class SubA : Base
{
public override void MyMethod()
{
throw new NotImplementedException();
}
}
public class SubB : SubA
{
public void Foo() { }
}
Wrting this code and compiling works fine
public abstract class BlahA
{
}
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyList()
{
return new List<int>();
}
}
We will need a bit more of the code that isnt working
EDIT:
from comments you need to implement the method from interface in abstract class.
public interface IBlah
{
int GetVal();
}
public abstract class BlahA : IBlah
{
public int GetVal()
{
return 1;
}
}
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyList()
{
int i = GetVal();
return new List<int>();
}
}
Related
Let's say I have follow classes:
public class File { }
public class DB { }
public abstract class Validator<T>
{
T obj;
}
public class FileValidator : Validator<File>
{
}
public class DbValidator : Validator<DB>
{
}
In code I would like to have a ref to abstract class and create certain implementation depends on some condition (like factory).
public class Program
{
static Validator getValidator()
{
//some condition here
return new FileValidator<File>();
}
public static void Main()
{
Validator v = getValidator();
}
}
So, the compiler has a different opinion on this point
Using the generic type 'Validator' requires 1 type arguments
Is there any workaround in this case? Unfortunately, File and DB can't have common interface.
You need a non-generic interface or abstract base-class:
public interface IValidator { ... }
public abstract class Validator<T> : IValidator
{
}
Now you can return IValidator from your method:
static IValidator getValidator()
{
//some condition here
return new FileValidator<File>();
}
public static void Main()
{
IValidator v = getValidator();
}
However be aware that there is no way for the compiler to infer the actual type, which is based upon a runtime-decision.
Apart from this your implementing classes should not be generic at all, only your abstract class should be:
public class FileValidator : Validator<File>
{
}
public class DbValidator : Validator<DB>
{
}
I am defining a base class that has a method that returns type T. The classes the derive from this can return different types.
public abstract class BaseTransport
{
public abstract T Properties<T>();
}
public class Car : BaseTransport
{
public override T Properties<T>()
{
return new CarProperties();
}
}
public class Bike : BaseTransport
{
public override T Properties<T>()
{
return new BikeProperties();
}
}
If it makes a difference the return BikeProperties and CarProperties are both derived from BaseProperties.
Is this possible to do? Just trying to enforce the implementation of a method...
You don't want generic methods, you want a generic class:
public abstract class BaseTransport<T> where T : BaseProperties
{
public abstract T Properties();
}
public class Car : BaseTransport<CarProperties>
{
public override CarProperties Properties()
{
return new CarProperties();
}
}
public class Bike : BaseTransport<BikeProperties>
{
public override BikeProperties Properties()
{
return new BikeProperties();
}
}
I defined 3 interfaces:
public interface IManufacturerInput
{
}
public interface IManufacturerOutput
{
}
public interface IManufacturerApi<in S, out T>
where S : IManufacturerInput
where T : IManufacturerOutput
{
T Calculate(S);
}
And I defined a specific Manufacturer:
public class ManufacturerAInput : IManufacturerInput
{
}
public class ManufacturerAOutput : IManufacturerOutput
{
}
public class ManufacturerAApi : IManufacturerApi<ManufacturerAInput, ManufacturerAOutput>
{
public ManufacturerAOutput Calculate(ManufacturerAInput)
{
return null;
}
}
And In Main() I created a ManufacturerAApi, and try assign it to IManufacturerApi.
IManufacturerApi<IManufacturerInput, IManufacturerOutput> api = new ManufacturerAApi();
But it failed. The error message said (just abstract meaning):
Can't convert from ManufacturerAApi to IManufacturerApi<IManufacturerInput, IManufacturerOutput>
So is there any way I can make the assignment work? Thanks in advance.
What you are proposing isn't type safe. Let's change the names of your types to make the issue clearer:
public interface IPetFood { }
public interface IPetSound { }
public interface IPetCage<in S, out T>
where S : IPetFood
where T : IPetSound
{
T Feed(S s);
}
public class DogFood : IPetFood { }
public class CatFood : IPetFood { }
public class Bark : IPetSound { }
public class DogCage : IPetCage<DogFood, Bark>
{
public Bark Feed(DogFood input)
{
return new Bark();
}
}
And now suppose this is legal:
IPetCage<IPetFood, IPetSound> api = new DogCage();
Then we could do the following:
api.Feed(new CatFood()); //oops we've just given the dog some catfood.
The assignment will not work because S is contravariant, which means that any possible IPetFood passed into api.Feed would need to be a subtype of DogFood and you have the opposite; IPetFood is a superset of DogFood.
I have the following base class
class BaseClass
{
//Want to get the name of class A only, even if B is inherited from A
}
class A : BaseClass
{
}
class B : A
{
}
Can someone help me with getting the name of class A only from the base class.
Thanks
public abstract class BaseClass
{
public void GetInheritorName()
{
var nameIs = this.InheritorTellMeYourName();
}
protected abstract string InheritorTellMeYourName();
}
public class A : BaseClass
{
protected sealed override string InheritorTellMeYourName()
{
return typeof(A).Name;
}
}
public class B : A
{
}
Like someone mentioned in the comments, you shouldn't usually need anything like that...
using System;
public class Base
{
public Base()
{
}
public void M1()
{
}
public void M2()
{
}
public void M3()
{
}
}
public class Derived : Base
{
//this class should get only method 1
}
public class SecondDerived : Base
{
//this class should get only method 2 and method3
}
The requirement is : the base class contains the 3 methods M1, M2, M3.
The derived class should inherit only M1 and SecondDerived should inherit only M2 and M3.
How can this be done?
You cannot selectively inherit methods like this. A derived class automatically inherits all public methods of the base class. I suggest you to split the Base class into two classes:
public class Base1
{
public Base1()
{
}
public void M1()
{
}
}
public class Base2
{
public void M2()
{
}
public void M3()
{
}
}
public class First : Base1
public class Second : Base2
You cannot do it in this way. Inheritance implies an "IS A" relationship.
If SecondDerived would not have a M1() then it would not be compatible with a reference to a the class Base.
So maybe you shouldn't be using inheritance for whatever problem you're solving.
It is not possible to do what you want with inheritance.
It seems you have no intention of overriding, you simply want to "inherit" behavior from the base class selectively. You could do this using a "has a" relationship:
public class Base
{
internal Base() {} //mark constructor as internal so it can not be used outside your assembly if necessary
public Foo Mehtod1() {...}
public Foo Mehtod2() {...}
public Foo Mehtod3() {...}
}
Then simply do the following:
class A
{
private Base internalBase;
public A() { this.internalBase = new Base(); }
public Foo Method1() { return this.internalBase.Method1(); }
}
class B
{
private Base internalBase;
public A() { this.internalBase = new Base(); }
public Foo Method2() { return this.internalBase.Method2(); }
public Foo Method3() { return this.internalBase.Method3(); }
}
UPDATE: A possible alternative solution is to make your Base class methods virtual and override them all in your derived classes, throwing NotSupportedExceptions in those methods that you do not want the class to make available. I don't really like this solution but it has the advantage of not loosing the polyphormism inheritance gives you which might be useful if you have some core base functionality which all derived classes will share (in your example you seem to imply they wont).
It is possible by adding Obsolete attribute
public class A
{
public virtual void M1() { }
public void M2() { }
public void M3() { }
}
public class B : A
{
[Obsolete("You can not use this", true)]
public sealed override void M1()
{
}
}
public class C : B
{
public void Test()
{
// Will show error
base.M1();
}
}