best matching overloaded method is not being called - vb.net

I have the following methods:
Public Function RenderRateTable() As String
Private Function RenderRateTable(ToddVersionedObject As ToddVersionedObject,
FieldInfo As FieldInfo) As String
Private Function RenderRateTable(ArrayIndexes As List(Of ArrayIndexesAttribute.ArrayIndex),
ThreeDimensionalArray As ThreeDimensionalDecimalArrayType) As String
Private Function RenderRateTable(ArrayIndexes As List(Of ArrayIndexesAttribute.ArrayIndex),
TwoDimensionalArray As ArrayOfDecimalArraysType) As String
Note that the types that I'm overloading (ArrayOfDecimalArraysType, ThreeDimensionalDecimalArrayType) are not classes that inherit from the same base class. They are different structures that don't inherit from anything. They were written by someone else and I can't change them.
When I call it from within the same class like this
Dim MyThreeDimensionalDecimalArrayType As ThreeDimensionalDecimalArrayType
RenderRateTable(MyArrayIndexes, CType(MyThreeDimensionalDecimalArrayType, Object))
it doesn't go to the right method. It just goes to RenderRateTable().
What's even stranger is, the call is made with two arguments (parameters), but these are (ignored?) and it calls a method that takes no parameters and no run-time error is thrown!
I have Option Strict Off and the variable I'm passing to the parameter ThreeDimensionalArray is of type Object.
I'm trying to get it to where I have a bunch of overloads and it picks the right method based on the type of the Object passed.
Polymorphic method calling instead of an Select Case statement. I hate conditional blocks like that.
UPDATE
I got the code working by declaring the overloads Public, but I still don't understand:
Why the dispatcher wouldn't find the right Private method when the
call is within the same class.
Why the dispatcher would call a method with no parameters when the call is made with 2 parameters, and not throw an error.

You have RenderRateTable() as Public and the rest as Private, which will prevent it from being able to use the correct overload when called from outside the class.
It is very likely that Option Strict On would have pointed out that as a problem: I recommend that you use it to make your programming endeavours easier :)

Related

Using interface function defined in DLL

I'm using an interface defined in a DLL.
When I call...
m.GetMasterVolumeLevelScalar(btVol)
... I get a Null Reference Exception because "m" is nothing.
However, I can't use "new" on this interface.
How would I use this interface correctly?
I did read on implements, but I didn't find an example similar to this interface.
Thank you.
Edit: I know now that I need to type
Implements Vannatech.CoreAudio.Interfaces.IAudioEndpointVolume
and the functions will automatically be added to my class.
However, I'm not sure what to do with the NonImplementedException for example here:
Public Function GetMasterVolumeLevelScalar(ByRef level As Single) As Integer Implements IAudioEndpointVolume.GetMasterVolumeLevelScalar
Throw New NotImplementedException()
End Function
I got it:
I simply need to type
Implements Vannatech.CoreAudio.Interfaces.IAudioEndpointVolume
By doing that, all functions will automatically be added to the class in which I typed this.
I just didn't scroll down enough to see that.

None Shared member requires an object reference

