How can I get parallel extensions to run a function that has two input parameters? - parallel-extensions

I've tried really hard to get this to work and have had no luck. How can I get parallel extensions to run a function that has two input parameters? I'm using the more recent version, the Reactive Extensions with the 3.5 framework.
I need to get the extensions to run act (or the function ProcessOrder) but no matter what I try I can't get it to do it.
Dim act As New System.Action(Of Int32, Date)(AddressOf ProcessOrder)
act(CInt(RowA("ID")), RunDate)
Tasks.Task.Factory.StartNew(act)
I used to be able to do the following:
Dim A(0) As Object
A(0) = CInt(RowA("ID"))
A(1) = RunDate
Tasks.Task.Create(AddressOf ProcessOrder, A)
But it's not supported anymore

Create a small class that has the two parameters as properties and have a method on the class that acts upon those properties.
Public Class ProcessClass
Private _p1 As Integer
Private _p2 As Date
Public Sub New(ByVal p1 As Integer, ByVal p2 As Date)
Me._p1 = p1
Me._p2 = p2
End Sub
Public Sub ProcessOrder()
Trace.WriteLine(String.Format("{0}:{1}", _p1, _p2))
End Sub
End Class
And then invoke it by:
Dim Obj As New ProcessClass(1, DateTime.Now())
Dim Act As New System.Action(AddressOf Obj.ProcessOrder)
System.Threading.Tasks.Task.Factory.StartNew(Act)

Related

VB.Net: Call a module Method or Routine dynamically with parameters

I want to run a Method using a variable name that is stored in a Module with a parameter:
Dim subName as String = "sub1"
Dim param as Integer = 123
sub1(param) <- I want to run this using the **subName** string
I don't want to use Select Case because the Methods are in many different modules and I don't want to maintain a select-case function.
I looked up CallByName but it seems this only works for Classes. I can't figure out how to set the object ObjectRef when it comes to Modules :
Public Function CallByName(ByVal ObjectRef As System.Object,ByVal ProcName As String,ByVal UseCallType As CallType, ByVal Args() As Object) As Object
Is there a way to do this in VB.Net?
Edit: To make it really simple, I need the equivalent of VBA's:
Application.Run module_name.sub_name param
You can use reflection to create a delegate to the methods in the Module. I would load the created delegates into a Dictionary(Of String, Action(Of Int32)).
Action(Of Int32) is chosen because it matches the signature you specified of a subroutine taking an integer parameter.
Assume you have a Module defined like this:
Module SomeModule
Public Sub Sub1(arg As Int32)
Console.WriteLine("Sub1: {0}", arg)
End Sub
Public Sub Sub2(arg As Int32)
Console.WriteLine("Sub2: {0}", arg)
End Sub
End Module
Now to create and store the delegates in a dictionary.
Private methods As New Dictionary(Of String, Action(Of Int32))
Sub LoadMethods()
Dim modType As Type = GetType(SomeModule)
Dim mi As Reflection.MethodInfo
mi = modType.GetMethod("Sub1", BindingFlags.Static Or BindingFlags.Public)
methods.Add(mi.Name, CType(mi.CreateDelegate(GetType(Action(Of Int32))), Action(Of Int32)))
mi = modType.GetMethod("Sub2", BindingFlags.Static Or BindingFlags.Public)
methods.Add(mi.Name, CType(mi.CreateDelegate(GetType(Action(Of Int32))), Action(Of Int32)))
End Sub
You can retrieve and invoke the delegate like this:
methods("Sub1")(123)
methods("Sub2")(456)
Edit: I sometimes makes things to complicated. The LoadMethods method can be done without reflection like this:
Sub LoadMethods()
methods.Add("Sub1", New Action(Of Int32)(AddressOf SomeModule.Sub1))
methods.Add("Sub2", New Action(Of Int32)(AddressOf SomeModule.Sub1))
End Sub
Edit 2: Based on edit to question and comment below.
Edit: To make it really simple, I need the equivalent of VBA's:
Application.Run module_name.sub_name param
You will need to first extract the Module type from its containing assembly based on the entered name. Then you can retrieve the MethodInfo as shown above. The following example assumes that the module is contained in the executing assembly and has minimal checks implemented. It will require you to provide the module name, method name and an array properly typed method arguments. In a real world scenario, it would probably need to take a string of the arguments and perform some type of dynamic type casting to build up the typedArgs array based on calling MethodInfo.GetParameters.
Private Shared Sub Exec(moduleName As String, methodName As String, typedArgs As Object())
Dim asm As Reflection.Assembly = Assembly.GetExecutingAssembly
Dim modType As Type = asm.GetType(String.Format("{0}.{1}", asm.GetName.Name, moduleName))
If modType IsNot Nothing Then
Dim mi As Reflection.MethodInfo
mi = modType.GetMethod(methodName, BindingFlags.Static Or BindingFlags.Public)
If mi IsNot Nothing Then
mi.Invoke(Nothing, typedArgs)
End If
End If
End Sub
Example usage: Exec("SomeModule", "Sub1", New Object() {123})
Lets say you want to call subroutine (or function) sub1 with parameter 123 with optionally given module name module1
Call example, If module name is not available (function name to invoke should be unique among project):
Dim FunctionName As String = "sub1"
Dim Param As Integer = 123
InvokeModuleFunction(FunctionNameToCall:=FunctionName, FunctionParameters:=Param)
Alternatively, If you know module name:
Dim FunctionName As String = "sub1"
Dim Param As Integer = 123
Dim ModuleName As String = "module1"
InvokeModuleFunction(FunctionNameToCall:=FileType, ModuleName:=ModuleName, FunctionParameters:=Param)
InvokeModuleFunction definition
Private Sub InvokeModuleFunction(FunctionNameToCall As String, FunctionParameters As Object, Optional ModuleName As String = Nothing)
Dim MyReflectionAssembly = Reflection.Assembly.GetExecutingAssembly()
Dim MyFunctionType As Type
If IsNothing(ModuleName) Then
'Gets function without ModuleName. FunctionName should be unique in the assembly/programm.
MyFunctionType = MyReflectionAssembly.DefinedTypes.Where(Function(x) x.DeclaredMethods.Where(Function(y) y.Name = FunctionNameToCall).Count > 0).FirstOrDefault
Else
'Gets function using ModuleName, if available
MyFunctionType = MyReflectionAssembly.DefinedTypes.Where(Function(x) x.Name = ModuleName AndAlso x.DeclaredMethods.Where(Function(y) y.Name = FunctionNameToCall).Count > 0).FirstOrDefault
End If
If Not IsNothing(MyFunctionType) Then MyFunctionType.GetMethod(FunctionNameToCall).Invoke(MyFunctionType, New Object() {FunctionParameters})
End Sub
Alternatively you can use more than one parameter in invoking.
You would need to modify the above function to allow to pass more than one parameter.
The invoke part would look like:
FunctionType.GetMethod(FunctionNameToCall).Invoke(FunctionType, New Object() {Par1, Par2, Par3})

