Executing command/instruction from a string variable

G

Gancy

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
 
J

Jon Skeet [C# MVP]

Gancy said:
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

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.
 
G

Gancy

Gancy said:
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

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.

Hey thanks for quick response.

Actually I have an XML schema that looks like this

<xs:element name="CalculationInstructions" minOccurs="0">
<xs:complexType>
<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
 
J

Jon Skeet [C# MVP]

Gancy said:
Hey thanks for quick response.

Actually I have an XML schema that looks like this

<xs:element name="CalculationInstructions" minOccurs="0">
<xs:complexType>
<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

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.
 
G

Gancy

Gancy said:
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

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.

--
Jon Skeet - <[email protected]>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk- Hide quoted text -

- Show quoted text -

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top