473,609 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CodeEntryPointM ethod()
Dim cs1 As New CodeMethodInvok eExpression( _
New CodeTypeReferen ceExpression("S ystem.Console") , _
"WriteLine" , _
New CodePrimitiveEx pression("Hello World!") )
start.Statement s.Add(cs1)
*****

Please assist
Apr 22 '06 #1
4 1411
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*******@hotm ail.com> schreef in bericht
news:%2******** ********@TK2MSF TNGP05.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 CodeEntryPointM ethod()
Dim cs1 As New CodeMethodInvok eExpression( _
New CodeTypeReferen ceExpression("S ystem.Console") , _
"WriteLine" , _
New CodePrimitiveEx pression("Hello World!") )
start.Statement s.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.Reflecti on
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.GetMeth od(prmMethodNam e,
BindingFlags.No nPublic Or BindingFlags.Pu blic Or BindingFlags.St atic Or
BindingFlags.In stance)

Return objMethodInfo.I nvoke(Me, prmParams)
End Function

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

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

Public Function(prmArg ument1 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.ExecuteMetho ds(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(prmFor mName As String)
Dim app As System.Reflecti on.Assembly =
System.Reflecti on.Assembly.Get ExecutingAssemb ly()
Try
Dim frm As Form = app.CreateInsta nce("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*********@Ea gleCCI.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.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.Reflecti on
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.GetMeth od(prmMethodNam e,
BindingFlags.No nPublic Or BindingFlags.Pu blic Or BindingFlags.St atic Or
BindingFlags.In stance)

Return objMethodInfo.I nvoke(Me, prmParams)
End Function

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

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

Public Function(prmArg ument1 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.ExecuteMetho ds(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(prmFor mName As String)
Dim app As System.Reflecti on.Assembly =
System.Reflecti on.Assembly.Get ExecutingAssemb ly()
Try
Dim frm As Form = app.CreateInsta nce("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
13610
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 * from Employee;" <% Dim objRS, sqlStmt set objRS = Server.CreateObject("ADODB.Recordset") Dim conn Set conn = Server.CreateObject("ADODB.Connection")
1
1963
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 many tables with hundreds of fields). I can run the script in the mysql interface but I want to do this from my access front end to make it easy for the user to set up the tables. I need something like myconnection.execute...
1
1979
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 6.0 application which uses SQL Server as the back-end. Here's an example of what I'm wanting to do... A user accessing the VB GUI attempts to open a certain form. Code
2
2030
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 up a custom form to accept the input parameters (MS Feature Request!) for the Stored Procedure. We had two problems with MS's
2
1560
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
347
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 variable how much values i get.... This is my code: Public Function ExSP(ByVal StoredProcedure As String, ByVal Parameters As ArrayList, ByVal ParamName As ArrayList) As ArrayList
12
37056
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 the SQL commands are given bellow This line is used to generate the required query and stored to a variable @mnvcSQL select @mnvcSQL='Select @Prev=year' + convert(varchar,cast((@Field-1) as int))+ ' from tblFinancialDistribution where...
0
1428
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 connection to the AS400 is good, because this same ASP page is reading data via SQL. However, when I try to call the stored procedure, I can't tell if anything is actually happening. All that I know is that the end result is not being changed, so...
2
3731
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 now i want to execute the sql statement how (MS SQL SERVER 2000)
0
8573
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8541
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8406
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6057
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2531
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.