Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#将选择案例VBA转换为c#开关_C#_Vba - Fatal编程技术网

c#将选择案例VBA转换为c#开关

c#将选择案例VBA转换为c#开关,c#,vba,C#,Vba,我有一些vba代码需要转换成c# 我知道如何在这个问题上进行切换,但有更简单的方法吗 我不会检查单元格(rownum………),而会执行切换(somestring) 有没有比显式编写每个大小写更简单的方法?您可以创建一个dictionary对象,然后根据键从dictionary中提取值 var letterValues = new Dictionary<string, string>() letterValues["A01"] = "1" letterValues["B01"] = "

我有一些vba代码需要转换成c#

我知道如何在这个问题上进行
切换
,但有更简单的方法吗

我不会检查
单元格(rownum………)
,而会执行
切换(somestring)


有没有比显式编写每个大小写更简单的方法?

您可以创建一个dictionary对象,然后根据键从dictionary中提取值

var letterValues = new Dictionary<string, string>()
letterValues["A01"] = "1"
letterValues["B01"] = "2"
letterValues["C01"] = "3"
....

请记住,这只是伪代码,尚未经过测试。这只是为了展示的目的。希望您能理解。

您可以创建一个dictionary对象,然后根据键从dictionary中提取值

var letterValues = new Dictionary<string, string>()
letterValues["A01"] = "1"
letterValues["B01"] = "2"
letterValues["C01"] = "3"
....

请记住,这只是伪代码,尚未经过测试。这只是为了展示的目的。希望您能理解。

这两个片段是等效的。这回答了你的问题吗

string letter0 = "h01";
string result =
    letter0 == "h05" ? "x"
    : letter0 == "a03" ? "y"
    : letter0 == "fff" ? "z"
    : "6";

Cells[rownum, 2].Value = result;
这相当于:

switch (letter0)
{
    case "h05": result = "x"; break;
    case "a03": result = "y"; break;
    case "fff": result = "z"; break;
    default: result = "6"; break;
}

Cells[rownum, 2].Value = result;

如果不想使用switch语句,可以使用前者。不过,这个开关更干净、更高效,所以我肯定会使用它。

这两个代码片段是等效的。这回答了你的问题吗

string letter0 = "h01";
string result =
    letter0 == "h05" ? "x"
    : letter0 == "a03" ? "y"
    : letter0 == "fff" ? "z"
    : "6";

Cells[rownum, 2].Value = result;
这相当于:

switch (letter0)
{
    case "h05": result = "x"; break;
    case "a03": result = "y"; break;
    case "fff": result = "z"; break;
    default: result = "6"; break;
}

Cells[rownum, 2].Value = result;

如果不想使用switch语句,可以使用前者。不过,这个开关更干净、更高效,所以我肯定会使用它。

您可以通过这种方式更改逻辑

声明静态变量:

private static Dictionary<string, int> _values = new Dictionary<string, int>();
这样,所有值都将被缓存

然后,当您需要根据字母8获取值时,您只需调用:

Cells(rownum, 2).Value = _values[letter0];

你可以这样改变逻辑

声明静态变量:

private static Dictionary<string, int> _values = new Dictionary<string, int>();
这样,所有值都将被缓存

然后,当您需要根据字母8获取值时,您只需调用:

Cells(rownum, 2).Value = _values[letter0];
是的,它是有效的(至少在你的例子中)。当然,假设
letter0
始终采用A99格式-不包括验证

编辑我想算法现在更清晰了

是的,它是有效的(至少在你的例子中)。当然,假设
letter0
始终采用A99格式-不包括验证


编辑我想算法现在更清晰了一点…

等待您的更新,据我所知,这将是一个更好的选择。将单元格标签放在字典中,而不是仅仅为了设置一个值而包含10个以上的案例。大概是这样的:

// set the dimensions of the labels to generate
var rowCount = 10;  // set appropriate row count here
var colCount = 8;

// use LINQ to generate the cell labels.
// make it static if dimensions are large
// or would otherwise generate this once
var cellLabels = (from r in Enumerable.Range(1, rowCount)
                  from c in Enumerable.Range('A', colCount)
                                      .Select(Convert.ToChar)
                  // create the labels
                  select String.Format("{0}{1:d02}", c, r))
                  // convert to a dictionary
                 .Select((Key, i) => new { Key, Value = i + 1 })
                 .ToDictionary(p => p.Key, p => p.Value.ToString());

string letter0 = ...;

                  // assuming letter0 will be in the
                  // range of the generated cell labels
var stringToSet = cellLabels[letter0];

等待你的更新,从我可以告诉,这将是一个更好的选择。将单元格标签放在字典中,而不是仅仅为了设置一个值而包含10个以上的案例。大概是这样的:

// set the dimensions of the labels to generate
var rowCount = 10;  // set appropriate row count here
var colCount = 8;

// use LINQ to generate the cell labels.
// make it static if dimensions are large
// or would otherwise generate this once
var cellLabels = (from r in Enumerable.Range(1, rowCount)
                  from c in Enumerable.Range('A', colCount)
                                      .Select(Convert.ToChar)
                  // create the labels
                  select String.Format("{0}{1:d02}", c, r))
                  // convert to a dictionary
                 .Select((Key, i) => new { Key, Value = i + 1 })
                 .ToDictionary(p => p.Key, p => p.Value.ToString());

string letter0 = ...;

                  // assuming letter0 will be in the
                  // range of the generated cell labels
var stringToSet = cellLabels[letter0];

如果模式是可重复的,您可以这样做,尽管我不会在生产代码中使用它。我会使用spinon发布的字典解决方案

  // Incase of case statement
  Cells(rownum, 2).Value = GetValue(letter0);

  public String GetValue(String letter0)
  {
     String result = String.Empty;
     if (letter0.Length >= 3)
     {
        int row_letter_as_int = (int)letter0[0];
        int row_number = int.Parse(letter0.Substring(1));
        result = ((row_letter_as_int - 64) + (7 * (row_number - 1))).ToString();
     }
     return result;
  }

这与Rsena发布的内容类似。

如果模式是可重复的,您可以这样做,尽管我不会在生产代码中使用它。我会使用spinon发布的字典解决方案

  // Incase of case statement
  Cells(rownum, 2).Value = GetValue(letter0);

  public String GetValue(String letter0)
  {
     String result = String.Empty;
     if (letter0.Length >= 3)
     {
        int row_letter_as_int = (int)letter0[0];
        int row_number = int.Parse(letter0.Substring(1));
        result = ((row_letter_as_int - 64) + (7 * (row_number - 1))).ToString();
     }
     return result;
  }

这与Rsena发布的内容类似。

您能再多展示一点您的代码吗。如果switch/case语句有一大堆不同的情况,那么如果您正在设置的值在每种情况下都递增,那么可能有更好的方法来实现这一点。可能会显示
letter0
rownum
的类型,以及语句中的最后几个案例。您可以再显示一点您的代码吗。如果switch/case语句有一大堆不同的情况,那么如果您正在设置的值在每种情况下都递增,那么可能有更好的方法来实现这一点。也许可以展示一下
letter0
rownum
的类型,以及语句中的最后几个案例。@herrow:嗯,我只是觉得太“黑”了。但是让我试着让它变得更好,等一下…@herrow:嗯,我只是觉得它太“黑”了。但让我试着让它变得更好,等一下。。。