Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamic Execution of Function/Proc

Reply
Thread Tools

Dynamic Execution of Function/Proc

 
 
Kishor
Guest
Posts: n/a
 
      09-24-2003
Hi all,

I am Using VB.Net for developing my application. I am now needed help. In
this project I have to execute some function, but I cannot call them
directly using function name, I wanted to execute this function dynamically.
So I have a function list in database written as a string. I am now looking
for function or mechanism which will execute function dynamically.

I am here Giving a example.



Private Sub btnShow_Click(..)

Dim t as boolean

T = FunctionOne

End Sub



This btnShow_Click will call FunctionOne and the result will be assigned to
t but I wanted Function Like This

Private Sub btnShow_Click(..)

Dim t as boolean

T = ?????? "FunctionOne"



End Sub





Is This Possible in any language VB or C#??

Regards
--
Kishor Pise
Maharashtra, INDIA.



 
Reply With Quote
 
 
 
 
Lucas Tam
Guest
Posts: n/a
 
      09-24-2003
"Kishor" <(E-Mail Removed)> wrote in
news:#(E-Mail Removed):

> Hi all,
>
> I am Using VB.Net for developing my application. I am now needed help.
> In this project I have to execute some function, but I cannot call
> them directly using function name, I wanted to execute this function
> dynamically.


Look at CallByName... That might work.

But I would tend to think Dynamically calling a function is bad practice.




--
Lucas Tam ((E-Mail Removed))
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      09-24-2003
Hello,

"Kishor" <(E-Mail Removed)> schrieb:
> I wanted to execute this function dynamically.
> So I have a function list in database written as a string.
> I am now looking for function or mechanism which will
> execute function dynamically.
>
> I am here Giving a example.
>
>
>
> Private Sub btnShow_Click(..)
>
> Dim t as boolean
>
> T = FunctionOne
>
> End Sub
>
>
>
> This btnShow_Click will call FunctionOne and the result will
> be assigned to t but I wanted Function Like This
>
> Private Sub btnShow_Click(..)
>
> Dim t as boolean
>
> T = ?????? "FunctionOne"
>
>
>
> End Sub


Have a look at 'CallByName' and the 'System.Reflection' namespace.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet


 
Reply With Quote
 
Kumar Gaurav Khanna [.NET MVP]
Guest
Posts: n/a
 
      09-25-2003
Hi!

Reflection is the answer to your query. You should prompt the user for the
assembly contain the method implementation, dynamically load it, enumerate
the types and get to the type (read class) that implements the method in
question and then use the Invoke method to execute it and get the result
back.

Have a look at this URI:

http://www.developerfusion.com/show/1914/2/

Regards,
Kumar Gaurav Khanna
--
----------------------------------------------------------------------------
-------
I can't be garbage collected; I am pinned to .NET
----------------------------------------------------------------------------
-------
Microsoft MVP - .NET
WinToolZone - http://www.wintoolzone.com/
Spelunking Microsoft Technologies and my home on the web
OpSupport - Spelunking Rotor
http://opsupport.sscli.net/

http://dot-net.blogspot.com/
http://kgk.blogspot.com/
----------------------------------------------------------------------------
-------

"Kishor" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi all,
>
> I am Using VB.Net for developing my application. I am now needed help. In
> this project I have to execute some function, but I cannot call them
> directly using function name, I wanted to execute this function

dynamically.
> So I have a function list in database written as a string. I am now

looking
> for function or mechanism which will execute function dynamically.
>
> I am here Giving a example.
>
>
>
> Private Sub btnShow_Click(..)
>
> Dim t as boolean
>
> T = FunctionOne
>
> End Sub
>
>
>
> This btnShow_Click will call FunctionOne and the result will be assigned

to
> t but I wanted Function Like This
>
> Private Sub btnShow_Click(..)
>
> Dim t as boolean
>
> T = ?????? "FunctionOne"
>
>
>
> End Sub
>
>
>
>
>
> Is This Possible in any language VB or C#??
>
> Regards
> --
> Kishor Pise
> Maharashtra, INDIA.
>
>
>



 
Reply With Quote
 
Kishor
Guest
Posts: n/a
 
      09-25-2003
