473,386 Members | 1,668 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,386 software developers and data experts.

run a sub/function by using a string containing its name

Hi,

I'd like to know how to call a sub/function bu using a
string containing its name, as an example as follow:

Sub Main()
Dim name As String = "TestMe()"
' need help here

End Sub

Public Sub TestMe()
MsgBox("Hello")
End Sub

Thanks in advance
Nov 20 '05 #1
7 3614
Jan
Hi Li Pang,

Have a look at "Delegates".... a new technology for VB.NET. That should
solve your problem.

Regards,
Jan

"Li Pang" <li*****@eu.altria.com> wrote in message
news:04****************************@phx.gbl...
Hi,

I'd like to know how to call a sub/function bu using a
string containing its name, as an example as follow:

Sub Main()
Dim name As String = "TestMe()"
' need help here

End Sub

Public Sub TestMe()
MsgBox("Hello")
End Sub

Thanks in advance

Nov 20 '05 #2
"Li Pang" <li*****@eu.altria.com> schrieb
Hi,

I'd like to know how to call a sub/function bu using a
string containing its name, as an example as follow:

Sub Main()
Dim name As String = "TestMe()"
' need help here

End Sub

Public Sub TestMe()
MsgBox("Hello")
End Sub

Thanks in advance


What happens if the procedure name does not exist? The application will
crash because the compiler can't find this error.

If you really need it, you can use CallByName (shared member of
Microsoft.VisualBasic.Interaction).
--
Armin

Nov 20 '05 #3
Thanks Jan,

I have already used the Delagate. I have two classes,
class A calls class B who does some asynchrous staff
there, at the end, the callback must run the function in
class A. So, class A passes the name of function to class
B, then class B uses this name to run class A's function.

Any suggestion?

regards
-----Original Message-----
Hi Li Pang,

Have a look at "Delegates".... a new technology for VB.NET. That shouldsolve your problem.

Regards,
Jan

"Li Pang" <li*****@eu.altria.com> wrote in message
news:04****************************@phx.gbl...
Hi,

I'd like to know how to call a sub/function bu using a
string containing its name, as an example as follow:

Sub Main()
Dim name As String = "TestMe()"
' need help here

End Sub

Public Sub TestMe()
MsgBox("Hello")
End Sub

Thanks in advance

.

Nov 20 '05 #4
Hi Armin,

Thanks for your information. To avoid the errors, is that
possible to use some methods to check if the functions
exist before call them?

regards
-----Original Message-----
"Li Pang" <li*****@eu.altria.com> schrieb
Hi,

I'd like to know how to call a sub/function bu using a
string containing its name, as an example as follow:

Sub Main()
Dim name As String = "TestMe()"
' need help here

End Sub

Public Sub TestMe()
MsgBox("Hello")
End Sub

Thanks in advance
What happens if the procedure name does not exist? The

application willcrash because the compiler can't find this error.

If you really need it, you can use CallByName (shared member ofMicrosoft.VisualBasic.Interaction).
--
Armin

.

Nov 20 '05 #5
Li,
Look at the classes in the System.Reflection namespace. Which is what the
CallByName method is built-on.

You can use System.Reflection to get a list of methods for an object, given
this list you can verify that the name exists.

If the name is known at compile time I would recommend using a Delegate as
Jan suggested, if you are reading the name from a source external to your
program, then you will need to use CallByName along with the
System.Reflection namespace to call the method.

Of course reading the name of the function to call from an external source
opens a back door into your program allowing functions that should not be
called to be called!

I do not have a specific example of Reflection to verify the name for you.

Hope this helps
Jay

"Li Pang" <li*****@eu.altria.com> wrote in message
news:0a****************************@phx.gbl...
Hi Armin,

Thanks for your information. To avoid the errors, is that
possible to use some methods to check if the functions
exist before call them?

regards
-----Original Message-----
"Li Pang" <li*****@eu.altria.com> schrieb
Hi,

I'd like to know how to call a sub/function bu using a
string containing its name, as an example as follow:

Sub Main()
Dim name As String = "TestMe()"
' need help here

End Sub

Public Sub TestMe()
MsgBox("Hello")
End Sub

Thanks in advance


What happens if the procedure name does not exist? The

application will
crash because the compiler can't find this error.

If you really need it, you can use CallByName (shared

member of
Microsoft.VisualBasic.Interaction).
--
Armin

.

Nov 20 '05 #6
"Li Pang" <li*****@eu.altria.com> schrieb
Hi Armin,

Thanks for your information. To avoid the errors, is that
possible to use some methods to check if the functions
exist before call them?


What would you do if it is not there but you need it?

You could put the code in a try-catch block to catch the exception.
--
Armin

Nov 20 '05 #7
* "Li Pang" <li*****@eu.altria.com> scripsit:
Thanks for your information. To avoid the errors, is that
possible to use some methods to check if the functions
exist before call them?


You can use Reflection to get the methods of a class. Have a look at the
'GetMethod' method of the 'Type' class.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
2
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but...
3
by: Alexander Gräf | last post by:
Hello, I'm stuck with a simple problem, for which I don't have a solution. I basically have an XML file containing fragments of plain text and html, in several languages: <?xml version="1.0"...
35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
2
by: Gregor Horvath | last post by:
Hi, Is there a possibility to format a log message to give the function name where the log appears? Example import logging def aTestFunction():
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.