如何获取数组中元素的名称

时间:2016-01-15 14:22:40

标签: c# .net arrays visual-studio

我有一个数据表作为spec-flow中的一个步骤的一部分。我想阅读'放入表中的值并记录值以及输入表的列/行。

我保存了表的每个单元格并创建了一个包含所有框的字符串数组,并使用以下代码检查表中是否存在任何内容:

<!doctype HTML>
<html>

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

    <script src="/angular2/bundles/angular2-polyfills.js"></script>
    <script src="/systemjs/dist/system.src.js"></script>
    <script src="/rxjs/bundles/Rx.js"></script>
    <script src="/angular2/bundles/angular2.dev.js"></script>
    <script src="/angular2/bundles/upgrade.dev.js"></script>
    <script src="/angular2/bundles/upgrade.dev.js"></script>
    <script src="/css/main.css"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <!-- Latest compiled and minified JavaScript -->
    <!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>-->
    <style>
        * {
            border-radius: 0px !important;
        }

        body {
            background-color: rgba(251, 251, 251, 0.55) !important;
            border-radius: 0px !important;
            padding-top: 70px;
        }

        .dashboarContainer .panel-footer {
            padding: 1px 15px !important;
        }

        .dashboarContainer a {
            color: #333 !important;
        }

        .dashboarContainer a:hover {
            text-decoration: none !important;
        }

        .dashboarContainer .panel-body {
            height: 80px;
        }

        .tag {
            background-color: #EFEEEE;
            padding: 5px 10px;
            margin-right: 5px;
            display: inline-block;
            margin-bottom: 5px;
        }

        .myHr {
            margin-top: 10px;
            margin-bottom: 10px;
        }
    </style>
    <base href="/">
</head>

<body>

    <app>Loading......</app>

    <script>
        System.config({
        defaultJSExtensions: true,
        packages: {        
            app: {
                defaultExtension: 'js'
            },
           'ng2-bootstrap': {
                defaultExtension: 'js'
            }
        },
        paths: {
            'angular2/upgrade': '../node_modules/angular2/upgrade'
          },
        map: {
            'ng2-bootstrap': '/ng2-bootstrap'
        }   
      });
       System.import('/rxjs/operator/map');
        System.import('scripts/src/bootstrap');
    </script>
</body>

</html>

我已将表中输入的数字存储为数字,我还想将元素的名称存储在包含该数字的数组中,以便我可以在另一个列表中搜索它并为其分配数字。

2 个答案:

答案 0 :(得分:1)

我认为你正在寻找一本字典。字典本质上是一个键值对。键将是您的单元格编号或单元格名称,以及值,即单元格中的值。我们说密钥是string,值是int。这是您初始化词典的方式:

Dictationary<string, int> myDictionary = new Dictionary<string, int>();

然后,当您浏览该表时,您将向词典添加条目。您可以使用add方法,也可以只分配它。

myDictionary.Add("MyCellName", 20);

myDictionary["MyCellName"] = 20;

稍后在您的代码中,如果您需要检索单元格&#39; xyz&#39;的值,您只需使用:

var myValue = myDictionary["xyz"];

答案 1 :(得分:0)

如果您有 DataTable ,您可以使用此循环:

foreach (DataRow dtRow in tableInfo.Rows)
{

    foreach(DataColumn dc in tableInfo.Columns)
    {
      var field = dtRow[dc].ToString();
    }
}