How can i cast my interface object to my current object - vb.net

I have following interface and class
Public Interface IMyInterface
Sub GenericImplementation()
End Interface
Public Class MyClass
Implements IMyInterface
Public Sub GenericImplementation() implements IMyInterface.GenericImplementation
'doing some stuff
end Sub
Public Sub DoRefresh()
'doing some stuff
End Sub
End Class
Now i have some code where i want to check if my object passed as interface is of the concrete type MyClass and then perform DoRefresh()
Public Sub TestMethod(testObject As IMyInterface)
Dim test = TryCast(testObject, MyClass)
If test IsNot Nothing Then
test.DoRefresh()
End If
End Sub
When i am debugging the code i can see clearly the my instance of the interface is a ´MyClass´. But when i TryCast this it return nothing.

Related

VB.Net Class Inherit Sub New

I'm currently trying to convert some basics in C# class methods into VB, and I'm having some difficulties.
Say we have two classes, baseClass and newClass
Public Class baseClass
Public Sub New(ByVal obj As Object)
'... do something here
End Sub
End Class
Public Class newClass
Inherits baseClass
Public Sub New(ByVal obj as Object)
'... do something here
End Sub
end Class
However, whenever I tried to declear something like
dim nc as newClass(myObj)
I kept getting an error telling me that
First statement of this 'Sub New' must be a call to 'MyBase.New' or 'MyClass.New' because base class 'baseClass' of 'newClass' does not have an accessible 'Sub New' that can be called with no arguments.
When I was trying to do something like, if written in C#, like this
public partial class newClass : baseClass
{
public newClass(Object obj) : baseClass(obj)
{
// do something here
}
}
I understand that the debugger is telling me just add an empty public sub new() in baseClass, but that's not what I'm trying to do, I wanted it to invoke the other constructor, so hopefully someone can point me in the right direction, since I've been trying to find an easy conversion guide but I couldn't find any.
Any answer is much appreciated.
Public Class newClass
Inherits baseClass
Public Sub New(ByVal obj as Object)
MyBase.New(obj)
End Sub
end Class

How to cast object of type 'especific' to type 'FileHelpers.Events.INotifyRead in Multirecording

