1

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
    }
}
fibonaccilinguine
  • 101
  • 1
  • 2
  • 11
  • updated with the error message – fibonaccilinguine Jul 01 '20 at 09:30
  • 1
    Where? Here? `value.GetType() = castingType` ? You're trying to store `castingType` to the result of a method call. This doesn't doesn't make sense. This isn't casting, it's the same as trying to write `3.ToString()="Moo"` – Panagiotis Kanavos Jul 01 '20 at 09:33
  • 1
    This "pure form" terminology sounds like something you've made up - you are storing values in a dictionary with a base type of `object`, if these are value types they will be boxed (go read up on boxing). What are you achieving by getting the value from the dictionary and writing it back to a dictionary of `` as itself - all that will happen is you unbox it and re-box it... you are achieving nothing. If you are actually storing strings, then why is the `V` generic parameter `object` and not `string`? – Charleh Jul 01 '20 at 09:37
  • `Because this table required all input to be strings` => It's always string, so it cannot revert back without parsing. Besides that, the column could contain classic type like string, int, DateTime, ... and it would help you cast back easier by [reflection](https://stackoverflow.com/a/38865748/3789481) – Tấn Nguyên Jul 01 '20 at 09:37
  • I see. 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 – fibonaccilinguine Jul 01 '20 at 09:39
  • `Type` contains type information, if you want to convert a `string` to an `int` you would need to **parse** the value - this can be done with `int.Parse` or `int.TryParse` depending on how you want to be able to handle errors. Also, it's worth noting that your dictionary has `key` and `value` but I assume the `key` is the property name in your JSON and the `value` is the string based value. If that's the case, how do you know which type should be parsed from each key/value pair? – Charleh Jul 01 '20 at 09:42
  • You also cannot use the standard cast operator `(SomeType)` with runtime type information - this method of casting is at compile time and needs to be checked. There are other ways to do it, but you don't want to cast anyway, you want to parse. – Charleh Jul 01 '20 at 09:45
  • So I start off with a JSON file, then I parse this into key, value pairs in a dictionary. I traverse this dictionary and add all key,value pairs as strings into my table. Now, I traverse the table and check the 3rd column to see if there were any updates. Because I have this initial dictionary, I know what type I want the values to be casted to. After I traverse the table and get all pairs that have had changes, I update their values in the dictionary and serialize this dictionary back to a JSON file.Is there a better way to convert back to the JSON file? Otherwise all values will be strings – fibonaccilinguine Jul 01 '20 at 09:47
  • Yeah, just serialize/deserialize it - is the JSON a known format (the same structure each time?). Also, why are you saving the values as strings in the table and not their raw types? – Charleh Jul 01 '20 at 09:49
  • The JSON format can be 1 of 6 different types of formats. I am currently serializing it and the issue is that all the values are saved in the JSON as strings (because they are strings in the table - the table cells only accept strings). How can I leverage knowing the JSON format? – fibonaccilinguine Jul 01 '20 at 09:52
  • If you have the type and they are all primitives you could use `Convert.ChangeType(value, castingType)` it will at least give you a boxed `object` of the correct underlying target type (assuming well-formed string data) – pinkfloydx33 Jul 01 '20 at 10:33
  • If you know what format the JSON is going to be in each case, just create 6 objects mapping the JSON structure and serialise/deserialize these when you need to work with them. I'm not sure why you need a table, I assume this is because you are reading values from a data store and that's where your updates come from, but as far as updating some JSON with those values, I'd just deserialize into an object, update the values and serialize back... either way you are going to have to somehow parse the JSON and update the keys/values. – Charleh Jul 01 '20 at 11:41

0 Answers0