0

This is the declaration for save_g

public static IsolatedStorageSettings save_g = IsolatedStorageSettings.ApplicationSettings;

here cons.term[7] is of type string

save_g[cons.term[7]] = (double)save_g[cons.term[7]] + 1;

The statement above executes with no problem on the emulator. But when I run it on device (Lumia 820) it gives error.

A first chance exception of type 'System.InvalidCastException' occurred in PhoneApp2.DLL

An exception of type 'System.InvalidCastException' occurred in PhoneApp2.DLL but was not handled in user code

And I have no idea whats wrong.

Plz help.

aclap
  • 404
  • 5
  • 13

1 Answers1

2

The invalid cast exception means that save_g[cons.term[7]] isn't a double. The value is most likely null. You should check the part of the code that assigns a value to save_g[cons.term[7]] for the first time.

If it's the only place where you assign this value, you should add code to handle this case:

double value = save_g[cons.term[7]] == null ? 0 : save_g[cons.term[7]];
save_g[cons.term[7]] = value + 1;
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • For matter of conciseness `double value = save_g[cons.term[7]] == null ? 0 : save_g[cons.term[7]];` can be replaced with `double value = save_g[cons.term[7]] ?? 0;`. Read more about `??` operator [here](http://stackoverflow.com/a/446839/2365197) – Shishir Gupta Jul 25 '14 at 19:33