Results 1 to 6 of 6

Thread: call functions dynamic

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52

    call functions dynamic

    hi!

    is there a way in vb.net to call functions and procedures dynamically at runtime.

    background of the question is that i want to fire events at runtime which call a pool of functions. those functions should be defined in a databasetable to keep the application open for further extensions and modifications.

    i've experimented a little bit with the codeDOM of .NET but i didn't found what i'm searching for. has anybody an idea how to call functions dynamically at runtime?

    best regards
    robert

  2. #2
    New Member
    Join Date
    Aug 2002
    Posts
    10
    I have written a probram that calls an dll in this manner. In my case it calls a function with the same name in each of them, what you are wanting to do seems to be an extension of that.

    Check into reflection, you might start with this article , I think it will help you accomplish this.

    HTH
    Brad

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's what addhandler and withevents created for .

    Here's an example , that uses the power of addhandler method : Addhandler can fires one or multiple events or methods at the same time . This is also called delegates. This creates new timer control and you can manage its events at runtime.


    VB Code:
    1. Dim WithEvents TimerEvent As Timer
    2.     Dim timr As New Timer()
    3.    
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         timr.Interval = 2000
    6.         timr.Enabled = True
    7.     End Sub
    8.  
    9.     Private Sub FireTimer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEvent.Tick
    10.         MsgBox("Hey there")
    11.     End Sub
    12.  
    13.     Private Sub StopTimer()
    14.         timr.Enabled = False
    15.     End Sub
    16.  
    17.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    18.         AddHandler timr.Tick, AddressOf FireTimer
    19.     End Sub
    20.  
    21.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    22.         timr.Enabled = False
    23.     End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52
    the problem with addhandler is that the number of functions/procedures and their names must be known at designtime...

    with the dynamic functions and procedures it should also be possible to give the user exits from the standard-software to implement his own code. For ex: the software fires events at points where the user may want to do something other than the standardsoftware do... so he can implement a DLL with a func/proc and adds this info (event + functionname) in the db-table.

    but maybe it's better to realize all objects in DLLs and exchange them when necessary. how do you organize your software-projects in order to keep them open?

  5. #5
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    reflection is the way to go as far as .NET goes, but an old VB6 throwback could be used for a quick and dirty fix - look into calling procs by using CallByName It accepts the proc name as a string. Bit of a performance hit though!

    Code:
    Public Sub Test(ByVal str As String)
            MessageBox.Show(str)
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
            CallByName(Me, "test", CallType.Method, "HELLO")
    End Sub
    Last edited by powdir; Jun 12th, 2003 at 07:28 PM.

  6. #6
    New Member
    Join Date
    Aug 2002
    Posts
    10
    Originally posted by escape0
    how do you organize your software-projects in order to keep them open?
    I'm not sure I understand the question. Are you wanting to know how I keep the dlls available at runtime? I don't. I call them once and then dispose of them. I would guess you will need a collection of objects at the class level, but I haven't tried that myself.

    If I haven't answered your question let me know and I'll try again.

    Brad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width