473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic Execution of Function/Proc

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.

Nov 20 '05 #1
9 3714
"Kishor" <kp***@hotmail.com> wrote in
news:#0*************@TK2MSFTNGP10.phx.gbl:
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 (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #2
Hello,

"Kishor" <kp***@hotmail.com> 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
Nov 20 '05 #3
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" <kp***@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #4
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]" <ga****@wintoolzone.com> wrote in message
news:eu**************@TK2MSFTNGP09.phx.gbl...
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" <kp***@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.


Nov 20 '05 #5
Hi Kishor,

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

Regards,
Fergus
Nov 20 '05 #6
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



"" <fi******@tesco.net> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
Hi Kishor,

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

Nov 20 '05 #7
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" <kp***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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]" <ga****@wintoolzone.com> wrote in message
news:eu**************@TK2MSFTNGP09.phx.gbl...
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" <kp***@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.



Nov 20 '05 #8
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>
Nov 20 '05 #9
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]" <ga****@wintoolzone.com> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
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" <kp***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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]" <ga****@wintoolzone.com> wrote in message news:eu**************@TK2MSFTNGP09.phx.gbl...
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" <kp***@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
> 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.
>
>
>



Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robin Hammond | last post by:
Can anybody tell me why a) when running a stored proc from an asp page to return a recordset the command succeeds if the sp queries an existing table directly, but b) if the stored proc populates...
0
by: CSDunn | last post by:
Hello, I have a format issue on an Access 2000 ADP report that I am going to attempt to explain from a 'ten thousand foot view' : I have an Access 2000 ADP report that has a SQL Server 2000...
6
by: MattC | last post by:
Hi, I'm implementing a new Business Layer in one of our applications. I'm toying with the idea of placing all the Create, Read, Update and Delete SQL in the object in question and build a...
1
by: pbb | last post by:
I'm creating a set of dynamic controls on a webpage by calling my BuildControls sub in the Page_Init sub. I recreate the controls by calling the BuildControls sub in the LoadViewState override...
1
by: brianbasquille | last post by:
Hello all, Hope you can help me with this little conundrum! I'm running a batch file and want to print the execution of it line-by-line. Best reference i could find was:...
6
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
3
by: vijay.db | last post by:
Hi Group, Running DB2 V8.2 Fxpack 9 in AIX 5.2, I get the following error frequently and my instance is stopped...collected some info like: the signal received is 11 which is SEGMENTATION...
4
by: TheRealPawn | last post by:
I'm trying to get the execution plan for a single stored procedure from Profiler. Now, I've isolated the procedure but I get all execution plans. Any ideas on how to connect the SPIDs so that I...
7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.