创建动态的变量名 [英] Create dynamic variable name

查看:132
本文介绍了创建动态的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以用C#创建动态的变量?

我知道我下面code是扔的错误和编码很差。但是,这code有小的逻辑如创建动态的变量

 变数名称= 0;
对于(I = 0; I&小于10;我+ +)
{
    名+ I = I;
}VAR XX1 = NAME1;
VAR XX2 = NAME2;
VAR XX3 = NAME3;


  

是否有可能在C#中? C#创建动态的变量?并改变在C#中的变量名?而在C#中串联变量名(就像我们可以连接任何控制标识或名称的)...


为什么需要改变变量名称:

  VAR VARIABLENAME =
VAR variablename0 =否;
VAR variablename1 =是;
VAR variablename2 =否;



请想我有一个 GridView控件多行。和需要服务器端变量分配给每一行。我已经设置服务器端的一些变量名。所以我已经设置
文本= LT;%#VARIABLENAME + rowCount等%方式> 的一个模板字段

rowCount等意味着每个网格行索引。

如果网格具有2行,那么rowCount等值是0,1,2

现在我需要改变VARIABLENAME到variablename0,variablename1,variablename2在单独row.So我需要动态改变的变量名。


解决方案

C#是强类型的,所以你不能动态地创建变量。可以使用的阵列,但更好的C#的方法是使用一个词典如下。更多关于 C#这里字典。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;命名空间的QuickTest
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            字典<字符串,整数>名称=新词典<字符串,整数>();
            的for(int i = 0;我小于10;我++)
            {
                names.Add(的String.Format(名称{0},i.ToString()),I);
            }            VAR XX1 =名称[名称1];
            VAR XX2 =名称[NAME2];
            VAR XX3 =名称[NAME3];
        }
    }
}

Can we create dynamic variable in C#?

I know my below code is threw error and very poor coding. But this code have small logic like create dynamic variable

var name=0;
for(i=0;i<10;i++)
{
    name+i=i;
}

var xx1=name1;
var xx2=name2;
var xx3=name3;

Is it possible in c#? Create dynamic variable in c#? and change the variable name in c#? and concatenate the variable name in c#(like we can concatenate any control id or name)...

Why i need change the variable name :

var variablename=""
var variablename0=No;
var variablename1=Yes;
var variablename2=No;

. . .

Please think i have one gridview with multiple rows. and need assign server side variable to every row. I have already set some variable name in server side. So i have set Text=<%# variablename+rowCount%> for one template field .

This rowCount means every grid row index.

If the grid has 2 rows, Then rowCount values are 0,1,2

Now i need to change variablename to variablename0,variablename1,variablename2 in separate row.So i need change the variable name dynamically.

解决方案

C# is strongly typed so you can't create variables dynamically. You could use an array but a better C# way would be to use a Dictionary as follows. More on C# dictionaries here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> names = new Dictionary<string,int>();


            for (int i = 0; i < 10; i++)
            {
                names.Add(String.Format("name{0}", i.ToString()), i);
            }

            var xx1 = names["name1"];
            var xx2 = names["name2"];
            var xx3 = names["name3"];
        }
    }
}

这篇关于创建动态的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