Implicit string cast with potential data loss from 'string' to 'AnsiString' / ADOQuery - string

got here Delphi 10.3 Update 1. On the Form I have a ADOQuery which has a Field named ExtraText this field is TWideStringField .
In my Programm I assign it like this :
PrintPosQueryRack.Value:=PrintPosQueryExtraText.Value;
if I hover the cursor over PrintPosQueryRack.Value I get System.WideString
if I hover the cursor over PrintPosQueryExtraText.Value I get System.String
I really-really don't understand why . The PrintPosQueryRack is a Calculated Field , which I Created as plain string . Because I as far as I know in later Delphi versions string is Unicode (UnicodeString) in Delphi .
I also have a variable here strRack : string . If I assign it to PrintPosQueryRack.Value ( which is System.WideString ) , I get the same Warrning .
I can "fix" this by changing the strRack : string to strRack : AnsiString
and by changing the PrintPosQueryExtraText.Value to PrintPosQueryExtraText.AnsiString .
But I am kinda lost here .
Thank you .

TL;DR: Use WideString as the type for your calculated field. StringFields are internally based on AnsiString.
If you make a field of type String, (ftString), you get a TStringField. Its value is still the "old" AnsiString. This is probably for compatibility reasons.
That is, it depends on the NEXTGEN define, which basically means the for classic desktop applications TStringField.Value is still an AnsiString, while for iOS and Android apps written in Delphi, it is indeed a (unicode) String.
But that is only for the Value property. You can also use the explicit AsString, AsWideString or AsAnsiString properties. Those properties are available for any field type, but the value you give or get is translated to and from the internal type of the field. For TStringFields, that type is still AnsiString, regardless how you set the value.
For unicode values, use WideString or WideMemo fields.

Related

Converting a List of String to List of Double without losing information (VB.net)

I got a List of String. I am losing information (the dot) when I try to convert an entry to type Double. What am I doing wrong?
Dim list As New List(Of String)
Dim a As Double
list.Add("309.69686")
a = CDbl(list(0))
MsgBox(a)
'Output: 30969686
This happens because in your locale the separator for decimal numbers is probably not a point but something else (usually a comma).
You are using the old VB6 methods to convert this string to a double and this method (CDbl) has no way to use a different locale settings.
So in the most basic form you need to change that method to the native .NET methods
a = Double.Parse(list(0), CultureInfo.InvariantCulture)
Here we pass the information about what locale setting Parse should use in converting the input string to a double. And the InvariantCulture uses the point as separator.
Of course, you should consider that, if the input string is obtained from the user input, then you could face other problems (like invalid numeric strings). In this case you should not use double.Parse, but double.TryParse
If you have a German Windows, then the dot will be interpreted as thousands separator. You must specify the culture explicitly, if you need another behaviour.
Dim d = Double.Parse("309.69686", CultureInfo.InvariantCulture)

Trying to add items into a ComboBox via MFC

I am trying to add items to a ComboBox so the user has a choice of what constant to run a calculation with but I cant seem to add items to a ComboBox without an error.
CComboBox *m_YM = (CComboBox *)GetDlgItem(IDC_COMBO1);
I have tried:
m_YM->AddString("Wood");
m_YM->Items->Add("Wood");
m_YM.InsertString(0, "Wood");
All throw errors. Compiler tells me that:
The argument type is incompatible with LPCTSTR.
No idea what is the meaning.
The important thing is the middle T of LPCTSTR, which means it will automatically decide if your string is Unicode or plain old ASCII, but the string needs to be input properly.
Recommended reading: What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
Try to put an L before or enclosing inside _T(...). Example: L"Wood" or _T("Wood")

How to get fields of a Julia object

Given a Julia object of composite type, how can one determine its fields?
I know one solution if you're working in the REPL: First you figure out the type of the object via a call to typeof, then enter help mode (?), and then look up the type. Is there a more programmatic way to achieve the same thing?
For v0.7+
Use fieldnames(x), where x is a DataType. For example, use fieldnames(Date), instead of fieldnames(today()), or else use fieldnames(typeof(today())).
This returns Vector{Symbol} listing the field names in order.
If a field name is myfield, then to retrieve the values in that field use either getfield(x, :myfield), or the shortcut syntax x.myfield.
Another useful and related function to play around with is dump(x).
Before v0.7
Use fieldnames(x), where x is either an instance of the composite type you are interested in, or else a DataType. That is, fieldnames(today()) and fieldnames(Date) are equally valid and have the same output.
suppose the object is obj,
you can get all the information of its fields with following code snippet:
T = typeof(obj)
for (name, typ) in zip(fieldnames(T), T.types)
println("type of the fieldname $name is $typ")
end
Here, fieldnames(T) returns the vector of field names and T.types returns the corresponding vector of type of the fields.

Split string in WIX Bootstrapper

I have a property named "Version" in my bootstrapper project. It contains the version as a dot separated value (1.2.3). I only need to get the last numeric value.
How to perform a string operation within this WIX bootstrapper ? Is it possible or are there any other alternatives ?
You can use Expressions.
https://www.firegiant.com/wix/tutorial/com-expression-syntax-miscellanea/expression-syntax/
For your specific condition, ENDS WITH Expression should work ( >> )

How do I pass a String into a function in an NVelocity Template?

I'm using the NVelocity Templating engine to produce a fixed-length field output - you know the kind of thing:
Field Start Pos Field Length Notes
---------- --------- ------------ ---------
Supplier 1 7 Leading Zeros
GRN 8 9 -
...
e.g.
>0001234 123A<
The problem is I'm trying to call String.PadRight() with the overload to specify the leading zero, and NVelocity is having none of it..
This works:
$Document.SupplierCode.PadRight(7)
But this doesn't:
$Document.SupplierCode.PadRight(7,"0")
I've tried:
Single Quotes ('0')
Double Single-Quotes (''0'')
Double Quotes ("0")
Double Double-Quotes (""0"")
Escaping the quotes for all of the above (\"0\")
No Quotes!
All I've found to work from is the NVelocity Homepage, and the Velocity Templating Language Reference page, niether are pointing me at a solution.
Sorry I'm unable to supply or point you somewhere where you can test out your ideas for yourself, but any suggestions you may have will be most welcome!
Thanks for your help ;o)
I'm coping with the same problem at the moment, as far as I understand it is due to the fact that PadLeft and PadRight functions of String class receive the second parameter, the leading "0", as a char, not as a string.
NVelocity allows you to specify the parameter as a string using '0', but in this way internally it generate a cast exception (or something similar), because the parameter is expected as char.
I haven't found yet (I'm just using NVelocity since 1 hour!) a way to specify the parameter as char, at the moment I have just a dirty solution such as applying a Replace(" ", "0") after the PadLeft / PadRight, so the template becomes
$Document.SupplierCode.PadRight(7).Replace(' ', '0')
One solution that a colleague has come up with is to create another property in the Document object that returns the formatted String:
E.g.
Public ReadOnly Property SupplierCodeFormatted() As String
Get
Return Supplier.Code.PadLeft(7, "0")
End Get
End Property

Resources