How to limit the scope of a shared variable so that it can only be access in one function?

Private Shared _twolettercountryCodeDict As Generic.Dictionary(Of String, String)
Private Function twolettercountrycode() As String
If _twolettercountryCodeDict Is Nothing Then
_twolettercountryCodeDict = New Generic.Dictionary(Of String, String) From {{"ty", "turkey"}, {"py", "pakinmay"}, {"ra", "romania"}, {"vm", "vietnam"}, {"bl", "brazil"}, {"et", "egypt"}, {"ka", "korea"}}
Dim listOfCountries = fileToCol(COUNTRYCODESFileName)
For Each var In listOfCountries
Dim ar = var.Split({"*"}, System.StringSplitOptions.None).ToList()
_twolettercountryCodeDict.Add(LCase(ar(1)), UCase(ar(0)))
Next
End If
Return _twolettercountryCodeDict(Me.twoletter.ToLower)
End Function
Here, I am using Private Shared _twolettercountryCodeDict As Generic.Dictionary(Of String, String)
That's because I want to share that _twolettercountryCodeDict for the whole program. I am basically implementing lazy loading. I do not want part of the code that read a text file and populate country codes are done again and again.
The thing is if I declare it as Private Shared, other methods on the same class can access that variable too. Which is not much of a problem but say I want to avoid it.
If I declare the variable as static inside the function then the twolettercountryCodeDict won't be shared.
So I am in a dilemma. What's the solution?
Let's just say that twolettercountrycode requires a private member, so it can't be a shared function. But I want _twolettercountryCodeDict to be shared and accessible only from twolettercountrycode. Can I do so?
This doesn't do precisely what you asked for, but it solves the requirement of only allowing the resource loading to be done once. You could achieve the same thing by using a Shared Constructor on a class that's solely for loading your resource.
You may also want to use a ReadOnlyDictionary (implementation) so that your dictionary can't be modified by callers.
Friend Shared ReadOnly Property twolettercountrycode As Generic.Dictionary
Get
Static _twolettercountryCodeDict As Generic.Dictionary = Nothing
If _twolettercountryCodeDict Is Nothing Then
_twolettercountryCodeDict = New Generic.Dictionary(Of String, String) From {{"ty", "turkey"}, {"py", "pakinmay"}, {"ra", "romania"}, {"vm", "vietnam"}, {"bl", "brazil"}, {"et", "egypt"}, {"ka", "korea"}}
Dim listOfCountries = fileToCol(COUNTRYCODESFileName)
For Each var In listOfCountries
Dim ar = var.Split({"*"}, System.StringSplitOptions.None).ToList()
_twolettercountryCodeDict.Add(LCase(ar(1)), UCase(ar(0)))
Next
End If
return _twolettercountryCodeDict
End Get
End Property

