Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to run an expression in string format

AsAdiAsAdi Member Posts: 117
Dear Experts

I need to provide an environment that users can build a formula usuin field names of a database
table and some string functions like "substr","left", and "right".The environment saves formula
in a string variable,and when user clicks a button,my code must run that formula over values of
those fields for selected record of that table.
Now,I have that environment and it returns formula as a sring to me,but I don't still have any
idea how to run this string.Do I have to write some code to compile it?I found that .Net has
some namespaces for building dynamic methods,I mean Document object model,but if I want to use
classes of this namesapce I have to convert that formula to IL,which is more difficult for me.
Is there any other idea how I can run a user defined expression inside my code and show up its
result to user?That expression can be like this,if we suppose all table field names start with
"fld":
"fldDKCode+fldDKSerial+Substr(fldDKDate,3,2)+fldDKId+Left(fldDKUserName)"
and I have a database table named TBDKUsers,includes all fields mentioned above.When user clicks
on Run button,my program must show the result of this expression for selected record of TBDKUsers.

Thank you very much

Comments

  • tundeemmatundeemma Member Posts: 6
    well it's like i have an idea but i will need more information about what result are you expecting, are you passing your fomula as a query to the table for selection or computation?
    Please provide more details about what you want as result.

    Tunde
  • AsAdiAsAdi Member Posts: 117
    Thank you very much.
    I need the result of that expression for selected record,suppose current record has these values in it fields:
    fldDKCode="654-02-555-1254-87"
    fldDKId="AB123585444"
    fldDKName="Gulf"
    and the expression is something like this:
    myExpression="fldDKName+Substr(fldDKCode,7,3)+Left(fldDKId,2)"

    so I need an idea to implement it so that I can return this value

    myResult="Gulf555AB"
    (I supposed that Substr second argument is zero based index).
  • tundeemmatundeemma Member Posts: 6
    Hi, Sorry i don't think i got what you are looking for. Mayble this may give you an idea
    A. if you are getting your result from the database direct i don't think is should be a problem, you can run a simple
    "SELECT fldDKName+Substr(fldDKCode,7,3)+Left(fldDKId,2) FROM TABLENAME" will give your result as requested just pass that to the database.
    B. Another method i can guess is since you already have string myExpression you can do something like this:
    1. define an arrary of string (dim strAry() as string)
    2. Count the number of elements in your string
    (strAry=split(myExpression,"+")
    This will break down the variables and give strAry(2) like your example Note: it is an zero arrary.
    3. Result will then be
    Dim j as int16=0
    Dim myResult as string.Empty
    while j<=strAry.length
    myResult=strAry(j)+ myResult
    j +=1
    Loop
    As i said i am sorry i may not be able to provide good answer but this help you, send me a mail at tunde_emmanuel@yahoo.com
  • AsAdiAsAdi Member Posts: 117
    Thank you very much,it seems the first idea is more feasible,if I put the string among a SELECT expression,I can catch error if there is any,and take the result back for a specific record,ofcourse provided I have all SQL standard functions in myExpression variable.

    Reza
  • tundeemmatundeemma Member Posts: 6
    hello, well it is good trying your selection but i just remember two good disadvantages of that option
    1. It means you will have to be going back to the database each time your expression is formed to execute.
    2. In case you have a funtion that is not supported by your TSql of the database engine you are using or your end user will be using then most of the time you will have error.

    This second disadvantage is very important for you to considered. You have more functions in VB that will give your end-user more flexibility to work with. All the same have a good programming.

    Bye
Sign In or Register to comment.