create object dynamically

R

ronnel.santiago

hi guys,

i just have a doubt about this one... i have several class that just
basically do CRUD operations (create, read, update, delete) now for example
on my delete function, i want to create objects dynamically based on
TableName..

this is my current line of code
Public Sub ActionButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Select Case TableName.ToLower
Case "employee"
Dim o As New BusinessLogic.EmployeeController
o.Delete(1)
Case "gender"
Dim o As New BusinessLogic.GenderController
o.Delete(1)
Case "more of this on my code"
Dim o As New BusinessLogic.[other]Controller
o.Delete(1)
End Select
End Sub

now i want to make this line of code shorter, maybe by doing this

Public Sub ActionButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim o As New BusinessLogic.[TableName]Controller
o.Delete(1)
End Select
End Sub

note the [TableName] here is a property i created, so passing a EMPLOYEE as
the TableName will
Dim o As New BusinessLogic.EmployeeController and then i can call the delete
function, just want to load object dynamically here.. i hope i made my point
here clear...

is it anyway possible?

TIA!


(if i have 8 hours to chop down a tree, i'd spend 6 sharpening my ax)
 
J

Jon Skeet [C# MVP]

note the [TableName] here is a property i created, so passing a EMPLOYEE as
the TableName will
Dim o As New BusinessLogic.EmployeeController and then i can call the delete
function, just want to load object dynamically here.. i hope i made my point
here clear...

Not sure in what way this is really Windows Forms related, but anyway -
have a look at Activator.CreateInstance and Type.GetType. You probably
want to have a map set up between table name and Type, and then call
Activator.CreateInstance on the relevant Type.
 
H

Herfried K. Wagner [MVP]

ronnel.santiago said:
i just have a doubt about this one... i have several class that just
basically do CRUD operations (create, read, update, delete) now for
example
on my delete function, i want to create objects dynamically based on
TableName..

this is my current line of code
Public Sub ActionButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Select Case TableName.ToLower
Case "employee"
Dim o As New BusinessLogic.EmployeeController
o.Delete(1)
Case "gender"
Dim o As New BusinessLogic.GenderController
o.Delete(1)
Case "more of this on my code"
Dim o As New BusinessLogic.[other]Controller
o.Delete(1)
End Select
End Sub

now i want to make this line of code shorter, maybe by doing this

Public Sub ActionButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim o As New BusinessLogic.[TableName]Controller
o.Delete(1)
End Select
End Sub

note the [TableName] here is a property i created, so passing a EMPLOYEE
as
the TableName will
Dim o As New BusinessLogic.EmployeeController and then i can call the
delete
function, just want to load object dynamically here.. i hope i made my
point
here clear...

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///
 

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