Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2008

Tip: Looking for answers? Try searching our database.

Executing command/instruction from a string variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gancy - 27 Mar 2008 10:22 GMT
I need to execute the command/instruction which is stored in a
variable.  ex

string str = "drRow["userid"].ToString()"

here i need execute the instruction stored in 'str' and get the value
from executing

drGalileoToday["userid"].ToString()

I have no idea as how this could be done.  Please advise
Jon Skeet [C# MVP] - 27 Mar 2008 10:27 GMT
> I need to execute the command/instruction which is stored in a
> variable.  ex
[quoted text clipped - 7 lines]
>
> I have no idea as how this could be done.  Please advise

You pretty much can't, in C#. You can build whole assemblies, but
executing a single statement with the current context is a different
matter.

Now, I suggest you take a step back and try to explain *why* you want
to do this - what the larger requirement is. We may be able to suggest
a more pragmatic approach.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gancy - 27 Mar 2008 10:37 GMT
> > I need to execute the command/instruction which is stored in a
> > variable.  ex
[quoted text clipped - 19 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

Hey thanks for quick response.

Actually I have an XML schema that looks like this

<xs:element name="CalculationInstructions" minOccurs="0">
                 <xs:complexType>
                   <xs:sequence>
                     <xs:element name="leftOperand" type="xs:string"/

                     <xs:element name="rightOperand" type="xs:string"/

                     <xs:element name="operator" type="xs:string"/>
                     <xs:element name="type" type="xs:string"/>
                   </xs:sequence>
                 </xs:complexType>
               </xs:element>

here I use XML to define the operation along with operands so as to
keep my program logic independant of the calculations.  so

  <CalculationInstructions>
     <leftOperand>https://xyz-abc/Overview.aspx?id=</leftOperand>
     <rightOperand>drToday["userid"].ToString()</rightOperand>
     <operator>+</operator>
     <type>string</type>
   </CalculationInstructions>

from my C# program I would read this XML and perform leftOperand +
righOperand.  One of the operand could be language instruction.  in
this case rightOperand would have drToday["userid"].ToString() and
executing that in the program would give me id say '777' which would
result in

https://xyz-abc/Overview.aspx?id=777
Jon Skeet [C# MVP] - 27 Mar 2008 10:51 GMT
> Hey thanks for quick response.
>
[quoted text clipped - 30 lines]
>
> https://xyz-abc/Overview.aspx?id=777

I think you'd be a lot better off making the data model available to
your calculations (e.g. have property paths such as Today.UserId)
rather than embedding C# in your XML.

You haven't made the program logic independent of the calculations -
your calculation is relying a particular variable name, for starters.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gancy - 27 Mar 2008 11:08 GMT
> > Hey thanks for quick response.
>
[quoted text clipped - 43 lines]
>
> - Show quoted text -

Yes you are correct.  I will have to rethink on this.  thanks for
advise

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.