I have Capture class as follow:-
namespace FrontEnd
{
public partial class Capture : Window, DPFP.Capture.EventHandler
{
public Capture()
{
InitializeComponent();
}
protected virtual void init()
{
try
{
if (null != cap)
cap.EventHandler = this;
else
SetPrompt("Cannot Use the Device right now");
}
catch { MessageBox.Show("Cannot Use the Device right now"); }
}
}
}
I have Enrollment class as follow:-
namespace FrontEnd
{
public partial class Enrollment : Capture
{
protected override void init()
{
base.Init();
Enroller = new DPFP.Processing.Enrollment();
UpdateStatus();
}
}
}
I am getting three errors in Enrollment.cs:
1 - Partial declarations of 'Enrollment' must not specify different base classes.
2 - 'Enrollment.init()': No suitable method found to override.
3 - The name 'UpdateStatus' does not exist in current context.
I dont know where the problem exactly lies. Perhaps it is in the inheritance. Moreover, i am using WPF, so it might be possible that i am supposed to change xaml code as well in order to undergo inheritance.
Kindly help in resolving these errors.
I am pretty sure that like JAVA, multiple inheritance is also not allowed in C# as well. And for the class Capture you are inheriting from two classes.
public partial class Capture : Window, DPFP.Capture.EventHandler
There can be a work around for that. You can use multilevel inheritance. That will resolve the issue.
1 - Partial declarations of 'Enrollment' must not specify different base classes.
My first guess on this case would be, that you have another class that is also partial and derives from a different class than Capture.
2 - 'Enrollment.init()': No suitable method found to override.
The problem here is, that the base classes Capture, Window and Eventhandler do not define the init() method so you cannot override it. If you remove the override from the init method you should be good to go.
Probably the issue is a simple typo. In this method you call a Init() method and the override is a lower case init
3 - The name 'UpdateStatus' does not exist in current context.
Same issue like in 2. There is no UpdateStatus in the base class.
I also think, that it is an issue to derive from multiple classes becaus like java , c# does not support multiple inheritance.
Somewhere else in your code you have another class called FrontEnd.Enrollment. This will derive from something other than Capture. This is causing error 1.
Because the compiler cannot tell what the correct base class is you will not be able to resolve any members of that class. This is causing errors 2 & 3
So correct the issue with the multiple base classes and you'll be fine.
Related
my question could seem strange.
I use a class to encapsulate a method to not have to build a class of the interface (it's a bit long to explain and i don't want to go too far).
I would to know if it was possible to "extend" a generic class by add partial to "extend" its generic part. The purpose is to keep the same name class, but by add one (or more in the future) generic type to have the possibility to encapsulate any method, then pass the object containing the function and that include this interface.
I need to have:
new Foo<string>()
new Foo<string, int>()
...
I 'successful' made this i think, but perhaps it will generate some bug i can't imagine right now, or perhaps it's not.. how to say a good way to program.
Example:
Original
// A class to encapsulate a method "without parameter"
partial Foo<T>: Interface
{
public Func<Interface, T> FooLambda{ get; set; }
public virtual object Run()
{
return ToRun(this);
}
}
The method i need to pass (from another class)
void FooToEncapsulate(Interface patt)
{
//--- My code using an object with the interface pattern
}
Add another generic Type to Foo
The part to "extend" Foo
partial Foo<T,Y>: Foo<Y>
{
public new Func<Interface, T, Y> FooLambda{ get; set; }
public T Param {get;set;}
public override object Run()
{
return this.ToRun(this, Param);
}
}
The other method i need to pass (from another class)
void FooToEncaspulate(Interface patt, int param)
{
//--- My code using an object with the interface pattern
//--- and "param"
}
I have no problem for the while with this code, and i know it's something that could be strange, must i forget to use this technic, or could i think it was thought to work also like this ? Must i think if it compiles that means it's ok ? Is there another way to proceed like this without create a new class, and extend in same time on the generic part ?
(Sorry for my english)
Thx.
Edit:
I thought by using partial that could be a good idea, because i would to keep the same name for my class. After have read an answer and comment from Enigmativity, i tried without partial, and i have no errors relating to the name of the class when i compile.
If i well understand, the fact to add generic parameter to a class makes that create as many class than as "variants" depending on the generic type. "Partial" is useful to split code on several files on a basic class.
Is partial could be useful on code split with the same number of generic type ?
You don't need the word partial to extend a class with a single generic type to have two generic types. They are in fact two distinct classed.
This works fine:
class Foo<T>
{
}
class Foo<T, Y> : Foo<Y>
{
}
Now, as said in the comments, the rest of your code is quite flaky. If you can clean up the code I could provide you with a more answer that will be of more use to you.
So, after some searching on stackoverflow and on google, I did find a few answers on my question but didn't know how to actually implement it for my own use. Which is why I will ask it again here.
Because of my inexperience with wpf combined with that I have never used anything closely resembling it, I made the mistake of going into creating a class diagram which now(maybe) has to be completely changed.
I wasn't sure of how I would go about creating custom blocks, but kind of mapped out the behaviour I needed.
close example of what I was trying to work towards
After realizing I made a mistake after finding out about userControls I tried implementing this into my project.
This is a test I made:
base class
namespace TestTest
{
public abstract partial class TestBase : UserControl
{
public TestBase()
{
InitializeComponent();
}
public virtual void doSomething()
{
Console.WriteLine("ITS WORKING");
}
}
}
Child class
namespace TestTest
{
public partial class TestExtend : TestBase
{
public TestExtend()
{
InitializeComponent();
}
}
}
after this I tried to add testExtended to a canvas but it gave the type error: testExtended is not a UIElement
class diagram (both UIStackBlock and UISideBlock inherit from UIBlock and UIBlock is free to inherit from anything)
I will still need the inheritance I created for filtering out certain blocks(mostly on UIOperations)
Now my real question is, how would I go about adding multiple xaml files and defining the look of them in there without getting in the way of the current inheritance hierarchy I have in place. Or is this even achievable?
Again, I know this is a duplicate.
You can either write DataTemplates that are adjusted per DataType.
Or, you could leave behind inheriting from UserControl(just Inherit from Control or ContentControl) and make each of them a custom control with its unique style and Template.
In building a class structure, I would like to have derived classes potentially posses derived member classes. For example:
class GamePiece
{
}
class Checker : GamePiece
{}
class ChessMan : GamePiece
{}
class Game
{
protected GamePiece _piece;
}
class Checkers : Game
{
Checkers(){ _piece = new Checker(); }
}
class Chess : Game
{
Chess(){_piece = new ChessMan();}
}
This is seriously oversimplified, but makes the actual point. Now assuming that there are events and such, I would like to attach the common ones in the base class constructor, and the specialized ones in the derived constructor. For example both a checker and a chessman might have a "captured" event and a "moved" event. I would like to attach them in the base constructor. However, events that would be specific such as "castled" or something like that would be attached only in the specific constructor.
The problem I have is that the base constructor seems to only be able to run BEFORE the derived constructor. How do I effect this such that I get to actually instantiate the "gamepiece" before I call the base "Game" constructor to attach to events.
My gut suggests that this is better handled by removing that functionality from the constructor and simply having an "Attach()" member function and handling it there. However, I want to make sure I am going in the right direction, since it would seem there should be a way to do it in the constructor.
You can try injecting it from child to parent, like this:
abstract class Game {
protected GamePiece _piece;
protected Game(GamePiece piece)
{
_piece = piece;
// do the common work with pieces here
}
}
class Checkers : Game
{
public Checkers(Checker piece) : base(piece)
{
// piece-specific work here
}
}
If you need more complicated work done than just instantiating, you could create a static factory method that did all the work, and just call that when invoking the base constructor. This does change your Game implementation slightly in ways that change how you instantiate it, but that can be solved by using a factory object anywhere you need a new Game.
Edit: I actually realized the original code-sample I posted wasn't correct; there was no way to reference the Checker piece (as a Checker) in the Checkers class. But if you inject the Checker piece into that as well, the problem is solved and you have your reference. This is dependency injection, by the way.
You could try the Template design pattern:
class Game
{
Game() { Init(); }
protected virtual void Init() {}
}
Now you can insert generic event handling in the Game Init method and override it with concrete handling logic in the descendants.
I would like to override a virtual base class function on the fly and use this overriden method in application. If I write a basic example class:
partial Class myBase
{
public myBase() {}
public virtual void DoStuff()
{
throw new Exception("this function is not overriden");
}
}
partial Class myDeriv : myBase
{
public myDeriv() {}
}
And now I would like to override myDeriv.DoStuff on the fly.
So I create a string code block and compile it using
CSharpCodeProvider.CompileAssemblyFromSource
method. After writing this dll to disk I tried to load it using
Assembly.LoadFrom("onTheFly.dll");
But application fails to find this overridden function. If you have any other better solutions I'm open to them also.. I just need to override functions on the fly..
Thank you all!
I think it is not possible seems to be be caused by the same rule as described here or here
Quote from the original:
"You cannot have two partial classes referring to the same class in two
different assemblies (projects). Once the assembly is compiled, the
meta-data is baked in, and your classes are no longer partial. Partial
classes allows you to split the definition of the same class into two
files."
or
"You cannot use the partial keyword to split the code for a class
between projects. The partial keyword is a compiler trick; the
compiler will output one single class out of the parts it finds, so
all parts of the class must exist with the same binary file. Once the
class is compiled, there is no trace left of it being a partial class.
If you want to extend an existing class you will either need to
inherit it (if it is not sealed), or create your own new class that
contains the classes that you wish to combine information from."
Do you know the answer to following question?
Let us say, it MyMethod() is declared
as partial method in MyPartialClass in
MyPartialClass.cs. I have also
provided body of MyMethod() in
MyPartialClass in MyPartialClass2.cs.
I use a problem without answer“Magic”
code generator which has actually
generated MyPartialClass.cs, let us
say based on some DB schema. Some
innocent guy changes schema for good
and then runs “Magic”.
MyPartialClass.cs is re-generated but
with MyMethod2() instead of MyMethod()
declaration. Think of me. I am
thinking that I have implemented
MyMethod() which is used by “Magic”
but in reality, “Magic” is using
MyMethod2(). C# compiler does not tell
me that I have partial method
implemented without any declaration
and my implementation is not used at
all!
Any solution?
I think it is a problem without an answer.
EDIT I got an answer :-). I had a typo in my code and that is why compiler was not flagging error. Jon already has pointed that out.
You should get error CS0759. Test case:
partial class MyClass
{
partial void MyMethod()
{
Console.WriteLine("Ow");
}
}
partial class MyClass
{
partial void MyMethod2();
}
Compilation results:
Test.cs(6,18): error CS0759: No defining declaration found for implementing
declaration of partial method 'MyClass.MyMethod()'
Does that not do what you want it to?
In short, no; that is the point of partial methods - the declaring code doesn't need to know whether an implementation is provided or not.
Of course - you could just not declare the partial method: consume it assuming it exists; if you don't provide it, the compiler will complain of a missing method.
There is a hacky way to check at runtime (with partial methods), which is to have the other half update a ref variable:
partial void Foo(ref chk);
partial void Foo(ref chk) { chk++;}
(and verify it changes) - but in general, partial methods are designed to not know if they are called.
Another approach is a base-class with an abstract method - then it is forced by the compiler to be implemented.
This is the whole purpose of partial methods. If the method is not implemented, it is removed without a trace, and without a warning.
One solution to this type of problem would be to use a double derived pattern in your code generation. This is used extensively by DSLTools and is quite powerful.
Write the following code by hand :
public class MyClassBase
{
public abstract void MyMethod();
//Put all other methods required by the class here.
}
public partial class MyClass : MyClassBase
{
//This class is entirely empty!
}
Generate the following code in magic.
public partial class MyClass
{
public void MyMethod(){}
}
If someone fails to implememnt MyMythod() in the generated code, you will get a compiler error.