Answered by:
Variable holding a command

Question
-
Can I store a command (like messagebox.show) in a variable?Sunday, April 29, 2007 3:29 PM
Answers
-
The medium answer is sort of:
Code SnippetPrivate Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
CallByName(Me, "ShowMessagebox", CallType.Method)
End Sub
Public Sub ShowMessagebox()
MessageBox.Show("Showing")
End Sub
Edit:
I left out the variable.
Code SnippetPrivate Variable As String = "ShowMessageBox"
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
CallByName(Me, Variable, CallType.Method)
End Sub
Public Sub ShowMessageBox()
MessageBox.Show("Showing")
End Sub
Sunday, April 29, 2007 6:23 PM -
Use a delegate. For example:
Public Class Form1
Private Delegate Function ShowMessageDelegate(ByVal msg As String) As DialogResult
Private mShower As ShowMessageDelegate
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mShower = AddressOf MessageBox.Show
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mShower("howdy")
End Sub
End ClassSunday, April 29, 2007 6:27 PMModerator
All replies
-
The short answer is NO.
Sunday, April 29, 2007 5:06 PM -
The medium answer is sort of:
Code SnippetPrivate Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
CallByName(Me, "ShowMessagebox", CallType.Method)
End Sub
Public Sub ShowMessagebox()
MessageBox.Show("Showing")
End Sub
Edit:
I left out the variable.
Code SnippetPrivate Variable As String = "ShowMessageBox"
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
CallByName(Me, Variable, CallType.Method)
End Sub
Public Sub ShowMessageBox()
MessageBox.Show("Showing")
End Sub
Sunday, April 29, 2007 6:23 PM -
Use a delegate. For example:
Public Class Form1
Private Delegate Function ShowMessageDelegate(ByVal msg As String) As DialogResult
Private mShower As ShowMessageDelegate
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mShower = AddressOf MessageBox.Show
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mShower("howdy")
End Sub
End ClassSunday, April 29, 2007 6:27 PMModerator -
Hi JohnWein & nobugz,
I've heard it is possible to create a command Alias too.
Can one of you give an example as the OP may find that idea useful too.
Regards,
S_DS
Monday, April 30, 2007 12:42 AM -
Could you use the System.CodeDom om namespace?Tuesday, June 12, 2007 8:40 PM
-
NateNate,
If you don't mind me asking, why would one want to hold a messagebox command in a variable?
Wednesday, June 13, 2007 1:26 AM -
That was just an example.Friday, June 15, 2007 3:35 PM
-
I think the short answer was probably the realistic answer to your question.
VB.NET is a a compiled language and not an interpreted scripting language. In the further examples using delegates or callbyName suggested by John Weir and Nobugz the will indeed work but are really allowing you to call a method and adjust things a little programmatically but these are limited and are not really holding a program statement in a variable and executing it. So they may work with simple method calls but if your expecting to be able to store VB statements in variables and then execute them this isnt going to work.
So you cant store something like "Dim x as new foo" in a a variable and then "x.bar = 1" in another and execute them one after the other.
So I guess the medium answer may work for you but the short answer is probably the realistic one for being able to stored VB statements in variables and execute them at runtime.
Friday, June 15, 2007 7:21 PM