Can I use an interface to accomplish this task? - vb.net

Hi StackOverflow Community,
I have a specific situation dealing with Interfaces in VB.Net.
Particularly, I have one function that should be able to run for every child class the interface has. Here is a simple example:
Public Interface Book
Function TurnPage() As Page
Sub OpenBook()
Sub CloseBook()
End Interface
Public Class Reader
Public Sub Reading()
OpenBook()
TurnPage()
CloseBook()
End Sub
End Class
Public Class Magazine
Implements Book
Public Function TurnPage() As Page
Implements Book.TurnPage
' Code implementation for TurnPage
End Function
Public Sub OpenBook()
Implements Book.TurnPage
' Code implementation for OpenBook
End Sub
Public Sub CloseBook()
Implements Book.TurnPage
' Code implementation for CloseBook
End Sub
End Class
Public Class Novel
Implements Book
Public Function TurnPage() As Page
Implements Book.TurnPage
' Code implementation for TurnPage
End Function
Public Sub OpenBook()
Implements Book.TurnPage
' Code implementation for OpenBook
End Sub
Public Sub CloseBook()
Implements Book.TurnPage
' Code implementation for CloseBook
End Sub
End Class
How can I use the Sub in Reader(Reading) for all the child classes(Magazine and Novel) for that particular interface(Book)?

Change you Reader to accept a Book:
Public Class Reader
Public BookToRead as Book
Public Sub New(ABook As Book)
Me.BookToRead = ABook
End Sub
Public Sub Reading()
BookToRead.OpenBook()
BookToRead.TurnPage()
BookToRead.CloseBook()
End Sub
End Class
Then when you need a reader to read a book:
Dim ThreeMusketeers As New Novel()
Dim reader1 as New Reader(ThreeMusketeers) // Novel
reader1.Reading()
Dim NewYorkTimes As New Magazine()
Dim reader2 as New Reader(NewYorkTimes)
reader2.Reading()

Related

how to dont call contruvtor Inherits in vb

I want to call Dim objFkkiNinteiJokyoRpt As New A(objCsv) in class C. But class A Inherits Common. If now call contructor will An error occurred.Because type Object C.CsvGenerator diference type Object in common. I thnk now stop call to contructor common (or any other way) but i dont know how to do. Helf me please. Sorry because my english so bad
Public Class A Inherits Common
Public _objCsv As C.CsvGenerator
Friend Sub New(ByVal objCsv As C.CsvGenerator)
_objCsv = objCsvGenerator
End Sub
Public Sub New()
End Sub
End Sub
Public Sub New(ByVal objRSReportObj As Object)
MyBase.New(objRSReportObj)
InitializeReport()
End Sub
End Class
Public Class C
Private Function SelectCSV
Dim objCsv As New CsvGenerator("")
Dim objFkkiNinteiJokyoRpt As New A(objCsv)
End Function
Friend Class CsvGenerator
Inherits cmShare.cmObject
End Class
End Class
Public Class Common
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal objRSReport As Object)
MyBase.New(objRSReport)
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

VB.NET Inheritance issue

