JOE SKEET
JOE SKEET

Reputation: 8118

c# converting select case VBA to c# SWITCH

i have some vba code that i need to convert to 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"
...
..

i understand how to do a switch on this, but is there an easier method?

i will not be checking CELLS(rownum.........) instead of will be doing switch(somestring)

is there an easier way to do this than explicitly write every single case?

Upvotes: 0

Views: 619

Answers (6)

Jeff Mercado
Jeff Mercado

Reputation: 134491

Pending your update, from what I can tell, this would be a much better alternative. Rather than including well over 10 cases just to set a value, place the cell labels in a dictionary. Something like this:

// 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];

Upvotes: 0

rsenna
rsenna

Reputation: 11973

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);         

Yes, it works (for your example at least). Assuming, of course, that letter0 is always in the format A99 - no validation included!

Edit I guess the algorithm is a little bit clearer now...

Upvotes: 2

spinon
spinon

Reputation: 10857

You could create a dictionary object and then pull the value from the dictionary based on the key.

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

Then just check that the value exists and, if so, make the assignment:

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

Keep in mind that this is just pseudo code and has not been tested. It is just for display purposes. Hopefully you get the idea.

Upvotes: 0

SwDevMan81
SwDevMan81

Reputation: 49998

You could do something like this if the pattern is repeatable, although I wouldnt use this in production code. I would use the dictionary solution that spinon posted.

  // 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;
  }

This is similar to what rsenna posted.

Upvotes: 0

HABJAN
HABJAN

Reputation: 9338

You can change logic this way

Declare static variable:

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

And create static method that you will call only once at startup:

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);
    ...
}

This way all values will be cached.

Then when you will need to get value based on letter8 you can simply call:

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

Upvotes: 0

user47589
user47589

Reputation:

These two snippets are equivalent. Does this answer your question?

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

Cells[rownum, 2].Value = result;

That is equivalent to:

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;

If you wanted to not use the switch statement, you could do the former. The switch is cleaner and more efficient, though, so I would definitely use it.

Upvotes: 1

Related Questions