I've a form class, named EditCellForm that should implement the interface ICoordsRequester.
This interface requires the Focus() method to be implemented.
As long it's a form, my class already implements the Focus() method but the compiler cannot see it because it's in the Windows.Forms.Form superclass.
Any idea?
You can create a new Focus method using the Shadows keywords to implement the interface:
Class EditCellForm
Inherits Form
Implements ICoordsRequester
Shadows Sub Focus Implements ICoordsRequester.Focus
Mybase.Focus
End Sub
End Class
or rename your method
Class EditCellForm
Inherits Form
Implements ICoordsRequester
Sub NewFocus Implements ICoordsRequester.Focus
Focus()
End Sub
End Class
You can implement the method as usual, but just call it something different. Then in the method, just call the Focus event of the base class. If all it does is the same thing, I'd just make it private so that it's only accessible via the interface.
Public Class EditCellForm
Implements ICoordsRequester
Private Sub ICoordsRequester_Focus() Implements ICoordsRequester.Focus
Me.Focus()
End Sub
End Class
Related
VB.NET
I want some WinForms to implement an interface, and be able to pass these to a procedure which can 'see' the implemented properties as well as the 'standard methods' of a Form. This is what I have so far...
Public Interface IMyInterface
Property MyProperty As String
End Interface
Public Class MyForm
Implements IMyInterface
Private _MyProperty As String
Public Property MyProperty() As String Implements IMyInterface.MyProperty
Get
Return _MyProperty
End Get
Set(ByVal value As String)
_MyProperty = value
End Set
End Property
End Class
then, elsewhere I have my method as follows...
Public Sub DoSomething(MyForm As IMyInterface)
MyForm.MyProperty = "x"
MyForm.ShowDialog()
End Sub
The obvious problem is that the compiler doesn't know what .ShowDialog is, and if I pass my form in as 'MyForm As Form' it doesn't know what 'MyProperty' is. I understand the reasons for this, but not how to solve this problem. Is a simple casting to Form the correct way to address this?
Many thanks.
You need to inherit System.Windows.Forms.Form to gain all regular form functionality and then Implement IMyInterface.
Public Class MyForm
Inherits System.Windows.Forms.Form
Implements IMyInterface
Passing MyForm As IMyInterface into the DoSomething() method is fine, however to use the regular form methods you'll need to cast it. Alternatively you could pass in the Form and then cast to IMyInterface, your choice.
Public Sub DoSomething(MyIForm As IMyInterface)
MyIForm.MyProperty = "x"
Dim MyForm As Form = TryCast(MyIForm, Form)
MyForm.ShowDialog()
Another aproach can be using your own base class
Public Class MyBaseForm
Inherits System.Windows.Forms.Form
Public Property MyProperty As String
End Class
Then all your forms can inherit that base form
Public Class MyForm
Inherits MyBaseForm
End Class
And you can use property and standard method of System.Windows.Forms.Form without casting
Public Sub DoSomething(someform As MyForm)
someform.MyProperty = "Some value"
someform.ShowDialog()
End Sub
I am trying to use Interception Call Handler and Handler Attributes on my interfaces (or implementation). Lets say my Interface has two methods DoSomething() and DoSomethingElse(), and I only have the interceptor for DoSomethingElse(); when I resolve my main interface the constructor for the Call Handler get called even if I never invoke DoSomethingElse().
I tried this by resolving to Lazy(of IMainInterface) but the moment I call the function DoSomething() the Call Handler is still created unnecessarily. Is there a way to prevent this from happening either by code or configuration. Here is my sample implementation
Handler Attribute and Call Handler
<AttributeUsage(AttributeTargets.Method)>
Public Class NormalSampleAttribute
Inherits HandlerAttribute
Public Sub New()
Console.WriteLine("Constructor of Normal Sample Attribute")
End Sub
Public Overrides Function CreateHandler(container As Microsoft.Practices.Unity.IUnityContainer) As Microsoft.Practices.Unity.InterceptionExtension.ICallHandler
Console.WriteLine("Create Handler")
Return New SampleCallHandler
End Function
End Class
Public Class SampleCallHandler
Implements ICallHandler
Public Sub New()
Console.WriteLine("Constructor of Sample Call handler")
End Sub
Public Function Invoke(input As IMethodInvocation, getNext As GetNextHandlerDelegate) As IMethodReturn Implements ICallHandler.Invoke
Console.WriteLine("Invoke of Sample Call handler - " & input.MethodBase.Name)
Return getNext.Invoke(input, getNext)
End Function
Public Property Order As Integer Implements ICallHandler.Order
End Class
Interface and implementation
Public Interface IMainInterface
Sub DoSomething()
<NormalSample()>
Sub DoSomethingElse()
End Interface
Public Class MainClass
Implements IMainInterface
Public Sub New()
Console.WriteLine("main class - Constructor")
End Sub
Public Sub DoSomething() Implements IMainInterface.DoSomething
Console.WriteLine("main class do something...")
End Sub
Public Sub DoSomethingElse() Implements IMainInterface.DoSomethingElse
Console.WriteLine("main class do something else...")
End Sub
End Class
Main module to register and execute the method
Module Module1
Public container As IUnityContainer
Sub Main()
container = New UnityContainer
DoRegistrations()
Console.WriteLine("Before lazy Resolve")
Dim lmc As Lazy(Of IMainInterface) = container.Resolve(Of Lazy(Of IMainInterface))()
Console.WriteLine("Before making lazy function call")
lmc.Value.DoSomething()
Console.ReadLine()
End Sub
Sub DoRegistrations()
container.AddNewExtension(Of InterceptionExtension.Interception)()
container.RegisterType(Of IMainInterface, MainClass)()
container.Configure(Of Interception).SetDefaultInterceptorFor(Of IMainInterface)(New InterfaceInterceptor)
End Sub
End Module
It produces the following output:
Before lazy Resolve
Before making lazy function call
main class - Constructor
Constructor of Normal Sample Attribute
Create Handler
Constructor of Sample Call handler
main class do something...
Even though DoSomethingElse() is never called the cost of the handler creation is being added on all flows. Is there any way to avoid this? Any help is appreciated.
Thanks in advance!
SV
I have been trying to do this for a long time but I can't find anything anywhere. I think I am not searching it as it should...
A little example:
Class MainClass
Property ExampleProperty As New ExamplePropertyClass
Private Class ExamplePropertyClass
Sub DoSomething()
End Sub
End Class
End Class
In the previous code the ExamplePropertyClass is used as an property of the MainClass.
There is always an error that says I can't expose a private class as propery.
But how is it possible to make only the property "Visible", I mean The user who is going to use the code should only use the property and not the class, how can the class be not inherited or visible?
What is property actually syntactic sugar for setter and getter.So mostly it is default public
You declare class as private. So it will be invisible outside. Then there is conflict if it be not visible then how people will know to assign and get that object without knowing its type. So that type should be public and visible
dim m as new MainClass()
m.ExampleProperty=? ' What is ExampleProperty ?int , object. So it should not be unknown
Another way you claim that you are not going to use that property outside.This way it is ok to have private class inside.
'Explicitly make property to be used only within class
Private Property ExampleProperty As ExamplePropertyClass
You do this with interfaces:
Public Interface IDoesSomething
Sub DoSomething()
End Interface
Public Class MainClass
Public Sub New()
m_example = New InternalClass
End Sub
Private m_example As IDoesSomething
Public ReadOnly Property Example() As IDoesSomething
Get
Return m_example
End Get
End Property
Private Class InternalClass
Implements IDoesSomething
Public Sub DoSomething() Implements IDoesSomething.DoSomething
End Sub
End Class
End Class
is it possible to extend different classes with the same generic class?
I tried something like this:
Public Class A
Public Sub TestA()
Debug.Print("Test A")
End Sub
End Class
Public Class B(Of T)
Public Sub TestB()
Debug.Print("Test B")
End Sub
End Class
Public Class C
Inherits B(Of A)
Public Sub TestC()
TestA() '**<-- Thows error 'is not declared'**
TestB()
Debug.Print("Test C")
End Sub
End Class
I basicly have some usercontrols, which derive from Combobox or Textbox and i'd like both to implement some functions(and interfaces) that are defined in a base class. In C++ i'd do it with multi inheritance.
is it possible to extend different classes with the same generic class?
Generics isn't some kind of "workaround" for a lack of multiple inheritance, no. Your class C doesn't derive from A - it just means that the T in B(Of T) would be A in the context of C.
Which instance of A would you expect TestA() to be called on? Creating an instance of C certainly doesn't create an instance of A...
The fact that B(Of T) doesn't use T anywhere should be a warning signal - types which are generic but never use their generic type parameters are generally problematic.
It's hard to know exactly how to help you solve your real problem without more details, but you can't add a common base class in like this, when you also need to derive from other types which aren't under your control.
Perhaps extension methods would help?
You could make both your Combobox and your Textbox classes implement the same interface.
Then you could define extension methods on that interface class.
Thanks to your hint i got this working with extentions
Public Class Form1
Public Interface IA
Property val As String
End Interface
Public Class A
Public Sub test()
Debug.Print("test")
End Sub
End Class
Public Class C
Inherits A
Implements IA
Public Property val As String Implements IA.val
Public Sub TestC()
val = "testxxx"
TestA()
test()
End Sub
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ct As New C
ct.TestC()
End Sub
End Class
Module TestModule
<Extension()>
Public Sub TestA(ByVal pvIA As IA)
Debug.Print(pvIA.val)
End Sub
End Module
This way every class can implement it's own 'parent' (like A here) and i don't need to implement the function TestA for every class.
thank you
Ok, so I have a scenario kind of like this. First of all, I have an interface, I'll call it Iinterface, and in the interface there is a method (i'll call it method).
Now I have a superclass, that implements the interface and a method that implements the method in the interface. There is a subclass that inherits from the superclass, however in the subclass, the method in the superclass that originally implemented the method has been shadowed by a different method with the same name and signiture.
In this case, would the subclass still implement the interface?
Specifically, would this work?
Dim stuff as Iinterface
stuff = new SuperClass
'do some things
stuff = new SubClass
thanks!
If you don't mind a compiler warning, you can have a Shadows method that does implement the interface (again) explicitly.
Class MySubClass3
Inherits MySuperClass
Implements IMyInterface
Public Shadows Sub MyMethod(ByVal a As Integer, ByVal b As String) Implements IMyInterface.MyMethod
MsgBox("Shadowed re-Implemented MyMethod")
End Sub
End Class
And even though the warning is provided, this is still a Shadows method as compiled (so
Dim mysTest As MySuperClass = New MySubClass3
mysTest.MyMethod(1, "a")
still displays "Original MyMethod"). As the Microsoft description for this warning explains, as you do intend reimplementation of the method, the warning must be ignored or explicitly disabled.
Yes, the original SuperClass method would still implement the interface, but using the original Superclass method. If the signature is the same, then use Overrides instead of Shadows. If it doesn't have the same signature, you are unable to implement the interface anyway.
Module Module1
Sub Main()
Dim myiTest As IMyInterface
myiTest = New MySuperClass
myiTest.MyMethod(1, "a")
myiTest = New MySubClass
myiTest.MyMethod(1, "a")
myiTest = New MySubClass2
myiTest.MyMethod(1, "a")
End Sub
Interface IMyInterface
Sub MyMethod(a As Integer, b As String)
End Interface
Class MySuperClass
Implements IMyInterface
Public Overridable Sub MyMethod(a As Integer, b As String) Implements IMyInterface.MyMethod
MsgBox("Original MyMethod")
End Sub
End Class
Class MySubClass
Inherits MySuperClass
Public Shadows Sub MyMethod(a As Integer, b As String)
MsgBox("Shadowed MyMethod")
End Sub
End Class
Class MySubClass2
Inherits MySuperClass
Public Overrides Sub MyMethod(a As Integer, b As String)
MsgBox("Overridden MyMethod")
End Sub
End Class
End Module