Passing various class types as parameters

J

John

Hi

I have the following code;

Public DA(3) As Object

DA(1) = New Class1
DA(2) = New Class2

Is there a way to pass the class types as parameter to a sub as below;

SetDA(Class1,1)
SetDA(Class2,2)

where the sub declares variables for the passed class types, as below?

Sub SetDA(ByRef ClassType As Object, ByVal Subs As Long)
DA(Subs) = New ClassType
End Sub

Thanks

Regards
 
A

Armin Zingler

John said:
Hi

I have the following code;

Public DA(3) As Object

DA(1) = New Class1
DA(2) = New Class2

Is there a way to pass the class types as parameter to a sub as
below;

SetDA(Class1,1)
SetDA(Class2,2)

where the sub declares variables for the passed class types, as
below?

Sub SetDA(ByRef ClassType As Object, ByVal Subs As Long)
DA(Subs) = New ClassType
End Sub


Have a look at System.Activator.CreateInstance. But what are you trying
to do? Where does the type information come from at runtime? Often there
are better solutions.


Armin
 
J

John

Hi Armin

Thanks. Class1 & 2 are data adapters for different tables. I need an array
of data adapters so I can write some generic code once the array has been
created.

Thanks again.

Regards
 
A

Armin Zingler

John said:
Hi Armin

Thanks. Class1 & 2 are data adapters for different tables. I need an
array of data adapters so I can write some generic code once the
array has been created.

Thanks again.

You can declare the items As DataAdapter. Why don't you know the type of
the object to be created?


Armin
 
J

John

Dim x as DataAdapter
x = MyTableDataAdapter

dose not work as MyTableDataAdapter can not be converted to DataAdapter
 
A

Armin Zingler

John said:
Dim x as DataAdapter
x = MyTableDataAdapter

dose not work as MyTableDataAdapter can not be converted to
DataAdapter

You wrote about "data adapters", not table adapters. Ok, Table adapters
are derived from Component but this doesn't help much. Once more the
question why you don't know the object type in advance, and where does
the data type come from at run time? The table adapters do not have
much in common because the (most) members are specific to the table.
What do you want to do with the TableAdatpers in the array?


Armin
 

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

Top