I have a problem with converting a string variable to TObject.
I have a query that returns two columns to me. In the first column I have varchar values that I translate into strings, and in the second column I have int values.
I want to fill a ComboBox in this way with these values:
cbx1-> AddItem (DataSet1->DataSet->Fields->Field[0]->AsString, (TObject *) (int) DataSet1->DataSet->Fields->Field[1];
As I refer to the second value which is int type, I receive some bushes, e.g., xD, etc.
By trying to convert this value to string, eg:
String temp = IntToStr (DataSet1->DataSet->Fields->Field[1]);
cbx1-> AddItem (DataSet1->DataSet->Fields->Field[0]->AsString, (TObject *) temp;
I receive an error message:
cannot cast from 'AnsiString' to 'TObject'
I do not know what further I can do to convert this value.
You cannot cast an AnsiString value to a TObject* pointer. You can only cast an integer value, or a pointer value, to a TObject* pointer. AnsiString is neither of those.
You are not retrieving the int value from the 2nd field correctly anyway. Field[1] is a pointer to an actual TField object in the Fields collection. That pointer is what you are trying to store in your ComboBox, NOT the int value that the TField represents.
You need to call Fields[1]->AsInteger to get the int value of the 2nd field, similar to how you use Fields[0]->AsString to get the string value of the 1st field:
cbx1->AddItem(
DataSet1->DataSet->Fields->Field[0]->AsString,
(TObject*) DataSet1->DataSet->Fields->Field[1]->AsInteger
// in C++, using reinterpret_cast is preferred over C-style casting:
// reinterpret_cast<TObject*>(DataSet1->DataSet->Fields->Field[1]->AsInteger)
);
This is no different than the code in your previous question:
cbx1->AddItem("one",(TObject*)1);
You are now just placing the literals "one" and 1 with runtime variables of equivalent types.
Related
I am reading data from Firestore. I was getting an error that said type 'Timestamp' is not a subtype of type 'String' in type cast, so I changed the createDate line below to use .toString() instead of doing as String. This solved the problem, but why did this work?
Notification.fromJson(Map<dynamic, dynamic>? json): //Transform JSON into Notification
createDate = (json?['createDate']).toString(), //This works
modifiedDate = json?['modifiedDate'] as String; //This gives the error: 'type 'Timestamp' is not a subtype of type 'String' in type cast'
The format of both of these fields is October 5, 2022 at 10:49 PM UTC-5.
Like it is said in error message, you are getting this error, because the value's type, which is coming from json is Timestamp, not String. It is better to make createDate and modifiedDate fields in Timestamp type instead of String, because it provides it's own methods, that make your work easier. Converting to String (and probably, you are parsing this String in somewhere) is redundant.
In Flutter every object class have toString() method, which is return a value with String type
(json?['createDate']).toString();
So if you write that code above, it will return a value with String type and then assign that value to createDate variable. It means you not assign json?['createDate'].
modifiedDate = json?['modifiedDate'] as String;
That line above will error because you assign that json to modifiedDate which is have different type. Cast (as) only work if the object/variable have same hierarchy, like
Child child = Child();
Parent parent = child as Parent();
json?['createDate'] is a Timestamp type object that just contains numbers describing a point in time. As you can see from the API documentation, it has a method called toString that knows how to convert that to formatted date string. Since a Timestamp it is not a subclass of String, Dart does not allow it to be cast to one. Perhaps you want to cast it to Timestamp instead, since that's what it is?
I'm fiddling around with GlobalObjectKey, where my objects are DateTime instances. I noticed that if I create new instances of the same date and time, I get the same instance "ID":
DateTime newInstance = new DateTime.fromMicrosecondsSinceEpoch(datetime.microsecondsSinceEpoch);
My debug prints say that there are two separate keys with the same id:
[GlobalObjectKey DateTime#f056e] // Key assigned to Widget1
[GlobalObjectKey DateTime#f056e] // Key assigned to Widget2
However, even though the keys seem to be duplicates, I don't seem to get any widget/build errors.
Here's a more complete example of what I'm doing:
class DateTimeKey{
GlobalObjectKey key;
DateTimeKey(DateTime datetime){
DateTime newInstance = new DateTime.fromMicrosecondsSinceEpoch(datetime.microsecondsSinceEpoch);
key = new GlobalObjectKey(newInstance);
}
}
...
List<DateTimeKey> _bookingListMonthKeys = [];
List<DateTimeKey> _bookingListDayKeys = [];
DateTimeKey _monthKey = new DateTimeKey(theDate);
_bookingListMonthKeys.add(_monthKey);
DateTimeKey _dayKey = new DateTimeKey(theDate); // theDate here refers to the same DateTime instance as above
_bookingListDayKeys.add(_dayKey);
Even if I loop through both lists and cross reference them like this
_bookingListDayKeys.forEach((dayKey){
_bookingListMonthKeys.forEach((monthKey){
if( identical(dayKey, monthKey) )
print('Identical DateTimeKeys found: $dayKey, $monthKey');
if( identical(dayKey.key, monthKey.key) )
print('Identical GlobalObjectKeys found: ${dayKey.key}, ${monthKey.key}');
});
});
it doesn't show any duplicates, yet the printouts above clearly have the same "id" (#f056e). Can someone explain what's going on here?
So I think I figured out what's going on here.
First, the hash number in the question comes from the hashCode property of the DateTime instance. This is used for comparisons by the == operator, and hence will be the same for two DateTime instances that represent the same moment in time.
Sidenote: The actual string being printed (#f056e) seems to be the last 5 characters of the hexadecimal representation of the hashCode, which is an int.
However, hashCode is not used by GlobalObjectKey. Instead, it uses the method identityhashCode() from dart:core:
Returns the identity hash code of object.
Returns the same value as object.hashCode if object has not overridden Object.hashCode. > Returns the value that Object.hashCode would return on this object, even if hashCode has > been overridden.
This hash code is compatible with identical.
When creating two different DateTime instances from the same moment in time, their hashCode properties will be equal but the result of identityHashCode() will be different. So basically it seems like identityHashCode() is the closest substitute for an actual "memory address" of an object we will get.
DateTime instance1 = DateTime.now();
DateTime instance2 = DateTime.fromMicrosecondsSinceEpoch(instance1.microsecondsSinceEpoch);
print('hashCode: ${instance1.hashCode}, ${instance2.hashCode} identityHashCode: ${identityHashCode(instance1)}, ${identityHashCode(instance2)}');
will result in:
I/flutter (23321): hashCode: 89064814, 89064814 identityHashCode: 383428552, 594747591
Now it's clear to see that the hashCode is equal for both instances, but identityHashCode(DateTime) is different, meaning that they are in fact separate instances but equal when comparing their hashCode property or doing a == operation.
Why printing a GlobalObjectKey would print the hashCode of its value is still beyond me though, that's what threw me off from the start.
I have a jrxml file which has a field of BigDecimal value and when I try to execute the report. I get an error called " Cannot cast from BigDecimal to String in jasper". Im doing the report using ireport 5.6.0, and I get a weird error like this,
1. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getValue())); //$JR_EXPR_ID=18$
<----------------------------------------------------------------------->
2. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getOldValue())); //$JR_EXPR_ID=18$
<-------------------------------------------------------------------------->
3. Cannot cast from BigDecimal to String
value = (java.lang.String)(((java.math.BigDecimal)variable_variable1.getEstimatedValue())); //$JR_EXPR_ID=18$
<-------------------------------------------------------------------------------->
3 errors
I tried to change the expression class. but nothing seems to work. I need the value to get the sum and also as a field.
You have a field e.g $F{ID} with field class=java.math.BigDecimal.As you want to use this field as a string as well as decimal;
You can create two variables variable1 with variable Class=java.math.BigDecimal with Variable Expression as $F{ID}
and create another variable variable2 with variable Class=java.lang.String with Variable Expression as $F{ID}.toPlainString()
When I removed tag "printWhenExpression" for that text field with error, the error did not show again.
I need to match against an encrypted column in the DB. I need to pass the encrypted value for matching as a byte[]. The hashcode of the byte[] is passed instead of the actual value stored in the byte[]. Because the hash code is passed, it does not match the value correctly. Below is my query and the function call in the Mapper.java.
AccBalNotificationBean selectAccBalNotificationBean(#Param("acctIdByteArray") byte[] acctIdByteArray);
SELECT toa.accounts_id from tbl_transactions_other_accounts toa WHERE other_account_number = #{acctIdByteArray}
Thank you for your help.
I assume the datatype of your other_account_number column is of type string (char, varchar etc). Mybatis will use the StringDataTypeHandler by default and call the .toString() method of your byte array. Give MyBatis a hint that you want the content of your array to be used, by specifying the typeHandler.
.. WHERE other_account_number = #{acctIdByteArray, typeHandler=org.apache.ibatis.type.ByteArrayTypeHandler}
I have an TextField with datatype Integer, so I am trying to getFieldValue() and write it to Integer field. So in runtime I have an error here:
TextField<Integer> priceField = new TextField<Integer>();
Integer newPriceFieldValue = priceField.getValue(); //here is an error in runtime
So I cant understand whats the problem - proceField.getValue() should be Integer, why string? Maybe I should another type of Field?
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
at
ru.braginini.client.ProductForm$2.componentSelected(ProductForm.java:64)
at
ru.braginini.client.ProductForm$2.componentSelected(ProductForm.java:1)
If you are expecting only numbers to be used in this field NumberField may be the better choice.
NumberField field = new NumberField();
field.setPropertyEditorType(Integer.class);
It will ensure only numbers are entered, and save you some casting & error handling on the getValue() call.
getValue returns a String!
You want to assign this String to an Integer which causes an CastException (like it would in any Type oriented Programming language.
Try
Integer newPriceFieldValue = Integer.parseInt(priceField.getValue());
Regards,
Stefan