I'm trying to centralize all formatting and conversion rule in a single class especific. Using the interface INotifyRead(Of T As Class). When I implement the methods BeforeRead/AfterRead throws an exception: Unable to cast object of type 'Especific' to type 'FileHelpers.Events.INotifyRead`1[System.Object]'.
Below my code.
Using engine As New MultiRecordEngine(New RecordTypeSelector(AddressOf CifraRecordTypeSelector),
GetType(RemessaRegistroCliente),
GetType(RemessaRegistroContrato))
Dim records = engine.ReadFile(_camArquivo)
End Using
Public NotInheritable Class RemessaRegistroCliente
Implements INotifyRead(Of RemessaRegistroCliente)
Public Sub AfterRead(e As AfterReadEventArgs(Of RemessaRegistroCliente)) Implements INotifyRead(Of RemessaRegistroCliente).AfterRead
End Sub
Public Sub BeforeRead(e As BeforeReadEventArgs(Of RemessaRegistroCliente)) Implements INotifyRead(Of RemessaRegistroCliente).BeforeRead
End Sub
End Class
Public NotInheritable Class RemessaRegistroContrato
Implements INotifyRead(Of RemessaRegistroContrato)
Public Sub AfterRead(e As AfterReadEventArgs(Of RemessaRegistroContrato)) Implements INotifyRead(Of RemessaRegistroContrato).AfterRead
End Sub
Public Sub BeforeRead(e As BeforeReadEventArgs(Of RemessaRegistroContrato)) Implements INotifyRead(Of RemessaRegistroContrato).BeforeRead
End Sub
End Class
Since MultiRecordEngine has no generic version, you cannot implement the generic INotifyRead(Of T) to handle events. Instead, assign delegates to the engine.
Sub Main()
Using engine As New MultiRecordEngine(New RecordTypeSelector(AddressOf CifraRecordTypeSelector),
GetType(RemessaRegistroCliente),
GetType(RemessaRegistroContrato))
AddHandler engine.BeforeReadRecord, AddressOf BeforeReadRecordHandler
End Using
End Sub
Private Sub BeforeReadRecordHandler(ByVal engine As EngineBase, ByVal e As BeforeReadEventArgs(Of Object))
End Sub
You'll also need to modify your classes to remove the generic interfaces:
Public NotInheritable Class RemessaRegistroCliente
'your fields
End Class
Public NotInheritable Class RemessaRegistroContrato
'your fields
End Class

Inner class Factory Method

I have tried to implement the Factory method with a trick, but for some reason is not working:
Public Class MetamodelElement
Public Class MetamodelElementFactoryBase
End Class
Private Sub New()
End Sub
End Class
Public Class MetamodelElementFactory : Inherits MetamodelElement.MetamodelElementFactoryBase
Function CreateMetamodelElement() As MetamodelElement
Return New MetamodelElement()
End Function
End Class
It says that the class does not have access to the private method (constructor).
I have followed an example from C# in this post:
Hidden Features of C#?
The compiler complains that you are trying to use the Private constructor from outside of the class. That is not allowed. So either make it Public or don't call it.
Public Class MetamodelElement
Public Class MetamodelElementFactoryBase
End Class
Public Sub New() ' <---- HERE!!! Now it works because it's public
End Sub
End Class
Public Class MetamodelElementFactory
Inherits MetamodelElement.MetamodelElementFactoryBase
Function CreateMetamodelElement() As MetamodelElement
Return New MetamodelElement() ' <--- HERE was the error
End Function
End Class
You can access private class members only from inside of the class.
MSDN: Access Levels
According to the C# code that you've linked you have to move the constructor into the class that you are inheriting from. Then you can also use Protected:
Public Class MetamodelElement
Public Class MetamodelElementFactoryBase
Protected Sub New()
End Sub
End Class
End Class

VB generics with constraints -- type casting and inheritance?

Take this scenario:
Public Interface IMyClass
End Interface
Public mustinherit class MyBaseClass : implements IMyClass
End Class
public class MyClass : inherits MyBaseClass
End Class
public class MyModel(of t as IMyClass)
private Dim _parameter as t
Public Sub New(byval parameter As t)
_parameter As t
End Sub
End class
In my controller, I can do this with no problem:
Dim _myclass as IMyClass = new MyClass()
Can I do something similar with this:
Dim _myModel as MyModel(of IMyClass) = new MyModel(of MyClass)
???
My initial thought was wrong, as I thought the conversion could be done automatically, but it appears it is not done. Any way to achieve the same thing within .NET?
EDIT
I updated the MyModel class to show more of what I was doing. I want to constrain the instance I create, but then do what would be a narrowing conversion with traditional, non-generics code. Basically, my partial Razor views would require the explicit model, and those views end up rendering another view that will take that model and display it. Because the models all implement or inherit a class that implements IMyClass, all the methods should exist on all of the instances and should be callable but the types are not interchangable.
Let’s modify MyModel slightly, shall we?
Public Class MyModel(Of T As IMyClass)
Private _parameter As T
Public Sub Something(parameter As T)
_parameter = parameter
End Sub
End class
Public Class MyClassA : Inherits MyBaseClass
End Class
Public Class MyClassB : Inherits MyBaseClass
End Class
Dim _myModel As MyModel(Of IMyClass) = New MyModel(Of MyClassA)()
_myModel.Something(New MyClassB()) ' Boom!
If the assignment were allowed the last line would pose a problem: MyMode(Of MyClassA)._parameter has type MyClassA but the last line would assign an object of the (unrelated) type MyClassB. This is illegal and so VB forbids it.
Do you need multiple varieties of MyModel, or are you just attempting to require that the stored object be constrained to IMyClass?
Simplest approach (that might not do everything you need):
Public Interface IMyClass
Sub DoIt()
End Interface
Public Class MyModel
Private ReadOnly _parameter As IMyClass
Public Sub New(parameter As IMyClass)
_parameter = parameter
End Sub
Public Sub DoItToIt()
_parameter.DoIt()
End Sub
End Class
Public Class MyClassA
Implements IMyClass
Public Sub DoIt() Implements IMyClass.DoIt
End Sub
End Class
Public Class Tests
Public Sub Main()
Dim model1 As MyModel = New MyModel(New MyClassA)
model1.DoItToIt()
End Sub
End Class
Next step up in complexity is to define an interface IHasMyClass for classes that contain an IMyClass. This supports manipulations based on the allowed type, and the actual type, of the contained object:
Public Interface IMyClass
Sub DoIt()
End Interface
Public Interface IHasMyClass
Function GetIt() As IMyClass
Function GetItsType() As Type
Function GetAllowedType() As Type
End Interface
Public Class MyModel(Of T As IMyClass)
Implements IHasMyClass
Private ReadOnly _parameter As IMyClass
Public Sub New(parameter As IMyClass)
_parameter = parameter
End Sub
Public Sub DoItToIt()
_parameter.DoIt()
End Sub
Public Function GetItAsT() As T
Return _parameter
End Function
Public Function GetIt() As IMyClass Implements IHasMyClass.GetIt
Return _parameter
End Function
Public Function GetItsType() As Type Implements IHasMyClass.GetItsType
Return _parameter.GetType()
End Function
Public Function GetAllowedType() As Type Implements IHasMyClass.GetAllowedType
Return GetType(T)
End Function
End Class
Public Class MyClassA
Implements IMyClass
Public Sub DoIt() Implements IMyClass.DoIt
End Sub
End Class
Public Class Tests
Public Sub Main()
' Allow any IMyClass
Dim model1 As MyModel(Of IMyClass) = New MyModel(Of IMyClass)(New MyClassA)
model1.DoItToIt()
Dim it As IMyClass = model1.GetIt()
Dim allowedT As Type = model1.GetAllowedType()
' Restrict to MyClassA
Dim modelA As MyModel(Of MyClassA) = New MyModel(Of MyClassA)(New MyClassA)
modelA.DoItToIt()
Dim itA1 As IMyClass = modelA.GetIt()
Dim itA2 As MyClassA = modelA.GetItAsT()
Dim allowedTA As Type = modelA.GetAllowedType()
End Sub
End Class
In Tests(), notice that we now need to declare whether we are creating a MyModel that accepts ANY IMyClass MyModel(Of IMyClass), or one that requires a specific sub-class MyModel(Of MyClassA).
If we want to manipulate MyModels, that may be either of the above types, we use the common interface:
Dim model As IHasMyClass
model = model1
...
model = modelA
Or in your case, to support all the functionality of MyModel, rename IHasMyClass as IMyModel, and add the various MyModel functions, but instead of T, use IMyClass:
Public Interface IMyModel
Function GetIt() As IMyClass
Function GetItsType() As Type
Function GetAllowedType() As Type
Sub DoItToIt()
Function CompareIt(other As IMyClass) As Integer
End Interface
And make appropriate changes/additions to IMyClass and MyModel.
Then it becomes possible to do:
Dim model As IMyModel = modelA
If model.CompareIt(model1.GetIt()) > 0 ...

how to access class from inherited class

I have two classes:
class class2
inherits class1
public sub modify()
'modify property of class1
end sub
end class
How can I modify class1 in a sub in class2?
You just call it. Example:
Public Class class1
Private _Value As String = String.Empty
Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
_Value = value
End Set
End Property
End Class
Public Class class2
Inherits class1
Public Sub modify()
Value = "modified"
End Sub
End Class
And to show it works:
Dim c2 As New class2
c2.modify()
MessageBox.Show(c2.Value)
You are asking about properties, note that only protected and public properties are visible to inherited classes.
You need the MyBase keyword when you are overriding an existing function in the parent class. Other protected or public properties or functions can be accessed regulary without any special keyword.
One tip I wanted to add to the above comments regarding accessing base class info is where you have a base class without a default contructor or want to use a specific constructor This is a good opportunity to use Mybase. You have to call the constructor before any additional actions take place in this scenario.
Public Class MyClass
Inherits baseClass
Public Sub New()
mybase.new("Oranges")
End Sub
End Class
Public Class baseClass
Private _someVariable as String
Public Sub New(byval passedString as string)
_someVariable = passedString
End Sub
End Class