Malfunction Posted March 12, 2004 Posted March 12, 2004 (edited) I have a set of customized controls. E.g. they extend "RadioButton", "TextBox" and "CheckBox". Now I have added a couple of methods that these controls have in common (same implementation, too). Since they (my customized controls) can only inherit one type I wanted to put these common methods into an interface. That way I would still have to copy the methods' body into every control but that would at least guarantee that these controls implement the common methods. Now the problem: Some of the common methods are private or protected or even have the override modifier but my interface tells me it wont let me use these :( Should I just delete the interface and copy the methods to each control - not letting anyone know that they have these methods in common? Or is there a chance for my interface to survive? edit: "Explicit interface member implementation" doesn't help either... Edited March 12, 2004 by Malfunction Quote Debug me...
Administrators PlausiblyDamp Posted March 12, 2004 Administrators Posted March 12, 2004 Interfaces by definition contain public functionality, they are designed to provide a consistant public interface to a series of components. Basically you can't put protected or private members into and interface. However if your controls are in a single classlibrary you could mark the interface as being internal (friend in vb) and in each of the classes mark their implementation as also being internal. This way the interface can be used within the DLL but not from outside the DLL. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted March 12, 2004 *Experts* Posted March 12, 2004 I'd second Plausably's solution. If you just used the same named method names in each class, but not through an interface, then you lose the ability to cast an instnace of your control to the interface and call one of your new methods. Obviously that's only a benefit if you need that functionality, but I would think you would want it at some point. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Heiko Posted March 12, 2004 Posted March 12, 2004 You need not copy the functionality. You can programm a common HelperClass that is called by all classes. Public Class A implements IX Private _IXHelper as new IXHelper Private Sub Soso (ByVal jaja as string) _UXHelper.Soso(jaja) End sub Public Sub Dada (ByVal nene as string) implements IX.Dada nene = someLocalPreprocessing (nene) _UXHelper.Dada(nene) End sub End Class Public Class B implements IX Private _IXHelper as new IXHelper Private Sub Soso (ByVal jaja as string) _UXHelper.Soso(jaja) End sub Public Sub Dada (ByVal nene as string) implements IX.Dada nene = someOtherStuff (nene) _UXHelper.Dada(nene) End sub End Class Quote .nerd
DR00ME Posted March 12, 2004 Posted March 12, 2004 Just because an interface is public doesn't mean you have to implement it publicly. Implementing the interface privately is actually better. If you make every method implemented in private, you can no longer access specific methods directly. You have to use a reference to the interface. :p Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
Leaders dynamic_sysop Posted March 12, 2004 Leaders Posted March 12, 2004 you can access Private members still if you implement as Private in many cases by specifying the BindingFlags in a way like this ... Reflection.BindingFlags.NonPublic [color=Blue]Or[/color] Reflection.BindingFlags.Instance Quote
DR00ME Posted March 12, 2004 Posted March 12, 2004 (edited) I knew it! at least I tried! :D Edited March 12, 2004 by DR00ME Quote "Everything should be made as simple as possible, but not simpler." "It's not that I'm so smart , it's just that I stay with problems longer ." - Albert Einstein
Malfunction Posted March 13, 2004 Author Posted March 13, 2004 I thought about implementing a helper class but what about event-handlers? My controls have a couple of identical event-handlers... Quote Debug me...
Heiko Posted March 17, 2004 Posted March 17, 2004 Uh. I didn't have to tackle that problem. Can you add the event handlers by code ? Public Class malFunctionsTextBox Inherits TextBox Private _mF as new mFHelper Private Sub New() Mybase.New AddHandler me.OnClick, AddressOf _mF.Click End Sub End Class Quote .nerd
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.