Hi Kumar Gaurav Khanna,
At some extent it is true, I have to use Reflection but with reflection
I have one problem probably you will be right person to answer this.

I am having one assembly which I am loading it dynamically. I am getting
proper Tree structure of that assembly which includes list of properties and
methods. Now My problem is
1 I want to store the selected property/method in to database
2 and after receiving it from database I want to execute this

So How do I achieve this?. Is there any mechanism where can I get the
information about the same. If you dont understand question Please mail me I
wiil explain it more clearly to you.

Regards
Kishor




"Kumar Gaurav Khanna [.NET MVP]" <(E-Mail Removed)> wrote in message
news:euG6$(E-Mail Removed)...
> Hi!
>
> Reflection is the answer to your query. You should prompt the user for the
> assembly contain the method implementation, dynamically load it, enumerate
> the types and get to the type (read class) that implements the method in
> question and then use the Invoke method to execute it and get the result
> back.
>
> Have a look at this URI:
>
> http://www.developerfusion.com/show/1914/2/
>
> Regards,
> Kumar Gaurav Khanna
> --
> --------------------------------------------------------------------------

--
> -------
> I can't be garbage collected; I am pinned to .NET
> --------------------------------------------------------------------------

--
> -------
> Microsoft MVP - .NET
> WinToolZone - http://www.wintoolzone.com/
> Spelunking Microsoft Technologies and my home on the web
> OpSupport - Spelunking Rotor
> http://opsupport.sscli.net/
>
> http://dot-net.blogspot.com/
> http://kgk.blogspot.com/
> --------------------------------------------------------------------------

--
> -------
>
> "Kishor" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hi all,
> >
> > I am Using VB.Net for developing my application. I am now needed help.

In
> > this project I have to execute some function, but I cannot call them
> > directly using function name, I wanted to execute this function

> dynamically.
> > So I have a function list in database written as a string. I am now

> looking
> > for function or mechanism which will execute function dynamically.
> >
> > I am here Giving a example.
> >
> >
> >
> > Private Sub btnShow_Click(..)
> >
> > Dim t as boolean
> >
> > T = FunctionOne
> >
> > End Sub
> >
> >
> >
> > This btnShow_Click will call FunctionOne and the result will be

assigned
> to
> > t but I wanted Function Like This
> >
> > Private Sub btnShow_Click(..)
> >
> > Dim t as boolean
> >
> > T = ?????? "FunctionOne"
> >
> >
> >
> > End Sub
> >
> >
> >
> >
> >
> > Is This Possible in any language VB or C#??
> >
> > Regards
> > --
> > Kishor Pise
> > Maharashtra, INDIA.
> >
> >
> >

>
>



 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      09-25-2003
Hi Kishor,

What's the problem with CallByName() ? What does this fail to do for you?

Regards,
Fergus


 
Reply With Quote
 
Kishor
Guest
Posts: n/a
 
      09-26-2003
Hi Fergus Cooney,
In my project I am having following code, which works fine project is having
reference to the Excel thr' Interop.

