creating dynamic variables at run time

  • Thread starter Thread starter sony.m.2007
  • Start date Start date
S

sony.m.2007

Hi,
is there anyway to create dynamic variables at the runtime based on a
string input from another function

string datatypestring="int";
using this datatypestring i need to create a integer variable at run
time.is there any methods available in reflection to create dynamic
variables(like int, double etc)


Thanks,
Sony
 
is there anyway to create dynamic variables at the runtime based on a
string input from another function

string datatypestring="int";
using this datatypestring i need to create a integer variable at run
time.is there any methods available in reflection to create dynamic
variables(like int, double etc)

You can use reflection - possible supplemented with a simple
switch for the most common types.

But what do you need it for ? Do you want dynamic
properties and not local variables ?

Arne
 
Hi,
is there anyway to create dynamic variables at the runtime based on a
string input from another function

string datatypestring="int";
using this datatypestring i need to create a integer variable at run
time.is there any methods available in reflection to create dynamic
variables(like int, double etc)


Thanks,
Sony

My natural reaction every time this kind of question comes up is:

- Why do you think that you need to create variables dynamically?

So far I have never seen a good reason...

In a compiled language it's far from convenient to create variables
dynamically. Using reflection it's possible, but it's almost always
pointless. In scripting languages like VBScript and Javascript it's
easier, but even there it's almost always unneccesary.

Instead of asking about how it's done the way that you think that your
problem should be solved, I think that you should ask about whatever it
is you intend to use this for.
 
My natural reaction every time this kind of question comes up is:

- Why do you think that you need to create variables dynamically?

So far I have never seen a good reason...

For fields then a Dictionary<> usually works fine.

The only reason I know for doing similar stuff is to
dynamicly generate a class at runtime with specific
properties, because objects are to be given to something
that expects properties not a Dictionary<>.

Arne
 
Back
Top