A sqlite table declared like this:
CREATE TABLE Note(Id UNIQUEIDENTIFIER, Title TEXT)
is correctly read by Vici CoolStorage on Windows, but on MonoTouch, the following exception is thrown:
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
at Vici.CoolStorage.CSDataProviderSQLite.GetSchemaColumns (System.String tableName) [0x00000] in <filename unknown>:0
at Vici.CoolStorage.CSSchema.CreateColumns () [0x00000] in <filename unknown>:0
at Vici.CoolStorage.CSSchema..ctor (System.Type objType) [0x00000] in <filename unknown>:0
at Vici.CoolStorage.CSSchema.Get (System.Type objectType) [0x00000] in <filename unknown>:0
at Vici.CoolStorage.CSList``1[Store.CoolStorage.Note]..ctor () [0x00000] in <filename unknown>:0
It looks like the code that detects the data type of a column based on the type ID does not handle the UNIQUEIDENTIFIER type in Vici's CSDataProviderSqlite for MonoTouch:
From CSSqliteConnection.GetSchema:
switch (dbType)
{
case "TEXT": dataType = typeof(string); break;
case "VARCHAR": dataType = typeof(string); break;
case "INTEGER": dataType = typeof(int); break;
case "BOOL": dataType = typeof(bool); break;
case "DOUBLE": dataType = typeof(double); break;
case "FLOAT": dataType = typeof(double); break;
case "REAL": dataType = typeof(double); break;
case "CHAR": dataType = typeof(string); break;
case "BLOB": dataType = typeof(byte[]); break;
case "NUMERIC": dataType = typeof(decimal); break;
case "DATETIME": dataType = typeof(DateTime); break;
}
There isn't an handler for UNIQUEIDENTIFIER here. Is this a bug in Vici CoolStorage?
I would just modify Vici, since it looks like you have access to the source code:
switch (dbType)
{
case "TEXT": dataType = typeof(string); break;
case "VARCHAR": dataType = typeof(string); break;
case "INTEGER": dataType = typeof(int); break;
case "BOOL": dataType = typeof(bool); break;
case "DOUBLE": dataType = typeof(double); break;
case "FLOAT": dataType = typeof(double); break;
case "REAL": dataType = typeof(double); break;
case "CHAR": dataType = typeof(string); break;
case "BLOB": dataType = typeof(byte[]); break;
case "NUMERIC": dataType = typeof(decimal); break;
case "DATETIME": dataType = typeof(DateTime); break;
case "UNIQUEIDENTIFIER": dataType = typeof(Guid); break;
}
If there is more required beyond that, you will have to try. Unless there is no try. Use the source, Luke.
Related
switch (numYears) {
case 10:
intsRate = 0.06;
break;
case 15:
intsRate = 0.05;
break;
case 30:
intsRate = 0.04;
break;
default:
intsRate = 0.08;
break;
}
return intsRate;
}
when the input of numYears is 10, 15, or 30 its returns the according double, but its only returning the defult. its part of a larger code but everything in that code is right this is the only part returning the wrong thing.
I am developing an automation testing code with the following error:
Type mismatch: cannot convert from CellType to int.
Please what can I do?
public static String cellToString(HSSFCell cell) {
// TODO Auto-generated method stub
int type;
Object result;
type = cell.getCellType();
switch (type) {
case 0 : // numeric value in Excel
result = cell.getNumericCellValue();
break;
case 1 : //String value in Excel
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("No support for this of cell");
}
return result.toString();
}
CellType is an enum, not an int. The type of your type variable should be CellType and your switch should look like this:
CellType type = cell.getCellType();
switch (type) {
case CellType.NUMERIC : // numeric value in Excel
result = cell.getNumericCellValue();
break;
case CellType.STRING : //String value in Excel
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("No support for this of cell");
}
Alternatively, you can use Enum#ordinal(), which returns an ordinal integer of the enum value, but the example above is much preferable.
EDIT: Also have a look at this answer about how to get cell value as string using Formatter instead of switch.
So lets say I have this enum
public enum StatType
{
Strength, Dexterity, Intelligence, Wisdom, Luck
}
and I have this function,
public void SetStats(StatType type, int value)
{
switch (type)
{
case StatType.Strength:
hero.Stats.Strength += value;
break;
case StatType.Dexterity:
hero.Stats.Dexterity += value;
break;
case StatType.Intelligence:
hero.Stats.Intelligence += value;
break;
case StatType.Wisdom:
hero.Stats.Wisdom += value;
break;
case StatType.Luck:
hero.Stats.Luck += value;
break;
}
}
How can I change this incoming stat's "type" parameter to hero.Stats.( ) variable's name?
If this is possible, then I don't need to use switch, right?
I want to link my combobox selected item with switch case which result in different result depend on option chosen. But return in this error :Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to java.awt.event.ItemListener. Can only one help me, thanks you so much!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1.addItemListener((ItemListener) jComboBox1);
int usertype = (int) jComboBox1.getSelectedIndex();
switch (usertype) {
case '1':
new ResidentView().setVisible(true);
this.dispose();
break;
case '2':
new OfficeClerkView().setVisible(true);
this.dispose();
break;
case '3':
new OfficeManagerView().setVisible(true);
this.dispose();
break;
}
Debugging bit of code for bluetooth connection receive string etc. However, seem to be getting a ClassCastException at the line of code
String read_Message = (String) msg.obj;
But if I was to use my previous bit of code and collect the bytes and place into a string runs but doesn't collect all the data in one string.
Have I not cast something or missed something cause I know I'm missing something but can't see it.
If any more code is required I will place up, everything else is working
Thanks for any help
// The Handler that gets information back from the BluetoothService
private final Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MESSAGE_STATE_CHANGE:
// if (D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1)
{
case BluetoothService.STATE_CONNECTED:
break;
case BluetoothService.STATE_CONNECTING:
//mTitle.setText(R.string.title_connecting);
break;
case BluetoothService.STATE_LISTEN:
case BluetoothService.STATE_NONE:
//mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
//code to be entered here
break;
case MESSAGE_READ:
//Previous code
//byte[] read_Buf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
//String read_Message = new String(read_Buf, 0, msg.arg1);
String read_Message = (String) msg.obj;
if (mSmokeReadingArrayAdapter.isEmpty())
mSmokeReadingArrayAdapter.add("");
mAdapter_Text.set(0, mAdapter_Text.get(0).toString() + read_Message);
mSmokeReadingArrayAdapter.notifyDataSetChanged();
organiseString(read_Message);
break;
case MESSAGE_DEVICE_NAME:
//code to be entered here
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show(); // shows 'alert' messages
break;
}
}
};
Problem solved. It wa to do with the bluetooth service code that had been defined as a byte array rather than string