dynamic-variables

Dynamic variable in C#?

让人想犯罪 __ 提交于 2019-11-27 05:06:52
Is it possible to use a dynamic variable (not sure about naming) in C#? In PHP, I can do $var_1 = "2"; $var_2 = "this is variable 2"; $test = ${"var_".$var_1}; echo $test; output: this is variable 2; Can we do this in C#? In C#, you use dictionaries to associate values with strings. No, basically. The compiler doesn't guarantee that method variables will exist (in their form as written), or with names... If they were fields (instance or static), then you could use reflection to get the values; but not method variables. For what you want, perhaps use a dictionary as a substitute? var vars = new

JavaScript Dynamic Variable Names

心不动则不痛 提交于 2019-11-26 22:54:49
Ok so I want to create variables as a user clicks threw the code every click adds a new variable. I am currently using jquery and javascript I can't do it server side this must be done in the browser. newCount = document.getElementById('hello').innerHTML; $('.hello').click(function(){ //set count fast enumeration newCount++; var hello + newCount = '<p>Hello World</p>'; }); so I want the variables to be hello1, hello2, hello3, hello4, etc. You can only do that with bracket notation, which means you have to attach the variables to something. The global scope would be window, so that would be

Is it a good idea to dynamically create variables?

自古美人都是妖i 提交于 2019-11-26 21:58:28
问题 I recently found out how to dynamically create variables in python through this method: vars()['my_variable'] = 'Some Value' Thus creating the variable my_variable . My question is, is this a good idea? Or should I always declare the variables ahead of time? 回答1: I think it's preferable to use a dictionnary if it's possible: vars_dict = {} vars_dict["my_variable"] = 'Some Value' vars_dict["my_variable2"] = 'Some Value' I think it's more pythonic. 回答2: This is a bad idea, since it gets much

Calling dynamic variable in PowerShell

拥有回忆 提交于 2019-11-26 14:47:29
问题 I am trying to create a new variable that would use other variable with dynamic name as its value. Here's what I am trying to do: I have a System.Array with two values: $Years = 2015, 2016 Another variable $Transactions has a list of various transactions. I am trying to use each of those $Years values in the following way: ForEach($Year in $Years){ New-Variable -Name "Transactions_$Year" -Value $Transactions | Where {$_.Date -like "*.$Year" Now what I would like to do (within that same

Is there an easy way to create dynamic variables with Javascript?

不问归期 提交于 2019-11-26 13:18:37
问题 I've built a data-driven google map with different icons that get assigned to the map depending on the type of item located. So if I have 5 types of landmark, and each gets a different icon (store, library, hospital, etc.)-- what I'd like to do is generate the google icon objects dynamically. I was thinking something like this: types = array('hospital','church','library','store',etc); var i=0; while (i<=types.length) { var landmark + i = new google.maps.Icon(); landmark.image = "icon" + i + "

Struts 2 dynamic variables

空扰寡人 提交于 2019-11-26 11:35:18
问题 I\'m trying to create a dynamic variable in Struts2 using set tag <s:set var=\"myNum\" value=\"numConst\" /> <s:set var=\"number\" value=\"%{getText(\'@xxx.CommonConstant@\'+#myNum)}\" /> numConst will return a dynamic value that retrieved from database. For example, if the value is NINE then number should be @xxx.CommonConstant@NINE I have set the value in my java class so that @xxx.CommonConstant@NINE will return 9 . So far, the value can be displayed with no problem in text tag if I use <s

JavaScript Dynamic Variable Names

三世轮回 提交于 2019-11-26 07:46:04
问题 Ok so I want to create variables as a user clicks threw the code every click adds a new variable. I am currently using jquery and javascript I can\'t do it server side this must be done in the browser. newCount = document.getElementById(\'hello\').innerHTML; $(\'.hello\').click(function(){ //set count fast enumeration newCount++; var hello + newCount = \'<p>Hello World</p>\'; }); so I want the variables to be hello1, hello2, hello3, hello4, etc. 回答1: You can only do that with bracket notation

Are Variable Operators Possible?

夙愿已清 提交于 2019-11-26 00:59:06
问题 Is there a way to do something similar to either of the following: var1 = 10; var2 = 20; var operator = \"<\"; console.log(var1 operator var2); // returns true -- OR -- var1 = 10; var2 = 20; var operator = \"+\"; total = var1 operator var2; // total === 30 回答1: Not out of the box. However, it's easy to build by hand in many languages including JS. var operators = { '+': function(a, b) { return a + b }, '<': function(a, b) { return a < b }, // ... }; var op = '+'; alert(operators[op](10, 20));

Using braces with dynamic variable names in PHP

泄露秘密 提交于 2019-11-25 21:47:21
问题 I\'m trying to use dynamic variable names (I\'m not sure what they\'re actually called) But pretty much like this: for($i=0; $i<=2; $i++) { $(\"file\" . $i) = file($filelist[$i]); } var_dump($file0); The return is null which tells me it\'s not working. I have no idea what the syntax or the technique I\'m looking for is here, which makes it hard to research. $filelist is defined earlier on. 回答1: Wrap them in {} : ${"file" . $i} = file($filelist[$i]); Working Example Using ${} is a way to

Are Variable Operators Possible?

主宰稳场 提交于 2019-11-25 21:40:52
Is there a way to do something similar to either of the following: var1 = 10; var2 = 20; var operator = "<"; console.log(var1 operator var2); // returns true -- OR -- var1 = 10; var2 = 20; var operator = "+"; total = var1 operator var2; // total === 30 Not out of the box. However, it's easy to build by hand in many languages including JS. var operators = { '+': function(a, b) { return a + b }, '<': function(a, b) { return a < b }, // ... }; var op = '+'; alert(operators[op](10, 20)); You can use ascii-based names like plus , to avoid going through strings if you don't need to. However, half of