C#转换选择的情况下VBA到C#开关

C#转换选择的情况下VBA到C#开关

问题描述:

我有我需要转换为C#C#转换选择的情况下VBA到C#开关

Select Case letter0 
    Case "A01" 
    Cells(rownum, 2).Value = "1" 
    Case "B01" 
    Cells(rownum, 2).Value = "2" 
    Case "C01" 
    Cells(rownum, 2).Value = "3" 
    Case "D01" 
    Cells(rownum, 2).Value = "4" 
    Case "E01" 
    Cells(rownum, 2).Value = "5" 
    Case "F01" 
    Cells(rownum, 2).Value = "6" 
    Case "G01" 
    Cells(rownum, 2).Value = "7" 
    Case "H01" 
    Cells(rownum, 2).Value = "8" 
    Case "A02" 
    Cells(rownum, 2).Value = "9" 
    Case "B02" 
... 
.. 

我知道如何做这个switch一些VBA代码,但有更简单的方法是什么?

我不会被检查,而不是CELLS(rownum.........)会做switch(somestring)

有更简单的方法来做到这一点不是明确写出每一个情况?

+0

你能展示一点点你的代码吗?如果该switch/case语句有大量不同的情况,那么如果您设置的值在每种情况下都增加,则可能有更好的方法来执行此操作。也许显示'letter0','rownum'以及声明中最后几个例子的类型。 – 2011-01-05 20:33:27

public static string GetCellValue(string letter0) { 
    var line = Convert.ToInt32(letter0.Substring(1, 2)); 
    var column = Convert.ToInt32(letter0[0] - 'A' + 1); 
    var result = (line - 1) * 8 + column; 

    return Convert.ToString(result); 
} 

... 

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

是的,它的工作原理(您例如至少)。当然,假设letter0始终为A99格式 - 不包括验证!

编辑我猜的算法现在是有点更清晰......

+0

为什么我不应该使用它 – 2011-01-05 20:52:03

+0

@herrow:嗯,我只是觉得它太“哈克”了。但让我尝试做得更好,只需一秒... – rsenna 2011-01-05 20:54:01

+0

大的改进。 ;) – 2011-01-06 00:11:20

您可以创建一个字典对象,然后根据该密钥从字典中提取值。

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

然后,只需检查值存在,如果有的话,进行分配:

Cells(rownum, 2).Value = (letterValues.ContainsKey(letter0)) ? letterValues[letter0] : "default value"; 

请记住,这仅仅是伪代码,并没有经过测试。它仅用于显示目的。希望你明白这个主意。

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

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

Cells[rownum, 2].Value = result; 

这相当于:如果你想使用switch语句

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; 

,你可以做前者。但是,交换机更干净,更高效,所以我一定会使用它。

您可以更改逻辑这样

声明静态变量:

private static Dictionary<string, int> _values = new Dictionary<string, int>(); 

,并创建一个你会在启动时只有一次调用静态方法:

public static void InitValues() 
{ 
    _values.Add("A01", 1); 
    _values.Add("B01", 2); 
    _values.Add("C01", 3); 
    _values.Add("D01", 4); 
    _values.Add("E01", 5); 
    _values.Add("F01", 6); 
    _values.Add("G01", 7); 
    _values.Add("H01", 8); 
    ... 
} 

这样,所有值将被缓存。

然后,当你需要根据letter8你可以简单地调用get值:

Cells(rownum, 2).Value = _values[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; 
    } 

这与rsenna发布的内容类似。