473,411 Members | 2,014 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,411 software developers and data experts.

Trying to execute something stored in variable

April 22, 2006

Hi all

I want to execute a command stored in a variable, eg I may store:

Form1.Show()
or
Form2.Show()

in the variable and then execute whatever is in the variable.

I read about the System.CodeDom namespace in MSDN, however I am unable to
get the example given to work. Does this do what I have in mind ?

*****
Dim start As New CodeEntryPointMethod()
Dim cs1 As New CodeMethodInvokeExpression( _
New CodeTypeReferenceExpression("System.Console"), _
"WriteLine", _
New CodePrimitiveExpression("Hello World!") )
start.Statements.Add(cs1)
*****

Please assist
Apr 22 '06 #1
4 1392
Mike,

Are you person 1001 who tries to make a kind of Office Access Solution, you
change to succeed is probably the same as if you try to make a new
professional Texteditor?

It leads probably to nothing, however have a search in this newsgroup for
all those late binding solutinons using reflection.

Just my idea, maybe it does not help, or in the other hand maybe a lot
saving you a lot of time.

Cor
"Mike TI" <su*******@hotmail.com> schreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
April 22, 2006

Hi all

I want to execute a command stored in a variable, eg I may store:

Form1.Show()
or
Form2.Show()

in the variable and then execute whatever is in the variable.

I read about the System.CodeDom namespace in MSDN, however I am unable to
get the example given to work. Does this do what I have in mind ?

*****
Dim start As New CodeEntryPointMethod()
Dim cs1 As New CodeMethodInvokeExpression( _
New CodeTypeReferenceExpression("System.Console"), _
"WriteLine", _
New CodePrimitiveExpression("Hello World!") )
start.Statements.Add(cs1)
*****

Please assist

Apr 22 '06 #2
Hi Mike,

I have two routines that allow you to either open a form if the name of
the form is stored in a variable, or call a function (or subroutine) if
the name of the function is stored in a variable:

Note - The functions that are being called are actually methods of a
class, and the routine to call these functions is stored in the same
class. So, this code will need to be modified if you can't arrange
your application so that all possible functions that get called in this
manor are located in the same class. Most of the time names of
functions aren't stored in variables. I happen to build a Menu Strip
on the fly based on information in a database. 99% of the time when a
menu item is clicked, I open a form (using the below code). However,
there are a couple of situations when I need to call a function when a
menu item is clicked.

Here's the class for calling a function stored in a variable:

Imports System.Reflection
Public Class CallFunctions
Public Function ExecuteMethods(ByVal prmMethodName As String,
Optional ByVal prmParams() As Object = Nothing) As Object
'Create a type object and store the type of the object passed
Dim objType As Type = Me.GetType

'Declare a MethodInfo object to store the Methods of the class
Dim objMethodInfo As MethodInfo

'Get the MethodInfo for the current class. Binding Flags are
specified to get the public and private Methods of this class. When
'Public or Non-Public is specified in the BindingFlags, it is
also necessary to specify Static or Instance
objMethodInfo = objType.GetMethod(prmMethodName,
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static Or
BindingFlags.Instance)

Return objMethodInfo.Invoke(Me, prmParams)
End Function

Public Sub SubRoutine1()
.......Your code here
End Sub

Public Sub SubRoutine2()
......Your code here
End Sub

Public Function(prmArgument1 as String) As String
.....Your code here
End Sub
End Class

You would use this class in your code as follows:

Dim cf As New CallFunctions
Call cf.ExecuteMethods(variablename, array of parameters)
You can leave the second argument empty if you aren't passing any
parameters.
On the other hand, if you need to open a form and the name of the form
is stored in a variable, you can do this:

Public Sub OpenForm(prmFormName As String)
Dim app As System.Reflection.Assembly =
System.Reflection.Assembly.GetExecutingAssembly()
Try
Dim frm As Form = app.CreateInstance("Namespace." &
prmFormName, True)
frm.Show()
Catch e as Exception
End try
End Sub

You need to substitute "Namespace." with your namespace (and the
period).

Please note that I cannot take full credit for these routines. I have
found most of this in other places on the Internet and I modified them
slightly for my use.

Hope this helps.

Steve

Apr 22 '06 #3
April 23, 2006

You have made my day.

Thank you.
Mike TI

<St*********@EagleCCI.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Hi Mike,

