Casting interfaces and MEF - vb.net

I have the following problem with MEF:
Interface definition to be used by host:
Public Interface IExecuteDoSomething
Inherits IAddinSettings
Event DataReceived As EventHandler(Of DataReceivedEventArgs)
Function DoSomething() As Boolean
End Interface
Public Class DataReceivedEventArgs
Inherits EventArgs
Public Sub New(ByVal message As String)
Me.Message = message
End Sub
Public Message As String
End Class
extra interface needed by some other code inside the host:
Public Interface IAddinSettings
ReadOnly Property Setting() As AddinSettings
End Interface
Public Class AddinSettings
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Sub New(ByVal name As String)
Me.Name = name
End Sub
End Class
The class that provides the export:
<Export(GetType(SharedLibrary.IExecuteDoSomething))> Public Class Class1
Implements SharedLibrary.IExecuteDoSomething
Implements SharedLibrary.IAddinSettings
Private _Addinsettings As New SharedLibrary.Addinsettings("Test")
Public Function DoSomething() As Boolean Implements SharedLibrary.IExecuteDoSomething.DoSomething
MsgBox("i did something")
Return True
End Function
Public Event DataReceived(ByVal sender As Object, ByVal e As SharedLibrary.DataReceivedEventArgs) Implements SharedLibrary.IExecuteDoSomething.DataReceived
Public ReadOnly Property Setting() As SharedLibrary.AddinSettings Implements SharedLibrary.IAddinSettings.Setting
Get
Return _Addinsettings
End Get
End Property
End Class
The host:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim catalog As New Hosting.AggregateCatalog
Dim d As New Hosting.DirectoryCatalog("..path to dlll..")
catalog.Catalogs.Add(d)
Dim container = New Hosting.CompositionContainer(catalog)
Dim batch As New Hosting.CompositionBatch
batch.AddPart(Me)
container.Compose(batch)
For Each dd In dos
AddHandler dd.DataReceived, AddressOf testevent
Next
End Sub
<Import()> Public dos As IEnumerable(Of SharedLibrary.IExecuteDoSomething)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each d In dos
d.DoSomething()
Next
End Sub
Private Sub testevent(ByVal sender As Object, ByVal e As SharedLibrary.DataReceivedEventArgs)
MsgBox("Event received: " & e.Message)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dosomethingelse(DirectCast(dos, System.Collections.Generic.List(Of SharedLibrary.IAddinSettings)))
End Sub
Private Sub Dosomethingelse(byval settings as IEnumerable(Of SharedLibrary.IAddinSettings))
End Sub
End Class
Everything seems to work fine until the Button2_Click routine is executed, then an InvalidCastException is thrown with the info:
Unable to cast object of type 'System.Collections.Generic.List1[SharedLibrary.IExecuteDoSomething]' to type 'System.Collections.Generic.List1[SharedLibrary.IAddinSettings]'.
How can i solve this problem, because the imported object implements both of the interfaces?

I suspect you're actually running into a covariance issue - that's the typical cause of problems like this. A List<IFoo> is not a List<IBar> even if IBar extends IFoo.
If you're using .NET 3.5, the easiest way to get round this in your case is to remove the DirectCast and instead use Enumerable.Cast:
Dosomethingelse(dos.Cast(Of SharedLibrary.IAddinSettings))

Related

Early Binding with Generics

