Switch case in datatable

datatable
excel
activities

#1

Hi everyone.

I would like to how to use switch case with a table table to perform different operations based on the data in every cell in a column. what expression would meet this requirement?


#2

In a switch, the expression is simply the value you want to test. It can be just a variable or the result of an operator or function call. With a DataTable column, you have to use the type defined for it or convert it to the type you need. For example, if the first column in the table row row contains strings, you can get this string value with row.Field(Of String)(0). If you have to convert it and you can guarantee that a conversion will succeed for all cell values, you can use functions like Convert.ToInt32(row(0)) or Convert.ToDateTime(row(0)) directly on the cell value, because these accept Object arguments. If the conversion may fail, there are other functions like Int32.TryParse, DateTime.TryParse (these only accept string input) or a try/catch activity to handle this, but either way you should then do the conversion before the switch to ensure the validity of the switch expression.