I've got A base class Base and Sorter and Parser classes derived from it .
The same thing with BaseResult with derived SorterResult and ParserResult.
Base has a Result field of BaseResult type, BaseResult has a Log field.
The reason why I've used a Base class, is because both of Parser and Sorter must write a Log.
Here's my code:
Public MustInherit Class Base
Public Result As BaseResult
Event LogChanged()
Protected Sub AddLogLine(ByVal _logString As String)
If Not String.IsNullOrWhiteSpace(_logString) Then Result.Log.Add(_logString)
RaiseEvent LogChanged()
End Sub
End Class
Public Class Sorter
Inherits Base
Public Shadows Result As SorterResult
Sub New()
Result = New SorterResult With {.Log = New List(Of String)}
End Sub
Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
Public Class Parser
Inherits Base
Public Shadows Result As ParserResult
Sub New()
Result = New ParserResult With {.Log = New List(Of String)}
End Sub
Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
Public MustInherit Class BaseResult
Public Log As List(Of String)
End Class
Public Class SorterResult
Inherits BaseResult
'//SorterResult fields
End Class
Public Class ParserResult
Inherits BaseResult
'//ParsedResult fields
End Class
The issue here is that compiler sais(on pic below):
"variable 'Result' conflicts with variable 'Result' in the base class 'Base' and should be declared 'Shadows'." When I used Shadows keyword, warning disappeared, but I get a null reference exception on this line, because Result field is Nothing:
If Not String.IsNullOrWhiteSpace(_logString) Then Result.Log.Add(_logString)
I can't assign a value to a Result variable in Base class constructor, because It must be of type SorterResult in Sorter, and ParserResult in Parser. What is the regular pattern here? Sorry my bad english.
Use generics
Public MustInherit Class Base(Of TResult As BaseResult)
Public Result As TResult
Event LogChanged()
Protected Sub AddLogLine(ByVal _logString As String)
If Not String.IsNullOrEmpty(_logString) Then Result.Log.Add(_logString)
RaiseEvent LogChanged()
End Sub
Public MustOverride Sub Go()
End Class
Public Class Sorter
Inherits Base(Of SorterResult)
Sub New()
Result = New SorterResult With {.Log = New List(Of String)}
End Sub
Public Overrides Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
Public Class Parser
Inherits Base(Of ParserResult)
Sub New()
Result = New ParserResult With {.Log = New List(Of String)}
End Sub
Public Overrides Sub Go()
AddLogLine("Sorter started")
End Sub
End Class
However, this is not a "beautiful" inheritance hierarchy. Inheritance should formulate relations like "a student is a person" where student derives from person. What do sorters and parsers have in common? Are they a Base? Are they loggers? Are they commands (as suggests the Go method)? Is inheritance required here? Wouldn’t it be more appropriate to use aggregation? I would declare a completely independent logger class and inject it into classes. This allows you to be more flexible, as it enables you to inject different types of loggers.
Public MustInherit Class Logger
Public Event LogChanged()
Public MustOverride Sub AddLogLine(ByVal message As String)
Protected Sub OnLogChanged()
RaiseEvent LogChanged()
End Sub
End Class
Public Class TextFileLogger
Inherits Logger
Public Overrides Sub AddLogLine(ByVal message As String)
If Not String.IsNullOrEmpty(message) Then
'TODO: Write message to log file
OnLogChanged()
End If
End Sub
End Class
You can inject it like this:
Public Class SomeConsumerClass
Private _logger As Logger
Sub New(ByVal logger As Logger)
_logger = logger
End Sub
Public Sub DoSomething()
_logger.AddLogLine("Did something!")
End Sub
End Class
Use like this:
Dim obj As New SomeConsumerClass(New TextFileLogger())
obj.DoSomething()
If you have another kind of logger (XmlFileLogger, StringListLogger, DatabaseLogger...) it is now easy to use it without having to change all the classes using it.
Maybe you should even have only one global logger:
Dim globalLogger As New TextFileLogger()
Dim sorter As New Sorter(globalLogger)
Dim parser As New Parser(globalLogger)

NUnit says the test was successful

I am new to Unit Testing. The Unit Tests are succeeding in the below. I expect them to fail because Student.getCourse, unidergraduate.getCourse1 and postgraduate.getcourse1 return different values to what is being tested for e.g. "Student Course" <> "Student Course 1".
Imports NUnit.Framework
Imports NMock
Imports StudentCL
Namespace Test
<TestFixture()>
Public Class StudentTest
Private _mocks As MockFactory
Private _StudentMock As Mock(Of Student)
Private _GraduateMock As Mock(Of Graduate)
Private _UndergraduateMock As Mock(Of UnderGraduate)
<SetUp()>
Public Sub SetUp()
_mocks = New MockFactory()
_StudentMock = _mocks.CreateMock(Of Student)()
_GraduateMock = _mocks.CreateMock(Of Graduate)()
_UndergraduateMock = _mocks.CreateMock(Of UnderGraduate)()
End Sub
<Test()>
Public Sub getStudentCourse()
_StudentMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Student Course1")
End Sub
<Test()>
Public Sub getGraduateCourse()
_GraduateMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Graduate Course1")
End Sub
<Test()>
Public Sub getUndergraduateCourse()
_UndergraduateMock.Expects.One.Method(Function(x) x.getCourse1).WillReturn("Undergraduate Course1")
End Sub
End Class
End Namespace
Public MustInherit Class Student
Public Overridable Function getCourse1() As String
Return "Student course"
End Function
End Class
Public Class Graduate
Inherits Student
Public Overrides Function getCourse1() As String
Return "Graduate course"
End Function
End Class
Public Class UnderGraduate
Inherits Student
Public Overrides Function getCourse1() As String
Return "Undergraduate course"
End Function
End Class
What am I missing?
Your code isn't actually testing anything. It is simply setting up mocks. By themselves, mocks are useless. From what you've written, it appears you want to test the result of the getCourse1 function. An example of one of your tests might look like the following.
<Test()> _
Public Sub getCourse1_WhenCalled_ReturnsExpected()
' Arrange
Dim student As New Graduate()
' Act
Dim course As String = student.getCourse1()
' Assert
Assert.That(course, Iz.EqualTo("Graduate Course1")) ' This will fail as you expected.
End Sub
The test method name follows a convention described in the book The Art of Unit Testing. The test body is arranged in a sequence known as the AAA pattern.