I have a class which takes the method address and arguments, and executes it later when told to do so.
' need to turn option strict off due to Execute method executing late-bound code
Option Strict Off
Public Class WorkItem
Private Action As Object
Private Args() As Object
Public Overloads Sub [Set](action As Action)
SetArgs(action)
End Sub
Public Overloads Sub [Set](Of T)(action As Action(Of T), arg As T)
SetArgs(action, arg)
End Sub
Public Overloads Sub [Set](Of T1, T2)(action As Action(Of T1, T2), arg1 As T1, arg2 As T2)
SetArgs(action, arg1, arg2)
End Sub
'*** more overloads of [Set] method go here...
Private Sub SetArgs(ByVal action As Object, ParamArray args() As Object)
Me.Action = action
Me.Args = args
End Sub
Public Sub Execute()
'-- early binding doesn't work
'DirectCast(Me.Action, Action(Of T)).Invoke(Args(0))
'-- this works, but forces me to to keep option strict off
Select Case Args.Length
Case 0 : Me.Action.Invoke()
Case 1 : Me.Action.Invoke(Args(0))
Case 2 : Me.Action.Invoke(Args(0), Args(1))
Case 3 : Me.Action.Invoke(Args(0), Args(1), Args(2))
Case 4 : Me.Action.Invoke(Args(0), Args(1), Args(2), Args(3))
End Select
End Sub
End Class
Here is some tester code:
Public Class Form1
Dim TheTask As WorkItem
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TheTask = New WorkItem
TheTask.Set(AddressOf DummyProc, TextBox1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TheTask.Execute()
End Sub
Private Sub DummyProc(arg As TextBox)
Threading.Thread.Sleep(1000)
Debug.Print("work completed")
End Sub
End Class
The WorkItem class obviously doesn't work with OPTION STRICT ON, due to the late-bound call in Execute method.
Is there any way I can convert the late-bound call to early binding?
This can be achieved by using delegates. You can declare a delegate that represents a non-generic subroutine, and invoke a representation of that delegate on an object that passed arg as a constructor parameter. I don't know if your solution still needs a generic work item by implementing it like this, but my example still keeps that same setup:
Delegate Sub WorkItem()
Dim TheTask As WorkItem
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TheTask = AddressOf (New DummyProc(Of TextBox)(TextBox1)).Execute
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
TheTask.Invoke()
End Sub
Private Class DummyProc(Of T)
Private ReadOnly _arg As T
Public Sub New(ByVal arg As T)
_arg = arg
End Sub
Public Sub Execute()
MessageBox.Show(String.Format("My Arg: {0}", _arg))
Threading.Thread.Sleep(1000)
MessageBox.Show("work completed")
End Sub
End Class

vb.net use function from other form

I am in a situation where i have a single helper class that i re-use on most of my forms. and it happens that my helper class needs to call a function from that form as well
Public Class Form1
Dim hc As HelperClass
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass
End Sub
Private Sub someForm1Sub()
'do something
End Sub
End Class
Public Class Form2
Dim hc As HelperClass
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass
End Sub
Private Sub someForm2Sub()
'do something
End Sub
End Class
Public Class Form3
Dim hc As HelperClass
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass
End Sub
Private Sub someForm3Sub()
'do something
End Sub
End Class
Public Class HelperClass
'i need to call here someForm1Sub(), someForm2Sub(), someForm3Sub()
'but it bases on which form uses this class
End Class
i think that there are many ways to do this. I have read some about delegate function, but i do not know how to pass a delegate to other class. I am using vb.net 2010. Please provide the best way to do this
please reply thanks.
You can pass a delegate to a method using the AddressOf keyword. For example,
Public Sub Test()
' Make Foo() call the Bar() method.
Foo(AddressOf Bar)
End Sub
Public Sub Foo(d As Action)
' Call the delegate.
d()
End Sub
Public Sub Bar()
Console.WriteLine("Bar was called.")
End Sub
But, as you say, there are many ways you could do this. One possible solution which may be a bit nicer than passing delegates (especially if there are a few methods in the form that could be called by the helper class) would be to have Form1, Form2 and Form3 all implement an interface, and have the HelperClass call a method on the interface. An instance of the interface could then be given to HelperClass in its constructor.
For example,
Public Interface IForm
Sub DoStuff()
End Interface
Public Class Form2
Implements IForm
Dim hc As HelperClass
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
hc = New HelperClass(Me)
End Sub
Public Sub DoStuff() Implements IForm.DoStuff
' Do stuff here.
End Sub
End Class
Public Class HelperClass
Private _form As IForm
Public Sub New(form as IForm)
_form = form
End Sub
Public Sub MakeTheFormDoStuff()
_form.DoStuff()
End Sub
End Class

List(Of Event) in VB.NET

The following is part of a larger project, but for the purpose of this question, I have the following code:
Public MustInherit Class Class1(Of T As {System.Windows.Forms.Control, New})
Inherits System.Windows.Forms.UserControl
Friend Items As New Dictionary(Of Integer, T)
Sub Add(ByRef Item As T, ByVal Index As Integer)
Me.Items.Add(Index, Item)
AddHandler Item.Click, AddressOf Class1Click
End Sub
Public Shadows Event Click(ByVal sender As System.Object, ByVal e As System.EventArgs, ByVal Index As Integer)
Sub Class1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
RaiseEvent Click(sender, e, DirectCast(sender, T).Index)
End Sub
End Class
Public Class Class1CheckBox
Inherits Class1(Of System.Windows.Forms.CheckBox)
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
...
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.MyClass1 = New Class1CheckBox()
Me.CheckBox1 = New System.Windows.Forms.CheckBox()
Me.CheckBox2 = New System.Windows.Forms.CheckBox()
Me.CheckBox3 = New System.Windows.Forms.CheckBox()
Me.CheckBox4 = New System.Windows.Forms.CheckBox()
Me.SuspendLayout()
...
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox2.Name = "CheckBox2"
Me.CheckBox3.Name = "CheckBox3"
Me.CheckBox4.Name = "CheckBox4"
...
End Sub
...
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox3 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox4 As System.Windows.Forms.CheckBox
Friend WithEvents MyClass1 As Class1CheckBox
End Class
Public Class Form1
Private Sub MyClass1_Click(ByVal sender As Object, ByVal e As System.EventArgs, ByVal Index As Integer) Handles MyClass1.Click
MessageBox.Show(DirectCast(sender, CheckBox).Name)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Controls.OfType(Of CheckBox).AsParallel.ForAll(Sub(n) Me.MyClass1.Add(n, n.Index))
End Sub
End Class
The above code works beautifully. Anytime one of the four checkboxes are clicked, the click even is intercepted by the MyClass1 and is handled by MyClass1. That's what I want.
The problem is that "Click" is hardcoded. Notice, that Class1 is generic. I want for it to be able to accept any class which inherits System.Windows.Forms.Control. Some controls may have a Check event, or a hover, or a GotFocus. What I need is something as follows, I'm just not sure what the proper syntax is:
Public Class Class1CheckBox
Inherits Class1(Of System.Windows.Forms.CheckBox)
MyBase.AddEvent("Hover", <signature>...)
End Class
Public MustInherit Class Class1(Of T As {System.Windows.Forms.Control, New})
Inherits System.Windows.Forms.UserControl
Friend Items As New Dictionary(Of Integer, T)
Friend Events As New List(Of Event)
Sub AddEvent(EventName As String, ...)
Events.Add(EventName...)
End Sub
Sub Add(ByRef Item As T, ByVal Index As Integer)
Me.Items.Add(Index, Item)
For Each MyEvent As Event In Events
AddHandler ...
Next MyEvent
End Sub
'Public Shadows Event Click(ByVal sender As System.Object, ByVal e As System.EventArgs, ByVal Index As Integer)
'Sub Class1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' RaiseEvent Click(sender, e, DirectCast(sender, T).Index)
'End Sub
End Class
What is the proper syntax to create some kind of sequence of events, and how would I be able to raise those events?
Thanks,
What you want is not possible. Events are members, just like methods and properties. You can't write code to access any member unless you know that the type you have has that member.
Just as with methods and properties, if you want to decide what event to use at run time then you have to use Reflection.
As jmcilhinney correctly say, you need to use reflection.
Here is a simple example:
Imports System.Reflection
Public MustInherit Class Class1(Of T As {Control, New})
Private _Items As New Dictionary(Of Integer, T)
Private _Events As New Dictionary(Of String, [Event])
Protected Sub AddEvent(eventName As String, [delegate] As [Delegate])
If (Not Me._Events.ContainsKey(eventName)) Then
Dim info As EventInfo = GetType(T).GetEvent(eventName)
If (info Is Nothing) Then
Throw New ArgumentOutOfRangeException("eventName")
End If
Me._Events.Add(eventName, New [Event]([delegate], info))
End If
End Sub
Public Sub AddItem(item As T, index As Integer)
Me._Items.Add(index, item)
For Each [event] As KeyValuePair(Of String, [Event]) In Me._Events
[event].Value.Info.AddEventHandler(item, [event].Value.Delegate)
Next
End Sub
Friend Class [Event]
Friend Sub New([Delegate] As [Delegate], Info As EventInfo)
Me.[Delegate] = [Delegate]
Me.Info = Info
End Sub
Public ReadOnly [Delegate] As [Delegate]
Public ReadOnly Info As EventInfo
End Class
End Class
Public Class Class1CheckBox
Inherits Class1(Of CheckBox)
Public Sub New()
Me.AddEvent("CheckedChanged", New EventHandler(Sub(sender As Object, e As EventArgs) MsgBox(DirectCast(sender, CheckBox).Name & " is checked: " & DirectCast(sender, CheckBox).Checked.ToString())))
End Sub
End Class

inheritance in VB.net

Not real sure how to ask this question... Some of my terms may be incorrect but hopefully I'll be able to get the question across. If I have a class something like this
Public Class Agency
public property ID as integer=0
public property Name as string=string.empty
Public sub new()
end sub
end class
and then a factory class that returns a list
Public Class Agency_Controller
Public Sub New()
end sub
Public function Fetch() as list(of Agency)
pop the list and return it
end function
end class
If I create another class say Agency_Misc and want to inherit the Agency Class I get, how to do that?
Public Class Agency_Misc
inherits Agency
public property Address as string=string.empty
end class
Now if I want to use the Agency_Misc, how do I get the Agency_Controller Fetch function? In the code if I were going after the agency... I do something like
Dim oS as list(of Agency)=nothing
dim oC as new Agency_Controller
os=oc.Fetch()
but if I want my list to have the list(of Agency_Misc) (because I was doing some more stuff)
how do I do that. I can't change list(of Agency) to list(of Agency_Misc) because it will tell me it can't convert it I think it was.
Anyway... I'd like to learn what it is I'm missing or what other approach I need to look into.
If you have defined your classes properly, you can do the following:
Define a BaseClass.
Define a DerivedClass which inherits from BaseClass
Define a List(Of BaseClass) and populate it with objects of DerivedClass (the List will accept objects of Dervived class, because these are, through inheritance, also objects of BaseClass.
Access the items in the list with a variable of Type DerivedClass.
What you CAN'T do is populate the list with objects of type BaseClass, and then attempt to access them using a variable of Type DerivedClass.
Ex. #1 THIS will work:
Public Class Form1
Private MyListOfBaseCLass As List(Of MyBaseClass)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyListOfBaseCLass = New List(Of MyBaseClass)
Dim dc As New MyDerivedCLass("City of Portland", "555 SW 5th Avenue")
MyListOfBaseCLass.Add(dc)
dc = New MyDerivedCLass("City of Salem", "222 E River Road")
MyListOfBaseCLass.Add(dc)
dc = New MyDerivedCLass("City of Denver", "333 SomeStreet")
MyListOfBaseCLass.Add(dc)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each dc As MyDerivedCLass In MyListOfBaseCLass
MsgBox(dc.MyName & ", " & dc.MyAddress)
Next
End Sub
End Class
THIS will FAIL:
Public Class Form1
Private MyListOfBaseCLass As List(Of MyBaseClass)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyListOfBaseCLass = New List(Of MyBaseClass)
Dim dc As New MyBaseClass("City of Portland")
MyListOfBaseCLass.Add(dc)
dc = New MyBaseClass("City of Salem")
MyListOfBaseCLass.Add(dc)
dc = New MyBaseClass("City of Denver")
MyListOfBaseCLass.Add(dc)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each dc As MyDerivedCLass In MyListOfBaseCLass
MsgBox(dc.MyName & ", " & dc.MyAddress)
Next
End Sub
End Class
You best set the properties in the constructor
Public Class Agency
Private m_ID As Integer
Public Property ID() As Integer
Get
Return m_ID
End Get
Set(ByVal value As Integer)
m_ID = value
End Set
End Property
Private m_name As String
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
Sub New(ByVal id As Integer, ByVal name As String)
Me.m_ID = id
Me.m_name = name
End Sub
End Class
...
Dim HiDollar as New Agency(100, "High Dollar")

Error in programming

Imports SpeechLib
Public Class Form1
Public vox = CreateObject("sapi.spvoice")
Private Sub cmdSpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSpeak.Click
Dim t As String = "Hello , This is a Text"
Say(t)
End Sub
Public Sub Say(ByVal text As String)
vox.Speak(text,SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
vox.pause()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vox.AlertBoundary = SVEPhoneme
End Sub
End Class
I am getting an error
Name 'SVEPhoneme' is not declared.
How and where do I declare it ?
It is SpeechVoiceEvents.SVEPhoneme
This is all a lot easier if you make this code early bound:
Public vox as New SpVoice
Or better yet, use the .NET wrapper for the sapi, System.Speech assembly.
Imports System.Speech.Synthesis
Public Class Form1
Public vox As New SpeechSynthesizer
Public Sub Say(ByVal text As String)
vox.SpeakAsync(text)
End Sub
End Class
SVEPhoneme represents the Phoneme event, which occurs when the engine completes a phoneme while speaking.
Try setting SVEPhoneme to be the integer 64.
http://msdn.microsoft.com/en-us/library/ms720886(v=vs.85).asp