creating dynamic variables at run time

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
 
A

Arne Vajhøj

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
 
G

Göran Andersson

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

Arne Vajhøj

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
 

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