I'm having a problem because I changed my code from vb6 to .net and I cant seem to sort out this issue I am having. Please assist.
The error message I'm getting:
Error 5 Reference to a non-shared member requires an object reference.
This is happening quite a few places in the code. This is my code. The problem is where it says FrmInvItem.Inv.`
Option Strict Off
Option Explicit On
Public Class ClsInv
Public Function RunProcess(ByVal ConnectStr As String, ByRef Parstr As String) As Integer
Dim frmInvIt As frmInvItem
RunProcess = frmInvItem.Inv(ConnectStr, Parstr)
frmInvIt.Close()
End Function
I assume Inv is not shared, but you are calling it without an instance of the class in which it is. So you either have to make it shared or create an instance of frmInvItem:
Dim frmInvIt As New frmInvItem() ' create instance
RunProcess = frmInvIt.Inv(ConnectStr, Parstr) ' use it on this instance
frmInvItem is the class, you can call a method via classname only if the method is shared.
Shared procedures are class methods that are not associated with a
specific instance of a class. For example, the Cos method defined
within the Math class is a shared method. You can call a shared
procedure as a method of an object or directly from the class.
You are running a method called Inv on a the frmInvIt without creating an instance.
You should create an instance to run your method against:
http://msdn.microsoft.com/en-us/library/77s47661.aspx

How do I specify Enumerable.Count() instead of List.Count?

When attempting to use the Enumerable.Count() extension method from Visual Basic, the following code results in a compile-time error:
Imports System.Linq
Module Module1
Sub Main()
Dim l As New List(Of Foo) From {New Foo("a"), New Foo("b"), New Foo("a")}
Dim i As Integer = l.Count(Function(foo) foo.Bar = "a")
Console.WriteLine(i)
Console.ReadLine()
End Sub
Class Foo
Sub New(ByVal bar As String)
Me.Bar = bar
End Sub
Public Property Bar As String
End Class
End Module
The error produced is:
'Public ReadOnly Property Count As Integer' has no parameters and its
return type cannot be indexed.
I'm targeting .NET 4.0, so extension methods should be supported. It's also worth noting that the equivalent code in C# infers the extension method correctly...
Why is the compiler unable to infer the use of Enumerable.Count, given the predicate I'm passing as an argument, and how can I use the extension method instead of the List's Count property?
The VB.Net compiler first tries to look up Count on the List instance, and it finds the Count property. This property is used instead of the extension method since fields and properties always shadow extension methods by name. I don't know where this is stated in the Visual Basic Language spec, but you can read more in this MSDN Magazin article:
Fields and properties always shadow extension methods by name. Figure 4 shows an extension method and public field with the same name and various calls. Though the extension method contains a second argument, the field shadows the extension method by name and all calls using this name result in accessing the field. The various overloaded calls will all compile, but their results at run time may be unexpected since they will bind to the property and use the default property behavior to return a single character or result in a runtime exception. It is important to choose the names of your extension methods so you avoid clashes with properties, fields, and existing instance methods.
So, Count(Function(foo) foo.Bar = "a") could mean: call the Count-property with Function(foo) foo.Bar = "a", or take the result of the Count-property and index it with Function(foo) foo.Bar = "a", which could be totally valid, since indexed properties in VB.Net can take any parameter.
This works in C# (I guess) because it is easier for the C# compiler to distinguish between method calls and a property access, because unlike VB.Net C# does not allow arbitrary parameters on properties and indexed properties.
To use the extension method, you call it like you would call every other static (shared) method:
Dim i As Integer = Enumerable.Count(l, Function(foo) foo.Bar = "a")
or call Call on IEnumerable explicitly:
Dim i As Integer = l.AsEnumerable().Count(Function(foo) foo.Bar = "a")
To answer your question as to why VB can't do what C# can in this case...
VB lets you access properties with () after the name, and also lets you call functions with no parameters by omitting the (). Also indexers use rounded brackets, instead of square brackets you have in C#. These are examples of tremendous VB features designed to make programming easier, which actually results in more ambiguous, harder to understand, and bug prone code.
So, in this particular case, VB sees you are accessing Count, and assumes the brackets after it are an indexer to the Count property, rather than arguments to the Count function.
C# sees the rounded brackets, and realises that you aren't accessing the indexer, you must be calling a function, so looks for a function.
Of course, there's room for ambiguity in C# as well. For example, a property with the same name as an extension method, which returns a delegate type will be called in preference to the extension method...
public Action Count { get; set; }
Ah... happy days.
As to how to call the IEnumerable.Count() function, a cast (preferably DirectCast()) or executing the extension method directly Enumerable.Count(...), is far far preferable to creating a whole new array to call count on...!
I'm not sure as to why you aren't getting the overload as an option, but you should be able to cast the list to IEnumerable(Of Foo) at which point the compiler will no longer allow List(Of Foo).Count property.
CType(l, IEnumerable(Of Foo)).Count(Function(foo) foo.Bar = "a")
If the list is converted to an array it works
Dim l As New List(Of Foo) From {New Foo("a"), New Foo("b"), New Foo("a")}
Dim i As Integer = l.ToArray.Count(Function(x) x.Bar = "a")

Ninject dynamic multiple constructor arguments

I have been implementing Dependency Injection into an existing Winforms project and it has been going well so far, however I want to generalise the calling of the Forms, specifically the varying quantity of constructor parameters.
My code is as follows:
Public Shared Function GetForm(formObject As BaseObject, _
parameters As Dictionary(Of String, Object)) As Form
Select Case formObject.GetType()
Case GetType(Production.Task)
Return SMKernel.Kernel.Get(Of Forms.Production.Domain.ManageTask) _
(New Parameters.ConstructorArgument() _
{New Parameters.ConstructorArgument("task", _
CType(formObject, Production.RequiredTask))})
End Select
End Function
This works fine, the interface(s) are injected correctly, the constructor parameter "task" is populated and the Form works as expected.
As you can see I have a Dictionary that can contain several parameters which I need to add to the ConstructorArgument part of the Get method. Looking at the IntelliSense, I should be able to pass in an array of ConstructorArgument objects, however no matter what I have tried, it doesn't seem to work for one reason or another.
How do you accomplish this in Ninject if it is at all possible. If this way isn't possible, how can you pass multiple parameters into a Form's constructor via Ninject?
Use the already provided ResolutionExtensions
public static T Get<T>(this IResolutionRoot root, params IParameter[] parameters)
Then combine it with LINQ
Kernel.Get(parameters.Select(kvp => new ConstructorArgument(kvp.Key, kvp.Value)).ToArray())
Provide more details and we might give you are more elegant approach.

How to implement an interface in VB.Net when two methods have the same name but different parameters

I am a C# programmer but I have to work with some VB.Net code and I came across a situation where I have two methods on an interface with the same name but different method parameters. When I attempt to implement this interface in a class, VB.Net requires explicitly declaring "Implements MethodName" after the method signature. Since both method names are identical, this is confusing the compiler. Is there a way to get around this sort of problem? I suspect this must be a common occurrence. Any thoughts?
N.B. This was more a case of the programmer not verifying that the interface in question had not changed from underneath him.
How is this confusing the compiler?
The compiler expects to find an implementation for every method signature, and distinguishes the implementations by their signatures.
If the signatures are identical/undistinguishable (in most cases it means that the arguments are of the same types in the same order) you'll get a design-time error related to the interface, saying that the two methods cannot overload eachother as they have the same signature.
So, in any case, the compiler should not be confused.
Should you need further assistance, please attach a code sample - these things are relatively easy to resolve.
Tip: When writing the implementation, as soon as you write down "implements MyInterface" and hit Enter - Visual Studio will create a "skeleton" code of the implementation, which saves you writing the method signatures and correlating them to the interface.
Example code of having two methods with the same name and everythign working well:
Interface MyInterface
Sub MySub(ByVal arg0 As DateTime)
Sub MySub(ByVal arg0 As ULong)
End Interface
Class MyImplementation
Implements MyInterface
Public Sub MySub(ByVal arg0 As Date) Implements MyInterface.MySub
...
End Sub
Public Sub MySub(ByVal arg0 As ULong) Implements MyInterface.MySub
...
End Sub
End Class
You can make the method private and give it another name.
Like:
Private Sub SaveImpl(ByVal someEntity As IEntity) Implements IRepository.Save
this will look to the outside like: someRepository.Save