create/use array of different classes?

  • Thread starter Thread starter Ron
  • Start date Start date
R

Ron

Hello,

I have 4 classes that use 4 DTS packages on 4 different
tables. So I have
Dim cls1 As New clsDTS1, cls2 As New clsDTS2
Dim cls3 As New clsDTS3, cls4 As New clsDTS4

Each class has a common property called DataPath
cls1.DataPath = strPath

I want to use these classes in a loop, so I need to put
them in some kind of array. Obviously I can't use a class
array because they are all different classes. So I tried
using an object array like this:

Dim dts() As Object = {cls1, cls2, cls3, cls4}
For i As Integer = 0 To dts.Length - 1
Ctype(dts(i), dts(i)).DataPath = strPath(i)
....

I had a problem with Ctype(dts(i), dts(i)) where the
compiler said it needed a type for the 2nd dts(i), so I am
kind of in an infinite loop here. Could anyone suggest
what kind of collection object/structure I could use to
loop through the 4 different classes?

Thanks,
Ron
 
Well, I came up with one solution that isn't real dynamic,
but it seems to work:

For i As Integer = 1 to dts.Length
If i = 1 Then Ctype(dts(i), clsDTS1).DataPath = strPath(i)
If i = 2 Then Ctype(dts(i), clsDTS2).DataPath = strPath(i)
If i = 3 Then Ctype(dts(i), clsDTS3).DataPath = strPath(i)
If i = 4 Then Ctype(dts(i), clsDTS4).DataPath = strPath(i)
....

Any suggestions appreciated if there is a better way.
 
Can you change your classes? It just seems like a bad design. You should
have an interface with properties and methods that are common to your DTS
classes and then have all your classes implement that interface. Something
like this:

Interface IDts
Property DataPath() As String

Sub ExecutePackage()
End Interface

Class clsDTS1
Implements IDts

Private strPath As String

Public Sub ExecutePackage() Implements IDts.ExecutePackage
' execute your dts package..
End Sub

Public Property Path() As String Implements IDts.DataPath
Get
Return strPath
End Get
Set(ByVal Value As String)
strPath = Value
End Set
End Property
End Class

similarly define clsDTS2, clsDTS3 and clsDTS4.

then you can do something like:

Dim DTS(3) as IDts
Dim strPath() As String = {"path1", "path2", _
"path3", "path4"}

DTS(0) = New clsDTS1
DTS(1) = New clsDTS2
DTS(2) = New clsDTS3
DTS(3) = New clsDTS4

For i As Integer = 0 To DTS.Length - 1
DTS(i).DataPath = strPath(i)
DTS(i).ExecutePackage
Next i


hope that helps..
Imran.
 
That is very nice. I am pretty sure this is what I was
looking for. Thanks very much.

Question:

Where do I place
------------------------------------
Interface IDts
Property DataPath() As String

Sub ExecutePackage()
End Interface
-------------------------------------

I am guessing that I only state this once in my project.
Do I place it in a module? the main form class?, or any
of the other classes?

Thanks again,
Ron
 
Ron said:
That is very nice. I am pretty sure this is what I was
looking for. Thanks very much.

Question:

Where do I place
------------------------------------
Interface IDts
Property DataPath() As String

Sub ExecutePackage()
End Interface
-------------------------------------

I am guessing that I only state this once in my project.
Do I place it in a module? the main form class?, or any
of the other classes?

That's right. You only need it to be in one place - not necessarily in your
project also but it would sure make more sense placing it in your project.
You can place it along with your classes - even in the same file if you
want. Just one note - if you want to be able to use the interace outside
your project, make sure to define the interface as public.

hope that helps..
Imran.
 
I went and place the Interface declaration in the first
class

*******************************************
Option Strict On
Option Explicit On
Option Compare Binary

Imports DTS
Imports System.Data.SqlClient

Interface IDts
Property DataPath() As String
Sub ExecutePackage()
End Interface

Public Class clsDTS1
Implements IDts

Private goPackageOld As New DTS.Package
Private goPackage As DTS.Package2
Private strDataPath As String
...
************************************************

It appears to work perfectly. Thanks a bunch for your
help.
 
Ron,
Instead of /in addition to an Interface as Imran suggests I would consider a
base class, seeing as all the classes have a DataPath, a base class would
have the DataPath property.

This allows your four classes to share common logic, if all 4 have DataPath,
you can put DataPath in the base class once. If all 4 have goPackage, you
can put goPackage in the base class once. Plus it allows each derived class
to have specific logic, if only clsDTS1 has a DTS1Specific integer property,
you only add tDTS1Specific to clsDTS1.

Using a base class instead of an Interface can significantly reduce
duplicate code. Interfaces are useful when you already have a specific base
class you need to use, such as System.Windows.Forms.Form...

You can use Overridable & MustOverride in the base class to allow your
derived classes to change to override (Overrides keyword) methods of the
base class.

Something like:

Public MustInherit Class DtsBase
Private goPackageOld As New Dts.Package
Private goPackage As Dts.Package2
Private strDataPath As String

public Property DataPath() As String
Get

End Get
Set(ByVal Value As String)

End Set
End Property

' derived classes must supply this method
Public MustOverride Sub ExecutePackage()

End Class

Public Class clsDTS1
Inherits DtsBase

Public Property DTS1Specific As Integer
Get

End Get
Set(ByVal Value As String)

End Set
End Property

Public Overrides Sub ExecutePackage()
' DTS1 specific logic
End Sub

End Class

Public Class clsDTS2
Inherits DtsBase

Public Property DTS2Specific As String
Get

End Get
Set(ByVal Value As String)

End Set
End Property

Public Overrides Sub ExecutePackage()
' DTS2 specific logic
End Sub

End Class

Robin A. Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and
Microsoft Visual C# .NET - Step by Step" from Microsoft Press covers the how
of OOP, how to create a base class, defined overriable methods & use them...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top