bjan
bjan

Reputation: 2030

Run string as Code

How to call an existing VB6 function and pass parameters or execute a statement utilizing some defined objects dynamically? E.g.

Private Const KONST = 123.45
Private Function First()
    Dim var1 As String
    Dim var2 As Date
    Dim var3 As Integer
    ...
    var3 = Second(var1)                 'LINE 1
    ...
    var2 = var2 + IIf(var3 > KONST, 1, -1) 'LINE 2
    ...
    var2 = var2 * KONST                 'LINE 3
    ...
End Function

Private Function Second(ByVal str As String) As Integer
    Second = CInt(str)
End Function

At line 1: Name of the function Second could be dynamic while using var1 and returning value

At line 2: The whole IIf should be dynamic using var3 and KONST

At line 3: Whole var2 * KONST should be dynamic i.e. here i may write var2 + KONST or var3 / KONST or var3 + 222 or 1 + 2 or myCollection.Item("item_Key").

All such dynamic configuration will be in a config file.

Edit

I am trying to make grid layout and data population dynamic. By grid layout i mean number of columns, their title, order, format, etc. And by population i mean loading data into the grid, in doing so, sometimes we resolve a database value with some of our Enums, we apply some logic on data before showing it, value of one column is based on value of other column, etc. Though, upto some extent, this can be achieved via databases views, but to have all such logic in a central location we do such things from source code. Therefore i need some way to call my vb6 code dynamically and define the calling (function name, parameters, enums, types, statement) in a config file.

Upvotes: 0

Views: 1188

Answers (1)

tcarvin
tcarvin

Reputation: 10855

Well, you could use CallByName (see http://support.microsoft.com/kb/186143 for one of many easily found examples) to invoke methods and properties on an object dynamically.

But I think you want entire composed statements to be executed dynamically. For that you can use the Script control (as in VBScript). See http://support.microsoft.com/kb/184740 for a sample. In particular, it has a Eval function that runs arbitrary statements.

Upvotes: 2

Related Questions