Problem returning object from VB.NET COM Assembly into VBA (Access)

I have an assembly in VB .NET 2.0 that I am trying to use to call a webservice.
This will be COM visible, and return the results to Access in VBA.
The .NET Assembly passes all tests and executes perfectly.
I was experiencing "Object does not support this property or method" errors when calling the methods from VBA.
I broke it down to a certain object that was being returned and added some test methods to the .NET DLL.
There is a "Patient" object I want to return.
It looks like this (made it very very simple to test it):
Option Strict On
Option Explicit On
<ComClass(Patient.ClassId, Patient.InterfaceId, Patient.EventsId)> _
Public Class Patient
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "672dfbd9-8f3a-4ba2-a33d-89fef868f2b9"
Public Const InterfaceId As String = "74a9c54c-4427-4d31-8220-3258ecda345d"
Public Const EventsId As String = "dc25515e-1bb7-4a66-97d5-270c00d792a9"
#End Region
Public Sub New()
MyBase.New()
End Sub
Public Property StorePatientID() As Integer
Get
Return m_StorePatientID
End Get
Set(ByVal value As Integer)
m_StorePatientID = value
End Set
End Property
Private m_StorePatientID As Integer
End Class
So about as simple as an object can be really.
I have a method that just returns a dummy record, just to test it:
Public Function GetPatientTest() As Patient
Dim patient As New Patient
patient.StorePatientID = 99
Return patient
End Function
This fails with the afformentioned error.
HOWEVER,
This method succeeds!
Public Function GetPatientArrayTest() As Patient()
Dim strings As New List(Of Patient)
Dim patient As New Patient
patient.StorePatientID = 99
strings.Add(patient)
Return strings.ToArray
End Function
The DLL is made com visible through "Properties" page.
Builds to project/bin/debug, always do a rebuild.
Always seems to be updated with new methods etc when I look at it in VBA so don't think it's looking at an old version.
Obviously no funny dependencies with these methods.
Really really struggling with this.
EDIT:
Update 16/03/2011 - Added VBA script
Public Function FindPatientsTest(ByVal surname As String, ByVal surnameBeginsWith As Boolean, ByVal forename As String, ByVal forenameBeginsWith As Boolean, ByVal dateOfBirth As String)
Dim token As String
token = Login()
Dim patient As SCIStoreWS60.patient
Set patient = New SCIStoreWS60.patient
'// This doesn't work.
'// When adding a "Watch" to the function, I can see it returns an "Object/Patient" and is the correct results
'// When adding a "Watch" to the variable "patient" I can see it is a "Patient/Patient"
patient = sciStore.GetPatientTest()
'// This works fine
Dim something As Variant
something = sciStore.GetPatientArrayTest()
End Function
Update 16/03/2011 5 minutes later - Chastising myself
Sorry, I just worked it out.
I need to "Set" the patient variable.
Set patient = sciStore.GetPatientTest()
Why didn't I need to do this for the "something" variant?
So, yes, you need to Set object references, but not arrays.

VB.Net List.Find. Pass values to predicate

