Prev: Passing nullable string
Next: Global.asax Session_End
From: sony.m.2007 on 30 Dec 2008 12:38 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
From: cfps.Christian on 30 Dec 2008 15:14 Do some research on Activator.CreateInstance. It gets costly and the code can get hairy but every once in a while you need it.
From: Mark Rae [MVP] on 30 Dec 2008 15:57 <sony.m.2007(a)googlemail.com> wrote in message news:f2b811bc-1f58-4726-b4b0-254f927f4ec8(a)g1g2000pra.googlegroups.com... > 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. Apart from Activator.CreateInstance, I've seen some fairly nasty workarounds for this. Here's one using generics: http://www.codeproject.com/KB/cs/csdynamicprops.aspx -- Mark Rae ASP.NET MVP http://www.markrae.net
From: Arne Vajhøj on 30 Dec 2008 22:40 sony.m.2007(a)googlemail.com wrote: > 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
From: Göran Andersson on 31 Dec 2008 02:12
sony.m.2007(a)googlemail.com wrote: > 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. -- G�ran Andersson _____ http://www.guffa.com |