OExl.Visible = True
OExl.Workbooks.Open("C:\Documents and
Settings\Administrator\Desktop\ConsultantListLocat ionwise1.xls", , True)
esh = OExl.ActiveSheet
MsgBox(esh.Cells.Range("F18").Value)

This displays me text which is there in cell F18 assume this is "Kishor"
up to this I dont have any problem with this but when I am adding code it is
supposed to give me a message box with name "Kishor".
Private Sub Button1_Click(...) Handles Button1.Click
MsgBox(CallByName(esh, "Cell", CallType.Get, "F18"))
End Sub

But I got the Error.

Public member 'Cell' on type 'Worksheet' not found.

Hope u got this
Bye
Kishor







"" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Kishor,
>
> What's the problem with CallByName() ? What does this fail to do for

you?
>
> Regards,
> Fergus
>
>



 
Reply With Quote
 
Kumar Gaurav Khanna [.NET MVP]
Guest
Posts: n/a
 
      09-26-2003
Hi!

I dont see how answer to this is going to any different from the one I told
you before. Once you have persisted the method/type names in the database
and later read from the same, you got to loop thru the type list to get to
the right type, following by locating the correct method (the one which you
persisted in the database), and then using reflection, instantiate the type,
followed by invocation of the method using the Invoke call.

What issues are you facing?

--------------------------------------------------
Kumar Gaurav Khanna
Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
OpSupport - Spelunking Rotor
http://opsupport.sscli.net/

"Kishor" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi Kumar Gaurav Khanna,
> At some extent it is true, I have to use Reflection but with

reflection
> I have one problem probably you will be right person to answer this.
>
> I am having one assembly which I am loading it dynamically. I am getting
> proper Tree structure of that assembly which includes list of properties

and
> methods. Now My problem is
> 1 I want to store the selected property/method in to database
> 2 and after receiving it from database I want to execute this
>
> So How do I achieve this?. Is there any mechanism where can I get the
> information about the same. If you dont understand question Please mail me

I
> wiil explain it more clearly to you.
>
> Regards
> Kishor
>
>
>
>
> "Kumar Gaurav Khanna [.NET MVP]" <(E-Mail Removed)> wrote in message
> news:euG6$(E-Mail Removed)...
> > Hi!
> >
> > Reflection is the answer to your query. You should prompt the user for

the
> > assembly contain the method implementation, dynamically load it,

enumerate
> > the types and get to the type (read class) that implements the method in
> > question and then use the Invoke method to execute it and get the result
> > back.
> >
> > Have a look at this URI:
> >
> > http://www.developerfusion.com/show/1914/2/
> >
> > Regards,
> > Kumar Gaurav Khanna
> > --

>
> --------------------------------------------------------------------------
> --
> > -------
> > I can't be garbage collected; I am pinned to .NET

>
> --------------------------------------------------------------------------
> --
> > -------
> > Microsoft MVP - .NET
> > WinToolZone - http://www.wintoolzone.com/
> > Spelunking Microsoft Technologies and my home on the web
> > OpSupport - Spelunking Rotor
> > http://opsupport.sscli.net/
> >
> > http://dot-net.blogspot.com/
> > http://kgk.blogspot.com/

>
> --------------------------------------------------------------------------
> --
> > -------
> >
> > "Kishor" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > Hi all,
> > >
> > > I am Using VB.Net for developing my application. I am now needed help.

> In
> > > this project I have to execute some function, but I cannot call them
> > > directly using function name, I wanted to execute this function

> > dynamically.
> > > So I have a function list in database written as a string. I am now

> > looking
> > > for function or mechanism which will execute function dynamically.
> > >
> > > I am here Giving a example.
> > >
> > >
> > >
> > > Private Sub btnShow_Click(..)
> > >
> > > Dim t as boolean
> > >
> > > T = FunctionOne
> > >
> > > End Sub
> > >
> > >
> > >
> > > This btnShow_Click will call FunctionOne and the result will be

> assigned
> > to
> > > t but I wanted Function Like This
> > >
> > > Private Sub btnShow_Click(..)
> > >
> > > Dim t as boolean
> > >
> > > T = ?????? "FunctionOne"
> > >
> > >
> > >
> > > End Sub
> > >
> > >
> > >
> > >
> > >
> > > Is This Possible in any language VB or C#??
> > >
> > > Regards
> > > --
> > > Kishor Pise
> > > Maharashtra, INDIA.
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      09-26-2003
Hi Kishor,

This is the call that works. (Names changed)
oWorkSheet = oExcel.ActiveSheet
MsgBox (oWorkSheet.Cells.Range("F18").Value)

This is the call that fails:
MsgBox (CallByName (oWorkSheet, "Cell", CallType.Get, "F18"))

The reason is very simple. There is no method of oWorkSheet called 'Cell'.
If you look at the call that works, you'll see that it calls 'Cells' - plural.

That clears up <that> problem but it does nothing to solve your original
question - how can you call an Excel function that has been defined as a
string. It's fine if it's just a single function like 'Cells' because
CallByName will work perfectly with <that>. But you can't call
'Cells.Range("F18").Value' as this is <series> of calls.

Given
Dim O As Object
then
O = oWorkSheet.Cells.Range("F18").Value
is the equivalent of
O = oWorkSheet.Cells
O = O.Range("F18")
O = O.Value
and CallByName cannot do that for you.

What is needed is a way to call each of these in turn, providing the
arguments as appropriate. One way is for you to store your function string as
an array of separate functions and execute these in turn - using the result of
one as the object of the next. For example, 'Cells', 'Range("F18")', 'Value'.

The other is to have a function which wll accept the whole string,
'Cells.Range("F18").Value', and break it down into the required steps,
returning the final result.

Well, it just so happens that I've written it for you and it will accept
your string straight from the database.

You can call it like so:
O = CallByNameSeries (oWorkSheet, "Cells.Range(""A1"").Value")

It has limitations though. The only arguments that it allows are a single
string per function - Range("F18:G25") is fine, but Range("F18", "G25") is
not. And all functions must <be> functions - not methods.

Give it a go, anyhow, and see if it's of any use to you..

Regards,
Fergus

ps. For anyone else reading this - it will work for any object - not just
Excel objects, and is therefore a (simple) extension to CallByName.

<code>
'An extension of CallByName which accepts a series of
'function calls each with a single optional string argument.
'Functions can be actual Functions or Properties.
'
'Spaces between parts are removed.
'Spaces within an argument are retained.
'
'Examples:
'O = CallByNameSeries (oWorkSheet, "Cells.Range (""A1"").Value")
'O = CallByNameSeries (oFoo, "Parent.Name")
'O = CallByNameSeries (oBar, "Item (""The Elephant"").Size")
'
'Note that "Item (1)" is the same as "Item (""1"")" - ie. a string arg.
'
Public Function CallByNameSeries (oStartObj As Object, _
sFuncString As String) As Object

'Let's start at the very beginning.
Dim oObj As Object = oStartObj

Dim PosOfDot As Integer
Do
'Get the next function name.
Dim sFunc As String = sFuncString
Dim sArg As String = Nothing

'Extract the first function if it's a series.
PosOfDot = sFunc.IndexOf (".")
If PosOfDot >= 0 Then
sFunc = sFuncString.Substring (0, PosOfDot)
sFuncString = sFuncString.Substring (PosOfDot + 1)
End If

'Get any argument as indicated by an opening bracket.
Dim PosOfArg As Integer = sFunc.IndexOf ("(")
If PosOfArg >= 0 Then
'Get the arg and remove the brackets.
sArg = sFunc.Substring (PosOfArg + 1)
sArg = sArg.Replace (")", "")

'Remove any quotes from the argument.
sArg = sArg.Replace("""", "")

'Remove the argument from the function name.
sFunc = sFunc.Substring (0, PosOfArg)
End If

'Call the function.
If sArg Is Nothing Then
oObj = CallByName (oObj, sFunc.Trim, CallType.Get, Nothing)
Else
oObj = CallByName (oObj, sFunc.Trim, CallType.Get, sArg)
End If

'Repeat for each function part.
Loop Until PosOfDot < 0

'Send back the final result.
Return oObj
End Function
</code>


 
Reply With Quote
 
Kishor
Guest
Posts: n/a
 
      09-27-2003
Dear Kumar,
I got what you are saying, But at my end It is not working or I am not
able to achieve it. Now I am describing question in simple language. In my
Dot net Application, I have Declared one object as below.
Public OWord As New Word.Application()
In my one proc I am having following code. which will open a DOC file. and
make a word application visible.

OWord.Documents.Open("C:\Documents and
Settings\Administrator\Desktop\Address List.doc")

OWord.Visible = True

On the above statement if I places break point. and opens Quick watch
window, in window I will be able to see the full structure of oword object
in treeview. similar Interface I want to build where in I wanted to Hard
code object.
my problem here is 1 I am not getting the all properties and all the metods
in tree format. even if I know population of Tree.
2 whatever I will select I wanted to strore it in to database.
3 and after this I wanted execute the statement what user has selected
previously. (here your answer is suitable.)

So Please Help me.
Kishor






"Kumar Gaurav Khanna [.NET MVP]" <(E-Mail Removed)> wrote in message
news:Oh87#(E-Mail Removed)...
> Hi!
>
> I dont see how answer to this is going to any different from the one I

told
> you before. Once you have persisted the method/type names in the database
> and later read from the same, you got to loop thru the type list to get to
> the right type, following by locating the correct method (the one which

you
> persisted in the database), and then using reflection, instantiate the

type,
> followed by invocation of the method using the Invoke call.
>
> What issues are you facing?
>
> --------------------------------------------------
> Kumar Gaurav Khanna
> Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
> WinToolZone - Spelunking Microsoft Technologies
> http://www.wintoolzone.com/
> OpSupport - Spelunking Rotor
> http://opsupport.sscli.net/
>
> "Kishor" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hi Kumar Gaurav Khanna,
> > At some extent it is true, I have to use Reflection but with

> reflection
> > I have one problem probably you will be right person to answer this.
> >
> > I am having one assembly which I am loading it dynamically. I am getting
> > proper Tree structure of that assembly which includes list of properties

> and
> > methods. Now My problem is
> > 1 I want to store the selected property/method in to database
> > 2 and after receiving it from database I want to execute this
> >
> > So How do I achieve this?. Is there any mechanism where can I get the
> > information about the same. If you dont understand question Please mail

me
> I
> > wiil explain it more clearly to you.
> >
> > Regards
> > Kishor
> >
> >
> >
> >
> > "Kumar Gaurav Khanna [.NET MVP]" <(E-Mail Removed)> wrote in

message
> > news:euG6$(E-Mail Removed)...
> > > Hi!
> > >
> > > Reflection is the answer to your query. You should prompt the user for

> the
> > > assembly contain the method implementation, dynamically load it,

> enumerate
> > > the types and get to the type (read class) that implements the method

in
> > > question and then use the Invoke method to execute it and get the

result
> > > back.
> > >
> > > Have a look at this URI:
> > >
> > > http://www.developerfusion.com/show/1914/2/
> > >
> > > Regards,
> > > Kumar Gaurav Khanna
> > > --

> >

>
> --------------------------------------------------------------------------
> > --
> > > -------
> > > I can't be garbage collected; I am pinned to .NET

> >

>
> --------------------------------------------------------------------------
> > --
> > > -------
> > > Microsoft MVP - .NET
> > > WinToolZone - http://www.wintoolzone.com/
> > > Spelunking Microsoft Technologies and my home on the web
> > > OpSupport - Spelunking Rotor
> > > http://opsupport.sscli.net/
> > >
> > > http://dot-net.blogspot.com/
> > > http://kgk.blogspot.com/

> >

>
> --------------------------------------------------------------------------
> > --
> > > -------
> > >
> > > "Kishor" <(E-Mail Removed)> wrote in message
> > > news:%(E-Mail Removed)...
> > > > Hi all,
> > > >
> > > > I am Using VB.Net for developing my application. I am now needed

help.
> > In
> > > > this project I have to execute some function, but I cannot call them
> > > > directly using function name, I wanted to execute this function
> > > dynamically.
> > > > So I have a function list in database written as a string. I am now
> > > looking
> > > > for function or mechanism which will execute function dynamically.
> > > >
> > > > I am here Giving a example.
> > > >
> > > >
> > > >
> > > > Private Sub btnShow_Click(..)
> > > >
> > > > Dim t as boolean
> > > >
> > > > T = FunctionOne
> > > >
> > > > End Sub
> > > >
> > > >
> > > >
> > > > This btnShow_Click will call FunctionOne and the result will be

> > assigned
> > > to
> > > > t but I wanted Function Like This
> > > >
> > > > Private Sub btnShow_Click(..)
> > > >
> > > > Dim t as boolean
> > > >
> > > > T = ?????? "FunctionOne"
> > > >
> > > >
> > > >
> > > > End Sub
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > Is This Possible in any language VB or C#??
> > > >
> > > > Regards
> > > > --
> > > > Kishor Pise
> > > > Maharashtra, INDIA.
> > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Safe Dynamic Library Execution and unload Ggianks C++ 1 08-19-2010 07:45 AM
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
Dynamic function execution Andy Wu Python 5 11-26-2006 11:37 AM
Dynamic tracing of C code execution Nikhil C Programming 5 05-16-2006 06:21 AM
"Open-end" Java-session / dynamic compilation and execution Jesper Sahner Java 1 02-01-2005 04:52 AM



Advertisments