Having a bit of trouble using the List.Find with a custom predicate
i have a function that does this
private function test ()
Dim test As Integer = keys.Find(AddressOf FindByOldKeyAndName).NewKey
here's the function for the predicate
Private Shared Function FindByOldKeyAndName(ByVal k As KeyObj) As Boolean
If k.OldKey = currentKey.OldKey And k.KeyName = currentKey.KeyName Then
Return True
Else
Return False
End If
End Function
by doing it this way means i have to have a shared "currentKey" object in the class, and i know there has to be a way to pass in the values i'm interested in of CurrentKey (namely, keyname, and oldkey)
ideally i'd like to call it by something like
keys.Find(AddressOf FindByOldKeyAndName(Name,OldVal))
however when i do this i get compiler errors.
How do i call this method and pass in the values?
You can cleanly solve this with a lambda expression, available in VS2008 and up. A silly example:
Sub Main()
Dim lst As New List(Of Integer)
lst.Add(1)
lst.Add(2)
Dim toFind = 2
Dim found = lst.Find(Function(value As Integer) value = toFind)
Console.WriteLine(found)
Console.ReadLine()
End Sub
For earlier versions you'll have to make "currentKey" a private field of your class. Check my code in this thread for a cleaner solution.
I have an object that manages a list of Unique Property Types.
Example:
obj.AddProperty(new PropertyClass(PropertyTypeEnum.Location,value))
obj.AddProperty(new PropertyClass(PropertyTypeEnum.CallingCard,value))
obj.AddProperty(new PropertyClass(PropertyTypeEnum.CallingCard,value))
//throws exception because property of type CallingCard already exists
Here is some code to check if properties already exist
Public Sub AddProperty(ByVal prop As PropertyClass)
If Properties.Count < 50 Then
'Lets verify this property does not exist
Dim existingProperty As PropertyClass = _
Properties.Find(Function(value As PropertyClass)
Return value.PropertyType = prop.PropertyType
End Function)
'if it does not exist, add it otherwise throw exception
If existingProperty Is Nothing Then
Properties.Add(prop)
Else
Throw New DuplicatePropertyException("Duplicate Property: " + _
prop.PropertyType.ToString())
End If
End If
End Sub
I haven't needed to try this in newer versions of VB.Net which might have a nicer way, but in older versions the only way that I know of would be to have a shared member in your class to set with the value before the call.
There's various samples on the net of people creating small utility classes to wrap this up to make it a little nicer.
I've found a blog with a better "real world" context example, with good variable names.
The key bit of code to Find the object in the list is this:
' Instantiate a List(Of Invoice).
Dim invoiceList As New List(Of Invoice)
' Add some invoices to List(Of Invoice).
invoiceList.Add(New Invoice(1, DateTime.Now, 22))
invoiceList.Add(New Invoice(2, DateTime.Now.AddDays(10), 24))
invoiceList.Add(New Invoice(3, DateTime.Now.AddDays(30), 22))
invoiceList.Add(New Invoice(4, DateTime.Now.AddDays(60), 36))
' Use a Predicate(Of T) to find an invoice by its invoice number.
Dim invoiceNumber As Integer = 1
Dim foundInvoice = invoiceList.Find(Function(invoice) invoice.InvoiceNumber = invoiceNumber)
For more examples, including a date search, refer to Mike McIntyre's Blog Post

Possible Bug in Fluent-NHibernate Repository?

I think I might have found a bug, but I'm not really sure. It could be
a syntax error on my part, but the compiler isn't catching. Anyway,
here is what I'm trying to do. Basically I've written my own
repository class that essentially just wraps the Fluent Repository
class. So here is the relevant code:
Public Class GenericRepository(Of T As IHasIntId)
Private _fluentRepos As FluentNHibernate.Framework.IRepository
Public Sub New(ByVal FluentRepository As
FluentNHibernate.Framework.IRepository)
_fluentRepos = FluentRepository
End Sub
Private Sub New()
End Sub
Public Function GetById(ByVal Id As Integer) As T
Return Query(Function(x As T) (x.Id = Id)).FirstOrDefault
End Function
Public Function Query(ByVal w As Expression(Of System.Func(Of T,
Boolean))) As IList(Of T)
Return _fluentRepos.Query(Of T)(w).ToList()
End Function
End Class
Then I wrote two unit tests, one that would pass in an
InMemoryRepository and one that would use an actual NHibernate session
to hit the real database.
here they are:
<TestMethod()> Public Sub InMemoryTest()
Dim inmemRepos As New InMemoryRepository()
Dim p As New Product()
Dim id As Integer = 5
p.Id = id
p.Title = "my product"
inmemRepos.Save(p)
Dim genRepos As New GenericRepository(Of Product)(inmemRepos)
Dim foundP = genRepos.GetById(id)
Assert.AreEqual(p.Title, foundP.Title)
End Sub
<TestMethod()> Public Sub DatabaseTest()
Dim session = NHibernateSessionManager.Instance.GetSession()
Dim flRepos As New Repository(session)
Dim genRepos As New GenericRepository(Of Product)(flRepos)
Dim id As Integer = 1
Dim p = genRepos.GetById(id)
Assert.IsNotNull(p)
Assert.AreEqual(id, p.Id)
End Sub
The InMemoryTest passed, and the DatabaseTest failed. The exception
from the DatabaseTest was a type conversion, from int to product (or
maybe the other way around.) I was able to "fix" it though. In the
Fluent NHibernate code I changed the Query method on the Repository
class from:
return _session.Linq<T>().Where(where).ToArray();
to
return _session.Linq<T>().Where(where.Compile()).ToArray();
Now both tests pass. All of the unit tests in the Fluent NHibernate
project pass either way.
The answer that you received on the Fluent NHibernate mailing list is most likely the correct one. That is, that it's a bug in Linq to NHibernate rather than Fluent NHibernate, which is caused by the VB compiler producing different expression-trees to C#.