How to create a VB.NET COM-visible interface without IDispatch? - vb.net

If I create a COM-visible VB.NET interface, such as:
<ComVisible(True)>
Public Interface IMyInterface
Sub MyMethod()
End Interface
Then the resulting type library will show IMyInterface inheriting IDispatch. Is there a way to inherit just IUnknown, and not IDispatch?

Use the InterfaceTypeAttribute Class like this:
<ComVisible(True), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IMyInterface
Sub MyMethod()
End Interface

Related

Implement interface in abstract class for derived classes

Let's say I got this interface:
Public Interface IGsm
Function Run(filePath As String) As Boolean
End Interface
Now I want all child classes to use IGsm interface, therefore I decided to implement it by abstract class (hope it's good approach):
Public Class MustInherit GsmBase
Implements IGsm
Public Function Run(filePath As String) As Boolean Implements IGsm.Run
Throw New NotImplementedException()
End Function
End Class
First question:
What if I put Overridable to my function does it mean that I can define some implementation here in base class same for all derived classes and also ovveride rest implementation in derived classes? Is that correct approach?
Public Overridable Function Run(filePath As String) As Boolean Implements IGsm.Run
'Some shared code for all child classes
'......
End Function
so derived classes could do:
Public Ovverides Function Run(filePath As String) As Boolean Implements IGsm.Run
MyBase.Run
'add additional code for this specific child class...
End Function
Is that all correct what I just wrote?
Second question: If I do not mark Run as Overridable in base class. When inheriting from base class to my derived classes does it mean that my derived classes will just have to use the current implementation of that Run method from base class?
Third question: Why I cannot mark Run in base class as MustOverides?
Functions, Subs and properties and also Interface implementations are inherited in deriving classes, no matter whether the members are Overridable / Overridden or not.
Overridable means that a deriving class can just do nothing, if it is happy with the base implementation. A call to a Function will automatically call the base function. But it can override it, if another implementation is desired. You can then decide to call the base implementation or not, depending whether you want to add functionality or want to replace it completely.
Apply MustInherit to the class. This enables you to apply MustOverride to members. (This is a bit more complicated than in C#, where you can apply the same keyword abstract twice.)
Public MustInherit Class GsmBase
Implements IGsm
Public MustOverride Function Run(filePath As String) As Boolean Implements IGsm.Run
End Class
Note that you can combine different approaches. You could have several abstract base classes implementing the interface and also classes implementing the interface directly
Interface I
MustInherit Class BaseA Implements I
MustInherit Class BaseB Implements I
Class A1 Inherits BaseA
Class A2 Inherits BaseA
Class A2_1 Inherits A2
Class B1 Inherits BaseB
Class B2 Inherits BaseB
Class C Implements I
Class D Inherits C
Class E Inherits D
All objects of type A1, A2, A2_1,B1, B2, C, D and E are assigment compatible to I.
You didn't create an abstract class in your sample, it would look like this.
Public Interface IGsm
Function Run(filePath As String) As Boolean
End Interface
Public MustInherit Class GsmBase
Implements IGsm
Public MustOverride Function Run(filePath As String) As Boolean Implements IGsm.Run
End Class
Public Class Gsm
Inherits GsmBase
Public Overrides Function Run(filePath As String) As Boolean
Return False
End Function
End Class
Then you can use is as
Dim test As IGsm = New Gsm
test.Run("test") ' This will call Gsm.Run
or
Dim test As GsmBase = New Gsm
test.Run("test") ' This will call Gsm.Run
MustInherit is for class, not method. For method you would use MustOverride.

Implement an interface in partial class

I need all my TableAdapters to implement a custom interface. The problem is that some of the members defined by the interface reside in the DataSet's designer file, which I don't want to (and shouldn't) alter, as that code will be regenerated automatically. I can't relocate those members to my code file either for the same reason. What's my way out of it?
When you implement an interface, the members you declare do not have to have the same names as the members of the interface and they don't have to be public. Let's say that you have this designer-generated class:
Partial Public Class SomeClass
Public Sub FirstMethod()
Console.WriteLine("FirstMethod")
End Sub
Public Sub SecondMethod()
Console.WriteLine("SecondMethod")
End Sub
End Class
and you want it to implement this interface:
Public Interface ISomeInterface
Sub FirstMethod()
Sub ThirdMethod()
End Interface
Notice that the interface has a method named FirstMethod but SomeClass already has a method named FirstMethod. You can add your own partial class to implement the interface like this:
Partial Public Class SomeClass
Implements ISomeInterface
Private Sub FirstMethodInternal() Implements ISomeInterface.FirstMethod
Me.FirstMethod()
End Sub
Public Sub ThirdMethod() Implements ISomeInterface.ThirdMethod
Console.WriteLine("ThirdMethod")
End Sub
End Class
The method that implements ISomeInterface.FirstMethod is not named FirstMethod so it doesn't clash with the existing method with that name and it is also Private so it cannot be accessed from outside using a reference of type SomeClass. Using a reference of type ISomeInterface is another matter though. If you use code like this:
Dim sc As ISomeInterface = New SomeClass
sc.FirstMethod()
sc.ThirdMethod()
you'll find that the FirstMethodInternal method of your SomeClass object gets invoked and, in turn, invokes the FirstMethod method of the same object. Try running that code and placing breakpoints on the FirstMethod and FirstMethodInternal methods to prove it to yourself.

Implementing Inherited Interface with Overloaded Member vb.net

I am trying to implement a derived interface in a class. My interfaces and class are similar to the following. The Namespaces relate to different projects that hold these interfaces and the class:
Namespace ns1
Public Interface IParent
Function myFunction() As Double
End Interface
End ns1
Namespace ns2
Public Interface IDerived
Inherits ns1.IParent
Overloads / Shadows Function myFunction(ByRef myObject as Object) As Double
End Interface
End ns2
Namespace ns3
Public Class myClass
Implements ns2.IDerived
Public Function myFunction(ByRef obj as Object) As Double Implements ns2.IDerived.myFunction
End Function
End ns3
In the derived interface, I am trying to overload the function in a way that when I implement the derived interface, I only have to implement the function as defined therein - as is done with the code above on "myClass". However, I am getting an error saying I have to also implement the function from the parent interface (with the empty argument list). The error exists regardless of my using Overloads or Shadows on the function in the derived interface - both cause the error.
Is there anyway to accomplish what I am trying to do - implement only the derived interface's function in my class - using interfaces? If there is not a way using interfaces, can anyone suggest an alternate way? We really need to use interfaces and are trying to avoid using classes. That said, abstract classes my allow us to do all we need to do with these.
I have read a lot of info on all the topics covered by this question as every concept is pretty basic and well covered in online help. But, I have not found anything that I recognize as a direct solution to my specific issue.
Thanks in advance for any help.
I don't know if this is a typo but you have two distinct methods: one that takes no parameter, and another that takes an object, so the compiler requirement is legitimate.
If this is a typo and that you have only one method, say "myFunction()", then I fear VB.Net does not act like C# by simply hiding the base interface and allowing to only implement the derived one.
But you could easily fix this by forwarding:
Namespace ns1
Public Interface IParent
Function myFunction() As Double
End Interface
End Namespace
Namespace ns2
Public Interface IDerived
Inherits ns1.IParent
Function myFunction() As Double
End Interface
End Namespace
Namespace ns3
Public Class Class1
Implements ns2.IDerived
Public Function myFunction() As Double Implements ns2.IDerived.myFunction
Return 42
End Function
Private Function myFunction1() As Double Implements ns1.IParent.myFunction
Return myFunction()
End Function
End Class
End Namespace
Module Module1
Sub Main()
Dim cp As ns1.IParent = New ns3.Class1
cp.myFunction()
Dim cd As ns2.IDerived = New ns3.Class1
cd.myFunction()
End Sub
End Module
EDIT:
So was not a typo, here is the standard (good/best practice?) fix:
Public Class Class1
Implements ns2.IDerived
Public Function myFunction(ByRef obj As Object) As Double Implements ns2.IDerived.myFunction
End Function
Public Function myFunction() As Double Implements ns1.IParent.myFunction
Throw New NotImplementedException("'ns1.IParent.myFunction' has not been implemented because unicorns can't fly!")
End Function
End Class
I don't believe that what you want to accomplish is possible the way you are trying... As I recall when you inherit an Interface any class that implements your derived Interface is actually being told that it must implement both Interfaces rather allowing the options you have in a full class.
So effectively what you have in myClass is:
Namespace ns3
Public Class myClass
Implements ns2.IDerived
Implements ns1.IParent
Public Function myFunction(ByRef obj as Object) As Double Implements ns2.IDerived.myFunction
End Function
End ns3
So inheriting an interface is really just a way to enforce that a class implementing the derived interface must also implement the base interface.

Implements through inheritance

I'm having issues implementing an interface which vb.net is not picking it up through inheritance, not sure its possible but i believe there should be a way
Public Interface ITest
Sub SomeMethod()
End Interface
Public Class Test
Inherits OtherClass
Implements ITest
End Class
Public Class OtherClass
Sub SomeMethod()
End Sub
End Class
Class "OtherClass" is on another assembly where that ITest interface does not live and I cant have it there. However, with c# you are not forced to use the Implements keyword on each method, c# looks through the name and signatures.
So basically, Im getting the "Missing implementation of members" and what I was expecting was to be able to compile fine since Im inheriting a class that has the same methods defined on the interface.
Is this possible?
For OtherClass to implement the ITest interface, it needs a reference to that ITest interface.
Once you have it referenced, then your OtherClass would look like this:
Public Class OtherClass
Implements ITest
Sub SomeMethod() Implements ITest.SomeMethod
End Sub
End Class

How to indicate that an Entity Framework object is implementing an interface? [duplicate]

This question already has answers here:
Partial Classes, LINQ, Interfaces and VB.NET
(2 answers)
Closed 2 years ago.
OK, I have to begin saying that I'm working with Visual Basic. The problem is that I have a Entity Framework object and I want to indicate that this object implements a interface, for example:
Public Interface ICatalog
Property created_at() As Date
Property id() As Long
End Interface
Those properties are allready in the object. In c# I've done this just by declaring a partial class of the object and indicates that implements that interface, but in basic is not working, I supouse that is because of the language sintaxis used to declare that a property is implementing some property of the interface, for example:
Public Property created_at() As Date Implements ICatalog.created_at
So is there any other way to accomplish this?
Take a look at this example.
Namespace MyAppDomain
Public Interface IFoo
Sub Bar()
End Interface
Public Interface IPerson
Function Gender() As String
End Interface
Public Class MyFooPerson : Implements IFoo, IPerson
Public Sub New()
End Sub
Public Sub Bar() Implements IFoo.Bar
End Sub
Public Function Gender() As String Implements IPerson.Gender
Return Nothing
End Function
End Class
End Namespace
You'll notice the MyFooPerson Class implements the IFoo Interface as well as the IPerson Interface. Each method then implements the corresponding Interface method.
Your example doesn't say whether or not the Class containing Public Property created_at() As Date Implements ICatalog.created_at is Implementing the ICatalog Interface.