Dynamically calling classes and properties

W

wisaunders

I have several different classes that contain the same four
properties. I tell my application property of which class to call in
the configuration file.

Here are the values from my config file

<class>AAAA</class>
<property>1111</property>


This is how I current have it working.

Dim sql As String = ""
Select Case classVariable
Case "AAAA"
Dim extract As New AAAA
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
Case "BBBB"
Dim extract As New BBBB
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
Case "CCCC"
Dim extract As New CCCC
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
End Select

Is there a was to call these classes and properties dynamically with
variable from my config file.
Something that looks like this? I don't want to have to add another
case and select case for every new class.

Dim extract as new [classVariable]
Sql = extract.[propertyVariable]

Thanks in advance,
Will
 
V

VJ

you need to use Reflection... so you can use a string to convert it to
object i.e class

VJ
 
L

Lloyd Sheen

I have several different classes that contain the same four
properties. I tell my application property of which class to call in
the configuration file.

Here are the values from my config file

<class>AAAA</class>
<property>1111</property>


This is how I current have it working.

Dim sql As String = ""
Select Case classVariable
Case "AAAA"
Dim extract As New AAAA
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
Case "BBBB"
Dim extract As New BBBB
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
Case "CCCC"
Dim extract As New CCCC
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
End Select

Is there a was to call these classes and properties dynamically with
variable from my config file.
Something that looks like this? I don't want to have to add another
case and select case for every new class.

Dim extract as new [classVariable]
Sql = extract.[propertyVariable]

Thanks in advance,
Will

Can I make a suggestion.

Since your classes have the same 4 methods you can either create a base
class with those methods and then create your objects inheriting from the
base class. Then you would create base class variable which would hold the
object and the methods called would the same. It would get rid of alot of
your code and make the classes easier to understand.

An alternative is to create an interface with those 4 methods and have each
class "Implement" the interface. Again you can create a variable (this time
of type interface) and do your method according to the select statement.

I sure hope that is not production code since it will be unmaintainable in
its present form.

Lloyd Sheen
 
H

Herfried K. Wagner [MVP]

I have several different classes that contain the same four
properties. I tell my application property of which class to call in
the configuration file.

Here are the values from my config file

<class>AAAA</class>
<property>1111</property>

Take a look at 'Activator.CreateInstance' and the members of the
'System.Type' class. The latter can be used to call a method on the object
created by 'CreateInstance'.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

What is it that you are using this for?

I suspect that, as in most cases when someone asks for dynamic
variables, it can be solved in a different way.

I have several different classes that contain the same four
properties. I tell my application property of which class to call in
the configuration file.

Here are the values from my config file

<class>AAAA</class>
<property>1111</property>


This is how I current have it working.

Dim sql As String = ""
Select Case classVariable
Case "AAAA"
Dim extract As New AAAA
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
Case "BBBB"
Dim extract As New BBBB
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
Case "CCCC"
Dim extract As New CCCC
Select Case propertyVariable
Case "1111"
sql = extract.1111
Case "2222"
sql = extract.2222
Case "3333"
sql = extract.3333
Case "4444"
sql = extract.4444
Case Else
'quit function
End Select
End Select

Is there a was to call these classes and properties dynamically with
variable from my config file.
Something that looks like this? I don't want to have to add another
case and select case for every new class.

Dim extract as new [classVariable]
Sql = extract.[propertyVariable]

Thanks in advance,
Will
 

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