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

Calling a method at runtime

I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)
Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?
Nov 20 '05 #1
10 1247
Short Answer: No

Use the if or select statements :P

Rico Rivera wrote:
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)
Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?

Nov 20 '05 #2
Rico

Of Course

Case select myMethode
Case "A"
A
Case "B"
B
End Select

Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
....
End Sub

I hope this helps?

Cor
Nov 20 '05 #3
Assuming that the signatures of the methods are the same, you can use a
delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

....

Sub A(SomeString As String)
....
End Sub

Sub B(SomeString As String)
....
End Sub

HTH

David Williams [VB.NET MVP]
"Rico Rivera" <qR********@spamhole.com> wrote in message
news:40****************@news.verizon.net:
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)
Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?


Nov 20 '05 #4
There may be a way of doing this, I'm sure Ive seen it answered before but
it really is i'll advised to take this approach

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Les Hughes" <le*************@datarev.com.au> wrote in message
news:e5**************@TK2MSFTNGP09.phx.gbl...
Short Answer: No

Use the if or select statements :P

Rico Rivera wrote:
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)
Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?

Nov 20 '05 #5
Hi David,

And than build a complete program around it to prevent runtime errors?
Sometimes technical methods are simple however if it is a solutions is often
the question.

Just my thought.

Cor
Assuming that the signatures of the methods are the same, you can use a
delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

...

Sub A(SomeString As String)
...
End Sub

Sub B(SomeString As String)
...
End Sub

Nov 20 '05 #6
Hi David,

Yes, that is what I am looking for... one question. When you had:
someMethodDelegate = AddressOf A
Method 'A' is hard coded... I needed something like

someMethodDelegate = AddressOf s

s is a String that could have A or B or C (which are methods). In
other words I don't know what method to call until runtime and based
on the string, then the method would get called. All signatures will
be the same.

Thanks
Rico

On Mon, 05 Jul 2004 09:19:42 -0700, David Williams , VB.NET MVP
<da********************@earthlink.net> wrote:
Assuming that the signatures of the methods are the same, you can use a
delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

...

Sub A(SomeString As String)
...
End Sub

Sub B(SomeString As String)
...
End Sub

HTH

David Williams [VB.NET MVP]
"Rico Rivera" <qR********@spamhole.com> wrote in message
news:40****************@news.verizon.net:
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)
Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?


Nov 20 '05 #7
* qR********@spamhole.com (Rico Rivera) scripsit:
I want to call a method dynamically based on a string


Have a look at 'CallByName', altneratively there is a way using
reflection.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8
Rico,

I use the following where MethodName is the name of the method to invoke.

<code (VB.NET) >
Dim aType As Type = SomeClass.GetType
Dim Args() as Object = {Arg1, Arg2,...}
Dim aFunctionReturn As Object
aFunctionReturn = aType.InvokeMember(MethodName _
, Reflection.BindingFlags.InvokeMethod
_
, Nothing _
, aFeatureClass.Instance _
, Args)
Return aFunctionReturn

</code>

In my applications everything is a "plugin", so the "host" application looks
at the database to determine the users available options. From this list I
load the appropriate classes at runtime and invoke a standard set of methods
on the class which are defined by an interface.

-Sam Matzen

-Sam Matzen
"Rico Rivera" <qR********@spamhole.com> wrote in message
news:40****************@news.verizon.net...
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)
Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?

Nov 20 '05 #9
I don't know about that. I use delegates like this all the time and
catch any exceptions the same what as without delegates. I find that
the delegates help make the code cleaner.

David

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl:
Hi David,

And than build a complete program around it to prevent runtime errors?
Sometimes technical methods are simple however if it is a solutions is
often
the question.

Just my thought.

Cor
Assuming that the signatures of the methods are the same, you can use
a
delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

...

Sub A(SomeString As String)
...
End Sub

Sub B(SomeString As String)
...
End Sub


Nov 20 '05 #10
That is fine. Just be sure to catch any exceptions that happen when "s"
is not a valid function name or the function that is referenced by "s"
has the wrong signature. I do this all the time.

HTH

David

"Rico Rivera" <qR********@spamhole.com> wrote in message
news:40****************@news.verizon.net:
Hi David,

Yes, that is what I am looking for... one question. When you had:
someMethodDelegate = AddressOf A


Method 'A' is hard coded... I needed something like

someMethodDelegate = AddressOf s

s is a String that could have A or B or C (which are methods). In
other words I don't know what method to call until runtime and based
on the string, then the method would get called. All signatures will
be the same.

Thanks
Rico

On Mon, 05 Jul 2004 09:19:42 -0700, David Williams , VB.NET MVP
<da********************@earthlink.net> wrote:
Assuming that the signatures of the methods are the same, you can use a

delegate to do so...

Dim someMethodDelegate(SomeSortOfString As String) As [Delegate]

someMethodDelegate = AddressOf A
someMethodDelegate(SomeSortOfString)

...

Sub A(SomeString As String)
...
End Sub

Sub B(SomeString As String)
...
End Sub

HTH

David Williams [VB.NET MVP]
"Rico Rivera" <qR********@spamhole.com> wrote in message
news:40****************@news.verizon.net:
I want to call a method dynamically based on a string

For example

When s = "A" Then method A would get called. I don't wan't to use a
bunch of "If" statements.

Something like:

someMethod = AddressOf s 's = A

someMethod(SomeSortOfString)
Sub A(ByRef SomeString As String)
...
End Sub

Sub B(ByRef SomeString As String)
...
End Sub

Is this possible?


Nov 20 '05 #11

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

Similar topics

7
by: Doug Rosser | last post by:
I'm writing a fairly complicated test framework and keeping configuration data inside ini files that are parsed at runtime by the ConfigParser module. For example, there would be a section...
3
by: Cindy Liu | last post by:
Hi Everyone, I created C# COM+ component. It has two overloaded methods - the method names are same and their signatures are different, one takes two parameters and another takes four. I coded...
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
1
by: Paul Brun | last post by:
Hi guys, I get the following error during runtime: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at SXInit(Int32 ) The SXInit...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
8
by: Olivier BESSON | last post by:
Hello, VB.NET 1.1 IIS 6 I'm developping a winform client app of a web service of mine. I used to set the IDE with "halt in debugger" on "common language runtime exceptions". Every time i...
1
by: developing | last post by:
Hey all , Is there a way to get the instance name at runtime? I had homework assignment to calculate great circle distance between two cities and a few conversions....Im done them all...except: ...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: 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...

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.