I have two routines that allow you to either open a form if the name of
the form is stored in a variable, or call a function (or subroutine) if
the name of the function is stored in a variable:

Note - The functions that are being called are actually methods of a
class, and the routine to call these functions is stored in the same
class. So, this code will need to be modified if you can't arrange
your application so that all possible functions that get called in this
manor are located in the same class. Most of the time names of
functions aren't stored in variables. I happen to build a Menu Strip
on the fly based on information in a database. 99% of the time when a
menu item is clicked, I open a form (using the below code). However,
there are a couple of situations when I need to call a function when a
menu item is clicked.

Here's the class for calling a function stored in a variable:

Imports System.Reflection
Public Class CallFunctions
Public Function ExecuteMethods(ByVal prmMethodName As String,
Optional ByVal prmParams() As Object = Nothing) As Object
'Create a type object and store the type of the object passed
Dim objType As Type = Me.GetType

'Declare a MethodInfo object to store the Methods of the class
Dim objMethodInfo As MethodInfo

'Get the MethodInfo for the current class. Binding Flags are
specified to get the public and private Methods of this class. When
'Public or Non-Public is specified in the BindingFlags, it is
also necessary to specify Static or Instance
objMethodInfo = objType.GetMethod(prmMethodName,
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static Or
BindingFlags.Instance)

Return objMethodInfo.Invoke(Me, prmParams)
End Function

Public Sub SubRoutine1()
.......Your code here
End Sub

Public Sub SubRoutine2()
......Your code here
End Sub

Public Function(prmArgument1 as String) As String
.....Your code here
End Sub
End Class

You would use this class in your code as follows:

Dim cf As New CallFunctions
Call cf.ExecuteMethods(variablename, array of parameters)
You can leave the second argument empty if you aren't passing any
parameters.
On the other hand, if you need to open a form and the name of the form
is stored in a variable, you can do this:

Public Sub OpenForm(prmFormName As String)
Dim app As System.Reflection.Assembly =
System.Reflection.Assembly.GetExecutingAssembly()
Try
Dim frm As Form = app.CreateInstance("Namespace." &
prmFormName, True)
frm.Show()
Catch e as Exception
End try
End Sub

You need to substitute "Namespace." with your namespace (and the
period).

Please note that I cannot take full credit for these routines. I have
found most of this in other places on the Internet and I modified them
slightly for my use.

Hope this helps.

Steve

Apr 23 '06 #4
You're welcome. Glad I could help.

Steve

Apr 23 '06 #5

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

Similar topics

2
by: Matt | last post by:
I want to exexute stored procedure in ASP, but it has error "Microsoft VBScript compilation (0x800A0401) Expected end of statement" on line (1). The stored procedure "sp_emp" contain "select *...
1
by: Paradigm | last post by:
Does anyone know how I can use vba in access to run a script that is stored as a file. I can make a connection to mysql database and run sql statements but this sql statement is very long (creates...
1
by: Brad H McCollum | last post by:
I've looked through many suggestions and partial examples all over this newsgroup and still am not coming up with anything that does specifically what I'm wanting to accomplish. I'm writing a VB...
2
by: Noloader | last post by:
Hello, Access XP, SQL Server 2000 Is it possible to hide a SP under Queries in Access, yet still be able to Execute it from Access? (Similar to hiding Tables, then using Views) We hooked...
2
by: Mike TI | last post by:
April 20, 2006 Hi All Thank you every one for your help, I have now started to get the feel of VB.Net 2005. How can I execute something stored in a variable. For example, I want to store:
4
by: Dikkuuhh | last post by:
Hi, i have a function that can execute stored procedures, but i don`t know how i can give it back to the other classes. I did it in an ArrayList now, but how can i do it better, i can`t make it...
12
by: sukeshchand | last post by:
I want to exicute a Query that stored in a variable. and the Query is used to get a given value to a variable. I tried to use exec command but that returns an error. i cant get an idea for how to do...
0
by: bobby_b_ | last post by:
Hello, I'm trying to access a stored procedure on our AS400 that is written in RPG. I know that the stored procedure works (I've called it from an RPG program on the AS400), and I know that the...
2
by: hisham123 | last post by:
hi, I Wrote one stored procedure in which i declare one variable as Table then i stored in that table type variable select * from emp so now my table contain one sql statement by 4 rows...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.