I am trying to revert variables from a table back to a JSON file. My table has 3 columns (key, value, and updated value).
How can you use the "Type" type to define a type and then cast it to a variable? For example, if I had:
int originalvalue = 4;
var updatedvalue = "5";
Type valueType = originalvalue.GetType();
(valueType)updatedvalue;
This is what I'm trying to do
Because this table required all input to be strings, I saved the variable in its pure form in a dictionary. Now, I am accessing that variable from the dictionary, getting it's type, and casting it to a the specific updated variable from the table.
The error says:
The left-hand side must be a variable, property, or indexer
The table has string variables and has the following columns:
KEY NAME | VALUE NAME | UPDATED VALUE
I tried the following, but it wouldn't work.
// This saves the variable in the pure form (as an int, string, boolean, etc)
Dictionary<string, object> pureFormVariableDictionary = new Dictionary<string, object>();
// Add variables casted as strings to a DataGridView - this works
foreach (DataGridViewRow row in mydatagridview.Rows)
{
var valueCell = row.Cells[2].Value; // See if there were any updated values to update the dictionary
if (valueCell != null && !(string.IsNullOrEmpty(valueCell as string)))
{
var value = valueCell;
Type castingType = pureFormVariableDictionary[row.Cells[0].Value.ToString()].GetType(); // Get the variable type from the pureForm Dictionary
/ This is where the issue arises - how can you define a type to cast?
value.GetType() = castingType; // Cast the table string variable to the pure Form type
pureFormVariableDictionary[row.Cells[0].Value.ToString()] = value; // Update the pure Form Dictionary with the new value - doesn't work
}
}