By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

create/use array of different classes?

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

-----Original Message-----
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 classarray 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 amkind 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
.

Nov 21 '05 #2

P: n/a
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.

"Ron" <an*******@discussions.microsoft.com> wrote in message
news:1d****************************@phx.gbl...
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.

-----Original Message-----
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
.

Nov 21 '05 #3

P: n/a
Ron
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

-----Original Message-----
Can you change your classes? It just seems like a bad design. You shouldhave an interface with properties and methods that are common to your DTSclasses and then have all your classes implement that interface. Somethinglike 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.

"Ron" <an*******@discussions.microsoft.com> wrote in messagenews:1d****************************@phx.gbl...
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.

>-----Original Message-----
>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
>.
>

.

Nov 21 '05 #4

P: n/a

"Ron" <an*******@discussions.microsoft.com> wrote in message
news:1d****************************@phx.gbl...
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.
Nov 21 '05 #5

P: n/a
Ron
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.

-----Original Message-----
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

-----Original Message-----
Can you change your classes? It just seems like a baddesign. 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.

"Ron" <an*******@discussions.microsoft.com> wrote in

message
news:1d****************************@phx.gbl...
Well, I came up with one solution that isn't realdynamic, 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.
>-----Original Message-----
>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
>.
>

.

.

Nov 21 '05 #6

P: n/a
> It appears to work perfectly. Thanks a bunch for your
help.


Glad that worked out for you :)
Nov 21 '05 #7

P: n/a
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
"Ron" <an*******@discussions.microsoft.com> wrote in message
news:0f****************************@phx.gbl...
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

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.