I am stuck in a doubt...
I have an aspx page that has to inherit 2 classes:
1) Class expand : Page, IPostBackEventHandler
2) mydll.classX
But I am unable to accomplish the same.
Any workaround that can be suggested would be really helpful...
Thanks
You can't inherit from more than one class in .NET. You should look into one of these methods to solve your problem:
Make your classX inherit Page. Now your new class can inherit classX, and it'll be a child class of both clasX and Page;
Use interfaces instead of classes. You're kinda doing that already, with IPostBackEventHandler. You can't inherit from multiple classes but you can implement as many interfaces as you want;
Use composition - that is, your class has a member that is of type classX. Read about object oriented design patterns, that may help you accomplish what you want in elegant ways that make things easier. See this question: What is composition as it relates to object oriented design?
Since you can't do multiple inheritance in what looks to be C# I think you will have to:
a. Replicate the public interface of myDll.classX and forward calls to an instance of myDll.classX
or
b. Make myDll.classX inherit Page, but that may not really be an option.
Without more information on what you are trying to accomplish, I can't really see any other options.
Related
I am trying to teach myself design patterns. I was reading about the Decorator pattern, and there's one thing that I can't quite get. Here is an example found on wikipedia:
Let's say I want to implement a window that can scroll both horizontally and vertically, I would do this:
Window win=new Window();
win=new HorizontalScrollBarDecorator(win);
win=new VerticalScrollBarDecorator(win);
Since win is a reference of type Window, I can call neither drawHorizontalScrollBar() nor drawVerticalScrollBar() (apart from the code in the concrete decorator itself), and of course it would not make sense to change the reference type.
So, how can I "add new behaviour" (I.E. implement new methods) with decoration?
Nevermind, I think I got it:
Through decoration you can't "add new behaviour" as I had interpreted it (drawHorizontalScrollBar() is not visible from outside the HorizontalScrollBarDecorator class, which means it's probably called by its draw() method), BUT you can extend methods of the base class.
It's like extending the base class, overriding the draw() method and then calling super.draw();, with the difference that you can apply multiple decorators, but you can't inherit from multiple classes.
I just started using Caliburn.Micro and I've noticed in all the examples that the methods are all public. I decided to test this by adding a button with:
x:Name="CloseMainWindow"
In my VM I added a method:
private void CloseMainWindow()
{
TryClose();
}
When I click the button, nothing happens and I don't hit the breakpoint, but if I change the method to public it works.
I can't see this being the best way to do this.
Would creating ICommand properties for all the methods be an acceptable solution?
Edit: I just read the answer to the question immediately above, there is not and never will be ICommands in Caliburn.Micro. So my original question still needs an answer, why does everything have to be public in the VM and is this safe?
I don't know what you mean by "is this safe?". Safer than what?
Anyway, Caliburn.Micro could have been designed to allow its conventions to bind to private methods, but that has a couple of drawbacks. First, it wouldn't work in partial-trust environments, like Silverlight or XBAPs or sandboxed plugins. You need full trust to use Reflection to access private members, and Caliburn.Micro is designed to be able to run in partial-trust (it does support Silverlight, after all).
But a bigger reason is that it would violate encapsulation. These are methods that you intend to be called from outside the class. (The view is a separate class, after all; you'd have to make the viewmodel method public if you were wiring it up yourself in the code-behind.) There's a word for "I intend to call this from outside my own class" in the language specification, and that's public. If you set up some magic that calls private methods from outside the class, you're violating both encapsulation and the Principle of Least Astonishment, because that's not what private means.
If you really want to be able to bind to private methods, you can customize the conventions. But it would make your code much harder to understand, so I wouldn't recommend it unless you can come up with a really good justification.
I want to extend or copy the PopUpManager class to add the ability to keep track of the number of windows.
I just want to add a simple windowCount++ when a window is added and windoCount-- when it's removed.
the problem is PopUpManager is a Singleton class... I wasn't able to make it work properly by extending it. And now I have tried to copy the code from the PopUpManager.as file and just add my variable to the end of its functions. It doesn't seem to be working though since it says my properties are undefined even though they are declared above the constructor.
I am thinking I would have to make a copy of the PopUpManagerImpl.as since that's wehre it seems much of the business resides (PopUpManagerImpl extends EventDispatcher implements IPopUpManager) would that allow me to have access to the variable? and should I ignore the manager and just put it in the implementation class?
here is a link about Using the Flex Singleton register, which helped me out when finding myself in the same situation.
I hope you can inspire from that too.
You likely didn't declare yours properties as static. The PopUpManager uses all static methods - this is why working with it you use syntax like:
PopUpManager.createPopUp(...
instead of
var popUpManager:PopUpManager = new PopUpManager();
popUpManager.createPopUp(...
This means that any variables declared in the PopUpManager need to also be static so as to be accessible at the class level.
public static var windowCount:int
I want to inherit more than one class is there any method?
For instance in login.aspx page:
<%# page language="c#" codefile="nishant.aspx.cs" autowireup="true" inherit="nishant"%>
now code behind file
nishant.aspx.cs:
class nishant
{
//code...
}
class bill
{
//code.....
}
Now, I want to inherit bill class then how I will ?
.NET does not support multiple inheritance, this includes asp.net, so no, this is not possible.
You can have your nishant class inherit from the bill class or the other way around, if you want to share functionality. You page can then inherit from the inheriting class and access the functionality of both.
Another option is to inherit from one class and implement an interface (or several interfaces), but the fact that you can implement more than one interface is not the same as multiple inheritance.
There are other things that can be done, depending on what exactly you are trying to achieve (I am primarily thinking about composition versus inheritance).
Multiple inheritance is not allowed. The only way is:
public class Bill : Page
{ }
public class Nishant : Bill
{ }
But rather you should think about your design. Such approach is usually not needed.
No. By nature, .Net allows only single inheritance. At best you could implement an interface, but you will still have to have the code in your nishant class or extract the functionality into your bill class and make function calls.
Although in the case you mention, this is not actually multiple inheritance. Your nishant class must be of type System.Web.UI.Page. So if you create a library with a "bill class", you can then inherit it.
public class bill : System.Web.UI.Page
{
// Your custom code
}
///
public class nishant : bill
{
}
.NET does not support multiple inheritance (one class that inherit from two or more classes).
However you can have as many parent class as you want. Have a look at the decorator pattern.
Or use interfaces, you can have more than one.
In short: implement interfaces and use extension methods
Implementing interfaces might be the way to go, really depends on the functionality you want to inherit.
You can read about inheritance and interfaces here: http://msdn.microsoft.com/en-us/library/ms973861.aspx
When you implement interfaces, but don't want to duplicate the same code into every class that implements a certain interface, you can also write extension methods for the interfaces. Read more about extension methods here: http://msdn.microsoft.com/en-us/library/bb384936.aspx
Does anyone know if it is possible to inject into a regular as3 (non mxml) class? I've tried with limited success.
Thanks
Could you be more specific? There's no difference between an "MXML" class and a class defined in ActionScript, it's just different ways of writing the same thing.
All that is needed for injection to work is a source property that is bindable and a destination property that is public (either a public setter or a public instance variable). If those two requirements are met and the code compiles it should work.
Look at the code for the example application you can find here: http://code.google.com/p/mate-examples/wiki/DocumentBasedExampleIntro and you will find a ton of injectors that target classes not defined using MXML (look for injectors targeting classes whose names end in "Model" especially). You can also find countless examples in the Mate forums.