Search code examples
delphisoapdelphi-10.2-tokyo

Converting a String to TXSDecimal


I am trying to covert a string to a SOAP TXSDecimal, this is what I have currently, so the value in my edit box does not get stored inside Foo.Limit

Foo.limit := TXSDecimal(edtLimit.text);

PS : Foo.Limit is a TXSDecimal datatype

I have also tried

Foo.limit.asAsBcd := edtLimit.text; // get Access Violation

So I want to know how do you convert a String to a TXSDecimal


Solution

  • The first snippet is incorrect. You're typecasting a string to TXSDecimal, which is not valid. TXSDecimal is a class, for which you need to have an instance. Once you have an instance, you can use it's properties and methods to set the value.

    The second snippet is apparently incomplete. If you get an access violation, that is likely because Foo.limit is not assigned a proper TXSDecimal.

    So, you can create a new instance, assign it to Foo.limit, and assign it a floating point value like so:

    Foo.limit := TXSDecimal.Create;
    Foo.limit.AsBcd := Edit1.Text;
    

    You can assign a string to a BCD as above, and it will convert it to a numeric value automatically. The assignment will throw an exception (not an access violation, but a different kind) if the string doesn't contain a valid number.

    This conversion will take your system settings into account, so if you're set up to use a comma as a decimal separator, you cannot enter a number that uses a period.