Barry Andrews
Barry Andrews

Reputation: 382

How to access a boolean in 'const TValue & Value' parameter in C++ FireMonkey TStringGrid::OnDrawColumnCell event?

I am trying to check a boolean value from a boolean column in the FMX TStringGrid::OnDrawColumnCell event.

The callback has the following parameters:

void __fastcall TMainForm::SegmentStringGridDrawColumnCell(TObject *Sender, TCanvas * const Canvas, TColumn * const Column,
      const TRectF &Bounds, const int Row, const TValue &Value, const TGridDrawStates State)

If I try and read Value.AsBoolean I get the following error:

[bcc64x Error] main.cpp(235): 'this' argument to member function 'AsBoolean' has type 'const System::Rtti::TValue', but function is not marked const

If I try and declare a non-const TValue and assign it to the const TValue:

    TValue MyValue;
    #ifndef __clang__
      MyValue = TValue::_op_Implicit(Value);
    #else
      MyValue = TValue::From(Value);
    #endif
    bool myVal = MyValue.AsBoolean();

The code compiles and links, but at runtime I get the following exception:

exception class EInvalidCast with message 'Invalid class typecast'

The exception occurs when I try to access MyValue.AsBoolean().

How do I access the value from const TValue &Value?

Upvotes: 0

Views: 58

Answers (1)

Barry Andrews
Barry Andrews

Reputation: 382

I found that I needed to simplify how I copied from the const TValue to a local variable so I could access the contents without getting the error regards Value being const but function not.

This code did not make MyValue = Value, it changed it to a "tkRecord" type.

TValue MyValue;
#ifndef __clang__
  MyValue = TValue::_op_Implicit(Value);
#else
  MyValue = TValue::From(Value);
#endif
bool myVal = MyValue.AsBoolean();

All I actually needed to do was simply copy the value into my own variable. Then I could access the Kind and determine what type the TValue is.

TValue MyValue = Value;
System::Typinfo::TTypeKind Kind = MyValue.Kind;

In my case I found Kind was "tkString", once I knew this, I was able to do a string compare to determine the boolean value "True" and "False".

Upvotes: 1

Related Questions