Organizing VB.Net Mehods

Say I have a class with several methods within it. I want to organize the methods into groupings that can be accessed without constructing a new object each time. The purpose is to group the methods of the class into logical buckets
For instance:
Dim myclass as MyCustomClass
myclass.Shipping.Get_List()
myclass.Production.Get_List()
What is the best way to do this? I tried nested classes, but VB.NET won't let me access the methods as shown above.
so this is how i would do what you want
this is not the best design of the world but it would work
I would suggest you to move the actual get_list and other kind of method / property into the specific class while keeping the common one in the parent class, which in this case is test
but then, I have no idea what your code look like so from that point on, it's your choice
Module Module1
Sub Main()
Dim test As New test
test.Production.Get_List()
test.Shipping.Get_List()
End Sub
End Module
Public Class Shipping
Private parent As test
Public Sub New(ByRef parent As test)
Me.parent = parent
End Sub
Public Function Get_List() As List(Of Integer)
Return parent.GetShipping_List
End Function
End Class
Public Class Production
Private parent As test
Public Sub New(ByRef parent As test)
Me.parent = parent
End Sub
Public Function Get_List() As List(Of Integer)
Return parent.GetProduction_List
End Function
End Class
Public Class test
Public Property Production As Production
Public Property Shipping As Shipping
Public Function GetShipping_List() As List(Of Integer)
Return Nothing
End Function
Public Function GetProduction_List() As List(Of Integer)
Return Nothing
End Function
Public Sub New()
Production = New Production(Me)
Shipping = New Shipping(Me)
End Sub
End Class
With caution that you more than likely should re-evaluate your architecture, you could implement your pattern like this:
Public Class MyCustomClass
Private _shippingList As List(Of String)
Private _productionList As List(Of String)
Public Production As ProductionClass
Public Shipping As ShippingClass
Public Sub New()
Production = New ProductionClass(Me)
Shipping = New ShippingClass(Me)
End Sub
Public Class ShippingClass
Private _owner As MyCustomClass
Public Sub New(owner As MyCustomClass)
_owner = owner
End Sub
Public Function Get_List()
Return _owner._productionList
End Function
End Class
Public Class ProductionClass
Private _owner As MyCustomClass
Public Sub New(owner As MyCustomClass)
_owner = owner
End Sub
Public Function Get_List()
Return _owner._productionList
End Function
End Class
End Class
However, if your true intent is simply organizing the methods in a more accessible and logical manner, I would suggest considering:
Public Class MyCustomClass
Public Sub ShippingListGet()
End Sub
Public Sub ShippingListAddTo()
End Sub
Public Sub ShippingThatDO()
End Sub
Public Sub ShippingThisDo()
End Sub
Public Sub ProductionListGet()
End Sub
Public Sub ProductionListAddTo()
End Sub
Public Sub ProductionThisDo()
End Sub
Public Sub ProductionThatDo()
End Sub
End Class
Keep in mind, some people consider that difficult to read. I personally prefer organization along those lines so when the methods are sorted alphabetically they group logically.
I have found the solution I was looking for using interfaces
Public Interface ICompany
Function Company_List() As DataTable
End Interface
Public Class MainClass
Public Company As ICompany = New CompanyClass
Public Sub New()
MyBase.New()
End Sub
Private Class CompanyClass
Public Sub New()
MyBase.New()
End Sub
Public Function Company_List() As DataTable
My code....
End Function
End Class
End Class