I guess you changed the methodName variable to hold the exact name of your method? Are the methods you try to call defined in the same class? Please post your code.
Please 'Mark as Answer' if this post helped you.
Marked as answer by JAYHAWKER on Apr 09, 2012 05:49 PM
Dim TotalNxCalculations As String = "Total" & i & "xCalculations"
Dim mi As MethodInfo
mi = Me.GetType().GetMethod(TotalNxCalculations)
mi.Invoke(Me, New Object() {})
TotalNxCalculations(i & "xValue)
The functions are in the same class and here they are.
Private Function Total1xCalculations(ByRef 1xValue As System.Object)
txtItWorked = "1Worked"
Return (1xValue)
End Function
Private Function Total2xCalculations(ByRef 2xValue As System.Object)
txtItWorked = "2 Worked"
JAYHAWKER
Participant
1252 Points
1896 Posts
How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind page
Apr 08, 2012 12:55 PM|LINK
I have 4 subroutines in a vb codebehind page, and the only difference is that they end with a number 1 - 4
i.e. testSub1, testSub2, testSub3, testSub4
How can I nest through them with a
for i = 1 to 4
Dim testSubN as subroutine = "testSub" & i & ()
testsubN()
next
thanks
This doesn't work
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 01:06 PM|LINK
hi try this
for i = 1 as integer to 4
Dim testSub as string = "testSub" & i
testsub()
next
JAYHAWKER
Participant
1252 Points
1896 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 01:21 PM|LINK
Thanks for trying...I had already trid that and it is a no go! Any other ideas?
mm10
Contributor
6395 Points
1182 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 01:35 PM|LINK
Use reflection:
string methodName = "TestSub1";
MethodInfo mi = this.GetType().GetMethod(methodName);
mi.Invoke(this, null);
JAYHAWKER
Participant
1252 Points
1896 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 02:26 PM|LINK
Can you provide this for me in vb?
Thanks
mm10
Contributor
6395 Points
1182 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 02:46 PM|LINK
Dim methodName As String = "TestSub1"
Dim mi As MethodInfo
mi = Me.GetType().GetMethod(methodName)
mi.Invoke(Me, New Object() {})
JAYHAWKER
Participant
1252 Points
1896 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 03:18 PM|LINK
I am getting close, but I get the error:
Expression is not a method
mm10
Contributor
6395 Points
1182 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 04:38 PM|LINK
I guess you changed the methodName variable to hold the exact name of your method? Are the methods you try to call defined in the same class? Please post your code.
JAYHAWKER
Participant
1252 Points
1896 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 04:48 PM|LINK
Yes it is in the same class.
here is the vb code
for i = 1 to 4
Dim TotalNxCalculations As String = "Total" & i & "xCalculations" Dim mi As MethodInfo mi = Me.GetType().GetMethod(TotalNxCalculations) mi.Invoke(Me, New Object() {}) TotalNxCalculations(i & "xValue)The functions are in the same class and here they are.
Private Function Total1xCalculations(ByRef 1xValue As System.Object) txtItWorked = "1Worked"Return (1xValue) End FunctionPrivate Function Total2xCalculations(ByRef 2xValue As System.Object) txtItWorked = "2 Worked"Return (2xValue) End Functionetc....
mm10
Contributor
6395 Points
1182 Posts
Re: How can I declare a temporary variable to hold the name of a subroutine in a vb codebehind pa...
Apr 08, 2012 05:06 PM|LINK
The functions must be Public and in this case you need to pass an object parameter as argument:
mi.Invoke(Me, New Object() {New Object})