473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

create/use array of different classes?

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
7 1746
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
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
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

"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
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
> It appears to work perfectly. Thanks a bunch for your
help.


Glad that worked out for you :)
Nov 21 '05 #7
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Piotre Ugrumov | last post by:
I have tried to write the class Student(Studente), Teacher(Docente). This classes derive from the class Person. In a class university(facoltà). I have tried to create an array of Student and an...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
37
by: Carol Depore | last post by:
How do I determine the maximum array size? For example, int a works, but a does not (run time error). Thank you.
8
by: ASP Yaboh | last post by:
I have an ArrayList of data gathered from a database. I want to create a web page from this data by creating a <table>, each cell in each row displays the appropriate data. One of those cells in...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
14
by: mm | last post by:
How can I do a array of class? s1= ## this array should hold classes ## class definition class Word: word="" ## empty words... INIT
7
by: Steven W. Orr | last post by:
I have a table of integers and each time I look up a value from the table I want to call a function using the table entry as an index into an array whose values are the different functions. I...
4
by: =?Utf-8?B?SlA=?= | last post by:
I have a C# DLL project I have created. The DLL plugs into other C#.NET applications. This DLL has several classes and methods comprised for 4 namespaces that are used by other programmer to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.