0

I have the following loop in RunWebTest method which can work with different type of classes passed to it. Now the issue is that I don't know how to modify this code so I could pass different parameters type to the constructor of the class:

Public Class TestManager
    Inherits ThreadedWebTest
    Public Property ServerURL As String

    Public Overrides Sub Run()
        RunWebTest(New Login(Users.CustomerTradeAdvanced))
        RunWebTest(New CustomerCreate)
        RunWebTest(New Logout)
    End Sub

    Private Sub RunWebTest(Of WebTestType)(test As WebTestType)
        For Each r As WebTestRequest In IncludeWebTest(GetType(WebTestType).GetConstructor(New System.Type() {}).Invoke(New Object() {}), False)
            MyBase.Send(r)
        Next
    End Sub
End Class

I'd appreciate your help

Afflatus
  • 933
  • 1
  • 12
  • 39
  • You should create 2 constructors `New()` with different signatures, one with default setting and another with your parameters. – Nadeem_MK Sep 22 '14 at 06:58
  • @Nadeem_MK I have no issue withe defining two or more different constructor. I don't know how to adjust RunWebTest method to accept both cases. – Afflatus Sep 22 '14 at 07:05

3 Answers3

1

It's not clear what you are trying to do, but you should be able to make your class generic. Are all of your classes related in some way? Do they inherit from the same base class or implement the same interface?

Public Class TestManager(Of T)
    Inherits ThreadedWebTest

    Public Property ServerURL As String

    Public Overrides Sub Run()
        RunWebTest(New T())
    End Sub

    Private Sub RunWebTest(Of T)(test As T)
        'code here
    End Sub
End Class
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
0

This may help out. I tried generics and they did not come together as well as this. You can pass into your method what type you expect based on what NEW constructor you hit, then change types from there, see below:

 Private Class TestTypeClass

    Private Sub New(str As String)
        str = "hi"
        str = TryCast(str, Object)

        Dim myType As String = "string"

        RunWebTest(str, myType)
    End Sub
    Public Sub New(int As Integer)
        int = 99
        int = TryCast(int, Object)

        Dim myType As String = "integer"

        RunWebTest(int, myType)
    End Sub

    Private Sub RunWebTest(myThing As Object, myType As String)

        Select Case myType
            Case "string"
                myThing = DirectCast(myThing, String)
                'Do string logic here, or call method from here doing string logic.
            Case "integer"
                myThing = DirectCast(myThing, Integer)
                'Do integer logic here, or call method from here doing integer logic.
        End Select

    End Sub

End Class
Adam
  • 490
  • 7
  • 21
  • This looks like factory method somehow. but my issue is that the directCast throws an error as I try to cast it to my class type. – Afflatus Sep 24 '14 at 03:26
0

The key was to use the base class as the parameter of the method. This code works now:

   Public Overrides Sub Run()
        RunWebTestRequests(New Login(Users.CustomerTradeAdvanced))
        RunWebTestRequests(New CustomerCreate)
        RunWebTestRequests(New SalesInvoice)
        RunWebTestRequests(New Logout)
    End Sub

    Private Sub RunWebTestRequests(webTest As ThreadedWebTest)
        For Each r As WebTestRequest In IncludeWebTest(webTest, False)
            MyBase.Send(r)
        Next
    End Sub
Afflatus
  • 933
  • 1
  • 12
  • 39