Hi after failing with inheritance (it got complicated) I stumble onto Generics. I am new to coding in general and C# is my first language.
I have two classes CIMTDXInput, RMTTDXInput which have the same properties but those properties have slightly different members. For those same 3 properties between the two, I want to just check if they are null or not.
So I wrote this:
public static TDX2KlarfResult CheckCIMTDXInput <T> (T input, TDX2KlarfResult result) where T: CIMTDXInput, RMTTDXInput
{
if (input.ToolContext == null)
{
Logger.Warn("Missing Tool Context Skipping the file");
result.errorType = "Warning";
result.errorMessage = "Missing Tool Context";
result.errorSubject = ErrorCategory.MISSING_TOOL_CONTEXT;
result.success = false;
return result;
}
if (input.SCContext == null)
{
Logger.Warn("Missing Context Skipping the file");
result.errorType = "Warning";
result.errorMessage = "Missing Context";
result.errorSubject = ErrorCategory.MISSING_CONTEXT;
result.success = false;
return result;
}
if (input.WaferContainer == null)
{
Logger.Warn("Missing Wafer Container Skipping the file");
result.errorType = "Warning";
result.errorMessage = "Missing Wafer Container";
result.errorSubject = ErrorCategory.MISSING_WAFER_CONTAINER;
result.success = false;
return result;
}
return result;
}
However the code won't compile. I thought you can assign as many classes as you want to the "T"?
Again both classes have ToolContext, SCContext, and WaferContainer. Instead of writing a function for each, I thought it would be elegant to write one for both. I also have additional inputs in the future to add so I'd like to not need to write this function each time
The why
So when you say this:
public void MyMethod<T>(T value) where T: ClassA, ClassB
You're saying that T should be derived from both ClassA and ClassB. Now it would work in this scenario:
public class ClassA
{
public int SomeProperty { get; set; }
}
public class ClassB : ClassA
{
}
public class ClassC : ClassB
{
}
MyMethod<ClassB>(classBValue); // ClassB is a ClassB and is derived from ClassA
MyMethod<ClassC>(classCValue); // ClassC is derived from ClassB, and is indirectly derived from ClassA
But this won't work:
public class ClassA
{
public int SomeProperty { get; set; }
}
public class ClassB
{
public int SomeProperty { get; set; }
}
Because it requires a ClassC that looks like this:
public class ClassC : ClassA, ClassB
{
}
And this could will cause a compile time error because C# doesn't support multiple inheritance. That is to say that, while you can have a class derived from a class that itself is derived from another class, you can't create a class that directly derives from two classes.
The solution
What you can do is declare an interface:
public interface ISomeInterface
{
int SomeProperty { get; set; }
}
And have both the classes implement it:
public class ClassA : ISomeInterface
{
public int SomeProperty { get; set; }
}
public class ClassB : ISomeInterface
{
public int SomeProperty { get; set; }
}
Then if we change our method signature to require that the class implements our interface:
public void MyMethod<T>(T value) where T: ISomeInterface
Then within the method we can access the SomeProperty property of T:
public void MyMethod<T>(T value) where T: ISomeInterface
{
value.SomeProperty *= 2;
}
Try it online
you need to use an interface
public interface IInput
{
public string ToolContext {get;set;}
public string SCContext {get;set;}
public string WaferContainer {get;set;}
}
public class CIMTDXInput:IInput
{
public string ToolContext {get;set;}
public string SCContext {get;set;}
public string WaferContainer {get;set;}
.... another properties
}
public class RMTTDXInput:IInput
{
public string ToolContext {get;set;}
public string SCContext {get;set;}
public string WaferContainer {get;set;}
.... another properties
}
and your method should be
public static TDX2KlarfResult CheckCIMTDXInput<T>(T input, TDX2KlarfResult result) where T : IInput
or you can get without a generic in this case , would be enough
public static TDX2KlarfResult CheckCIMTDXInput(IInput input, TDX2KlarfResult result)
Related
I dont think what I am trying to do is possible; is there a way to actually make this work?
There is a Base class from which a variety of different classes are derived. Derived classes can be generic or not; instances of the derived classes are added to a collection of type Base in WindowViewModel. The Base class has a collection of Options that are accessed by the WindowViewModel.
The issue is: the IOption interface declares a return type of Func<object, bool> MyFunc but the return type of MyFunc needs to be Func<T, bool> for the generic class method RunIt() and for the assignment in MyClass to work. I could make the IOption generic, but then the Base class would need to be generic, and then the WindowViewModel.ViewModels would also need to be redefined somehow. I dont want to make the Base generic as introducing generics there just makes everything else a real mess.
Question: is there a different way to declare MyFunc in IOption without using generics to allow assignment of Func<T,bool> in MyClass ?
public interface IOption
{
public string Description {get; set;}
public Expression<Func<object,bool>> MyFunc { get; set; }
}
public class Option : IOption
{
public string Description {get; set;}
public Expression<Func<object,bool>> MyFunc { get; set; }
}
public abstract class Base
{
public abstract ObservableCollection<Option> Options { get; set; }
public abstract Option SelectedOption { get; set; }
public abstract void RunIt();
}
public class Generic<T> : Base
{
private DBContext _context;
public override ObservableCollection<Option> Options { get; set; }
public override Option SelectedOption { get; set; }
public Generic()
: base()
{
Options = new ObservableCollection<Option>();
}
public override void RunIt()
{
var result = _context.Set<T>().Where(SelectedOption?.MyFunc);
// process result
}
}
public class MyClass : Generic<MyType>
{
public MyClass
: base()
{
Func<MyType,bool> expression = t => t.MyDescription = "Hello World";
Options.Add(new Option("Hi", expression)); // fail to compile type mismatch
SelectedOption = Options.First();
}
}
public class Special : Base
{
// do something else
}
public class WindowViewModel
{
public WindowViewModel ()
{
MyViewModels = new ObservableCollection<Base>();
MyViewModels.Add(new Special());
MyViewModels.Add(new MyClass());
}
public ObservableCollection<Base> MyViewModels {get; set;}
public Base SelectedViewModel { get; set; }
public void DoRunIt()
{
SelectedViewModel.RunIt();
}
}
one of the things I did try that compiles but throws runtime exception when used, is
Func<MyType,bool> expression = t => t.MyDescription = "Hello World";
MyFunc = t => expression((MyType)t);
There is a way to do this. It uses the ability for all delegates (Func<MyType, bool> is a delegate) to be cast to Delegate.
You'd change IOption and Option to this:
public interface IOption
{
public string Description { get; set; }
Func<T, bool> GetMyFunc<T>();
}
public class Option : IOption
{
string description;
private Delegate expression;
public Option(string description, Delegate expression)
{
this.description = description;
this.expression = expression;
}
public string Description { get; set; }
public Func<T, bool> GetMyFunc<T>() => (Func<T, bool>)this.expression;
}
Then MyClass works as expected (except for the other syntax error in your code).
You then just need to change RunIt on Generic<T> to this:
public override void RunIt()
{
var result = _context.Set<T>().Where(SelectedOption?.GetMyFunc<T>());
// process result
}
Question: is there a different way to declare MyFunc in IOption without using generics to allow assignment of Func<T,bool> in MyClass ?
No, I don't believe that is possible. You can have generic methods in a non generic type, though.
However, there is an option that might work for you.
You state
I dont want to make the Base generic as introducing generics there just makes everything else a real mess.
How about having both?
public abstract class Base<T>
{
public abstract ObservableCollection<Option<T>> Options { get; set; }
public abstract Option<T> SelectedOption { get; set; }
public abstract void RunIt();
}
public abstract class Base : Base<object> { }
I need to have a list where all items extend class A and implement interface I. Additionally class A might be multiple parents up in the hierarchy.
If all the classes were direct descendants of class A I could just use an Abstract class that implements I as the generic and use that, but my use case doesn't allow for this.
is there a way to tell a List that its elements must both extend class A and implement interface I ? List<A,I> ? If not is there another way around this?
Example Code:
public class A
{
// Class belongs to a third party library
}
public class B : A
{
// Class belongs to a third party library
public string Text{ get; set; }
}
public class C : A
{
// Class belongs to a third party library
public string Other{ get; set; }
}
interface I
{
// Belongs to me
bool shouldSend();
string getName();
string getValue();
}
public class MyClass : B, I
{
public string Name{ get; set; }
public function myClass(ObjectWithName obj)
{
Name = obj.Name;
}
public string getValue()
{
return Text;
}
public bool shouldSend()
{
return true;
}
}
public class MyClass2 : C, I
{
public string Name{ get; set; }
public function myClass(ObjectWithName obj)
{
Name = obj.Name;
}
public string getValue()
{
return Other;
}
public bool shouldSend()
{
return true;
}
}
public class mainActions
{
// Here is where I need the list to use both restrictions
public List<A,I> myList;
// The class I need to use these things in
public function mainActions(List<ObjectWithName> elements)
{
ThirdPartyCollection TPC = new ThirdPartyCollection();
foreach(var el in elements)
{
MyList.Add(new MyClass(el));
MyList.Add(new MyClass2(el));
// TPC.Add accepts implementations of A here
TPC.Add(MyList.ElementAt(MyList.Count - 1));
TPC.Add(MyList.ElementAt(MyList.Count - 2));
}
}
public function doThisLater()
{
foreach(var el in myList)
{
if(el.shouldSend())
{
// I need an implementation of I here
doSomethingElse(el.getName(), el.getValue());
}
}
}
}
EDIT: For anyone coming in search of an answer here in the future, it doesn't seem to be possible. Instead I used #servys answer and made a new list to hold my sub class objects:
public class MyList<T> : List<T> where T : A, I
{
}
Then I kept different lists for each subclass:
protected MyList<MyClass> MCList = new MyList<MyClass>();
protected MyList<MyClass2> MCList2 = new MyList<MyClass2>();
When you specify generic constraints you can specify as many as you want, and all of them must be met, so you can simply add a generic constraint of A and I to your type, and a type has to meet both of those constraints to be a valid generic argument.
public class ClassThatNeedsABetterName<T> : List<T>
where T : A, I
{ }
I need to create a dll file which contains all the interfaces of the classes but doesn't contain any class.
Because I use these interfaces for a lot of classes it's must be like that:
public interface IClassA
{
string Word { get; }
}
public interface ITest<TClassA> where TClassA : IClassA
{
TClassA A { get; }
}
Example of two classes that implements these interfaces the way I want:
public class ClassA : IClassA
{
public string Word
{
get;
private set;
}
public string Sentence
{
get;
private set;
}
public ClassA(string word, string sentence)
{
this.Word = word;
this.Sentence = sentence;
}
}
public class Test : ITest<ClassA>
{
public ClassA A
{
get;
private set;
}
public Test(ClassA a)
{
this.A = a;
}
}
And I want to do something like that in the main program:
public static void Main(string[] args)
{
ClassA a = new ClassA("hey", "hey world!");
Test t = new Test(a);
Print((ITest<IClassA>)t);
}
public static void Print(ITest<IClassA> t)
{
Console.WriteLine(t.A.Word);
}
But this casting: (ITest<IClassA>)t makes a run time error.
How can I solve it?
thanks!
You should declare Test as
public class Test : ITest<IClassA>
instead of ITest<ClassA>.
Or declare ITest<TClassA> to be covariant on TClassA:
public interface ITest<out TClassA> where TClassA : IClassA
The Test-class implements the concrete ClassA (public class Test : ITest<ClassA>).
So you're trying to cast an ITest<ClassA> to ITest<IClassA> which obviously fails.
If you let the Test-class implement IClassA, the cast works:
public class Test : ITest<IClassA>
{
public IClassA A
{
get; private set;
}
public Test(IClassA a)
{
this.A = a;
}
}
I have the following classes and interfaces:
public interface IThing
{
string Name { get; }
}
public class Thing : IThing
{
public string Name { get; set; }
}
public abstract class ThingConsumer<T> where T : IThing
{
public string Name { get; set; }
}
Now, I have a factory that will return objects derived from ThingConsumer like:
public class MyThingConsumer : ThingConsumer<Thing>
{
}
My factory currently looks like this:
public static class ThingConsumerFactory<T> where T : IThing
{
public static ThingConsumer<T> GetThingConsumer(){
if (typeof(T) == typeof(Thing))
{
return new MyThingConsumer();
}
else
{
return null;
}
}
}
I'm getting tripped up with this error: Error 1 Cannot implicitly convert type 'ConsoleApplication1.MyThingConsumer' to 'ConsoleApplication1.ThingConsumer<T>'
Anyone know how to accomplish what I'm attempting here?
Thanks!
Chris
If you make ThingConsumer<T> an interface rather than an abstract class, then your code will work as is.
public interface IThingConsumer<T> where T : IThing
{
string Name { get; set; }
}
Edit
One more change needed. In ThingConsumerFactory, cast back to the return type IThingConsumer<T>:
return (IThingConsumer<T>)new MyThingConsumer();
The compiler is stumbling over the conversion from MyThingConsumer to ThingConsumer<T> even though T:IThing and MyThingConsumer:Thingconsumer<Thing> and Thing:IThing. Which is quite a few hoops for it to jump through!
The code works if you use return new MyThingConsumer() as ThingConsumer<T>; instead of a direct cast. You know the result will never be null, and the compiler is happy because it is guaranteed a return value of the right type at runtime.
Edit:
Here is the full code I used for testing (in Snippy):
public interface IThing
{
string Name { get; }
}
public class Thing : IThing
{
public string Name { get; set; }
}
public abstract class ThingConsumer<T> where T : IThing
{
public string Name { get; set; }
}
public class MyThingConsumer : ThingConsumer<Thing>
{
}
public static class ThingConsumerFactory<T> where T : IThing
{
public static ThingConsumer<T> GetThingConsumer()
{
if (typeof(T) == typeof(Thing))
{
return new MyThingConsumer() as ThingConsumer<T>;
}
else
{
return null;
}
}
}
...
var thing = ThingConsumerFactory<Thing>.GetThingConsumer();
Console.WriteLine(thing);
You need to define your class like this I believe:
public class MyThingConsumer<Thing> : ThingConsumer
The reason is that ThingConsumer is already typed in its definition with this: where T : IThing
Now, you can make the call return new MyThingConsumer<T>();.
This should in turn match the expected return type of ThingConsumer<T>
EDIT
Sorry for the confusion, here is what should work:
public class MyThingConsumer<T> : ThingConsumer<T> where T : IThing
and
return new MyThingConsumer<T>();
I want to create a class that could hold any of a number of same type of classes. For example lets says I have a base class like follows:
public class BaseClass
{
public string MyBaseString
{
get;
set;
}
}
And then I have a few derived classes like this:
public class DerivedClass : BaseClass
{
public MyDerivedClassString
{
get;
set;
}
}
public class DerivedClass2 : BaseClass
{
public MyDerivedClass2String
{
get;
set;
}
}
Now I would like a class that accepts one of these implementations and does stuff with it. Here is the only thing I can think of, but there must be a better way:
public class ClassA
{
public object MyClass
{
get;
set;
}
public ClassA (object myClass)
{
MyClass = myClass;
if (object is BaseClass)
{
//do something
}
else if (object is DerivedClass)
{
//do something specific to derived class
}
else if (object is DerivedClass2)
{
//do something specific to derived class 2
}
}
}
CLARIFICATION: The specific goal I am trying to accomplish is to use ClassA as a container class for various implementations of the BaseClass. The business goal I am trying to accomplish is to create a Legend object which might use multiple color schemes (i.e. a Mono Color Ramp, Multi Color Ramp, etc). So I would like the Legend class to contain the ColorScheme that is being used, but still have access to that color scheme's unique properties for modification later on.
CLARIFICATION 2 Based on the wide array of responses I got, I thought I'd provide an exact replication of what I'm trying to do:
public class BaseColorScheme
{
List<Color> _colors = new List<Color>();
public List<Color> Colors
{
get
{
return _colors;
}
set
{
_colors = value;
}
}
}
public class SingleColorScheme : BaseColorScheme
{
public Color MidColor
{
get;
set;
}
public SingleColorScheme( Color midColor, int numberOfClassifications )
{
Colors = CreateMonoColorRamp( midColor, numberOfClassifications );
}
}
public class MultiColorScheme : BaseColorScheme
{
public Color StartColor
{
get;
set;
}
public Color EndColor
{
get;
set;
}
public Color MidColor
{
get;
set;
}
public MultiColorScheme( Color startColor, Color endColor, Color midColor )
{
StartColor = startColor;
EndColor = endColor;
MidColor = midColor;
Colors = //do something to define multi color scheme
}
}
Then I would have a Legend Class that would be something like
public class Legend
{
public object ColorScheme
{ get; set; }
public Guid LegendId
{ get; set; }
public Legend(object colorScheme)
{
ColorScheme = colorScheme;
}
}
Finally I might have a form that sits on top of the legend that displays the properties of the various color schemes based on which type of color scheme it is. Hopefully that helps clarify a bit.
public class ClassA<T> where T : BaseClass
{
public T MyClass { get; set; }
public ClassA(T myClass) { MyClass = myClass; }
}
Beyond that, define the common interface of the class hierarchy either as an interface or as methods (concrete, abstract, or virtual) within the base class. Then you can be assured all derived classes have such method / properties and can use them within your generic wrapper.
Instead of letting ClassA perform whatever needs to be done, you can use polymorphism and let the classes do it to themselves.
Simply declare a virtual method in the base class, have it do whatever you need it do so, and then override this method in the subclasses. In the method in ClassA, you just need to call that method on the object you receive as a parameter - without having to care about the specific type.
If you need to access different properties based on which derived class is passed something like this should help:
public class ClassA<T> where T : BaseClass
{
public T MyClass { get; set; }
public ClassA(T myClass) { MyClass = myClass; }
public void DoStuffToMyClass()
{
if(MyClass is BaseClass)
{ // do base class stuff }
else if(Myclass is DerivedClass)
{ // do DerivedClass stuff }
else if(MyClass is DerivedClass2)
{ // do DerivedClass2 stuff }
}
}
This gives you the type saftey to ensure you at least have the BaseClass object, and possibly a derived class.
The answer is polymorphism, let the object do it themselves.
public class BaseClass
{
public string MyString { get; set; }
public virtual string DoIt()
{
return "I'm Base Class";
}
}
public class DerivedClassA
{
public override string DoIt()
{
return "I'm Derived Class A";
}
}
public class DerivedClassB
{
public override string DoIt()
{
return "I'm Derived Class B";
}
}
....
public ClassA (BaseClass myClass)
{
MyClass = myClass;
MyClass.DoIt();
}
.....
ClassA x1 = ClassA(new BaseClass()) // calls BaseClass.DoIt()
ClassA x2 = ClassA(new DerivedClassA()) // calls DerivedClassA.DoIt()
ClassA x3 = ClassA(new DerivedClassB()) // calls DerivedClassB.DoIt()
whenever you catch yourself acting differently based on the run-time type of the object, you are dealing with code that breaks OO principles, i.e. a class that does not respect the base class contract.
Can you use virtual methods?
public abstract class BaseClass
{
public abstract void DoStuff();
}
public class DerivedClass1 : BaseClass
{
public override void DoStuff()
{
...
}
}
public class DerivedClass2 : BaseClass
{
public override void DoStuff()
{
...
}
}
Without generics:
public class ClassA
{
public BaseClass MyClass
{
get;
set;
}
public ClassA (BaseClass myClass)
{
MyClass = myClass;
myClass.DoStuff();
}
}
or with generics:
public class ClassA<T> where T : BaseClass
{
public T MyClass { get; set; }
public ClassA (T myClass)
{
MyClass = myClass;
myClass.DoStuff();
}
}
Keep it simple: polymorphism
Hopefully your objects have a common interface, something like:
class Base {
public virtual void DoSomething() { /* Default implementation */ }
}
class Derived1 : Base {
public override void DoSomething() { /* Implementation specific to this type */ }
}
class Derived2 : Base {
public override void DoSomething() { /* Another implementation specific to this type */ }
}
Or maybe they implement a common interface. So hopefully your consuming class can hold the most general representation of your inputs as possible and invoke code as such:
class Dependent {
public Dependent(Base instance) {
instance.DoSomething();
}
}
So your Dependent class doesn't really are whether it has a derived type or a base type.
Not quite as simple: visitor pattern
Sometimes polymorphism doesn't really work, which is particularly the case if you need to access the specific members of your derived classes, and those members aren't in the base class. Visitor pattern works well in this case, especially if you have a fixed, well-defined graph of objects.
public interface IVisitor<T> {
T Visit(Base x);
T Visit(Derived1 x);
T Visit(Derived2 x);
}
class Base {
public virtual T Accept<T>(IVisitor<T> visitor) { visitor.Visit(this); }
public string BaseString { get; set; }
}
class Derived1 : Base {
public override T Accept<T>(IVisitor<T> visitor) { visitor.Visit(this); }
public string Derived1String { get; set; }
}
class Derived2 : Base {
public override T Accept<T>(IVisitor<T> visitor) { visitor.Visit(this); }
public string Derived2String { get; set; }
}
So Derived1 and Derived2 have a different set of properties, and if you need to get to those properties without a runtime type-checking, implement a visitor:
class DefaultStringVisitor : IBaseVisitor<string> {
public string Visit(Base x) { return x.BaseString; }
public string Visit(Derived1 x) { return x.Derived1String; }
public string Visit(Derived2 x) { return x.Derived2String; }
}
class Dependent {
public Dependent(Base x) {
string whatever = x.Accept<string>(new DefaultStringVisitor());
}
}
So the visitor pattern gives you access to your derived object's members without a type-check. Its a somewhat inflexible pattern (i.e. need to know which objects to visit up front), but it might work for your needs.