How to Call Function of vb.net whose name stored in Database?
-
Guru_yogi wrote:
I have created database driven menu in which against each menu item some function name is stored. I want to call function each menu item in vb.net as this function is defined in VB.net.
You might want to have a look in the Reflection namespace. Specifically the MethodInfo class.
Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Mixins in C#3.0 My website | Blog
-
You can create a database table storing something like: menuname functionname open openfunction new newfunction We can easily retrieved the function name from the database for the menu clicked. After the function name is received by us, we have to use Reflection. I am giving a code sample like this: Sopposing I have a class where the functions are available Public Class Class1 Public Sub openfunction() MsgBox("This is open function") End Sub End Class Then I have a button on the form, I want to call the function using reflection. You have to import System.Reflection namespace the code goes like this: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim t As Type = GetType(Class1) Dim o As Object = Activator.CreateInstance(t) Dim mi As MethodInfo = t.GetMethod("openfunction") ' U can write the variable which store the methor name in place of openfunction mi.Invoke(o, Nothing) End Sub The code will slightly change when we will be using any reference assembly or resolving the assemblies at runtime. Hope the solution is OK for you.
-
First create a function like fun1() use this funtion [private\public\sub] fun1 ............ ....... ..... end sub
-
First create a function like fun1() use this funtion [private\public\sub] fun1 ............ ....... ..... end sub
Did you even bother to read the original post?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007