Search code examples
propertiesborland-c++

Why is happened property write error?


c++ : Run time error is happened with error message like this:

RevStrings1->Height of reading included in error: The property is write-protected.
RevStrings1->Height の読み込中のエラー : プロパティは書き込み禁止です.

I'm using c++ builder 3.

This source code can be successfully compiled setting library, include path and etc.

But run time error is happened.


I guess that this problem is about property read & write.

How can I simplly fix the problem ?

A variable RevStrings1 is created by a class TRevStrings.

//---------------------------------------------------------------------------
#ifndef RevStringsH
#define RevStringsH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <Grids.hpp>
//---------------------------------------------------------------------------
class PACKAGE TRevStrings : public TStringGrid
{
private:
//  void __fastcall SetWidth(int W);
//  int __fastcall GetWidth(void);
//  int FColCount ;
  int FRowCount;
  int FFixedCols ;
  int FFixedRows ;
  int FDefaultColWidth ;
  int FDefaultRowHeight ;
  int FHeight;
//  int FWidth;
  int FScrollBars;
  int FMaxLength;
  bool ColColors[24];

protected:
public:
    __fastcall TRevStrings(TComponent* Owner);
  void __fastcall DrawCellText(TRect ARect,int ALeft,String S);
  virtual void __fastcall DrawCell(int ACol, int ARow,const Windows::TRect &ARect, TGridDrawState AState);
  void __fastcall SetColor_Col(int Col,int Row);
  void __fastcall SetColorFlag(int Col,bool flag);
  bool __fastcall GetColorFlag(int Col);
  void __fastcall SetEditText(int ACol, int ARow,const System::AnsiString Value);
  void __fastcall Clear(bool ALLorONE,int Position);
  void __fastcall DblClick(void);
__published:
//  __property int ColCount = {read = FColCount};//FColCount};
  __property int RowCount = {read=FRowCount};
  __property int FixedCols = {read=FFixedCols};
  __property int FixedRows = {read=FFixedRows};
  __property int DefaultColWidth = {read=FDefaultColWidth};
  __property int DefaultRowHeight = {read=FDefaultRowHeight};
  __property int Height = {read=FHeight};
//  __property int Width = {read=GetWidth,write=SetWidth};
  __property int ScrollBars = {read=FScrollBars};
  __property int MaxLength = {read=FMaxLength,write=FMaxLength};
/*
*/
};
//---------------------------------------------------------------------------
#endif

Solution

  • never heard of TRevStrings before

    so it is either BCB 3 discontinued stuff (my BDS2006 does not have it at disposal) or you have some 3th party custom package installed but the header file suggest it is based on TStringGrid so if below text does not work for it then you can switch to TStringGrid instead.

    in TStringGrid

    size properties are accessible normaly:

    StringGrid1->Height=256;
    StringGrid1->Width=128;
    

    if you want to have size-able col/rows then do not forget to open Options property and set goRowSizing,goColSizing to true and starting sizes are DefaultColWidth,DefaultRowHeight. Here example of usage

    // resize the grid
    StringGrid1->Height=128;
    StringGrid1->Width=256;
    // access to Cell AnsiStings
    StringGrid1->Cells[0][0]="(0,0)";
    StringGrid1->Cells[1][1]="(1,1)";
    StringGrid1->Cells[1][2]="(1,2)";
    StringGrid1->Cells[2][1]="(2,1)";
    // resizing row/col
    StringGrid1->RowHeights[0]=15;
    StringGrid1->RowHeights[1]=20;
    StringGrid1->ColWidths[0]=20;
    StringGrid1->ColWidths[1]=15;
    

    As your class is derived from this so this should work also for it if not the there are more possibilities:

    1. you have unrelated bug somewhere

      overwriting what you should not damaging the C++ engine your App is running on or have memory leak somewhere or your memory manager is invalidated see

      but that is probably not the case or you are calling VCL/Winapi visual stuff from threads.

    To check for all this:

    create empty application, add your TRevString and try to set its height on runtime. If no error occurs you have a bug somewhere if error occurs then:

    1. this component is not able to resize on runtime this way

      try to use functions like SetSize,SetBounds instead or place the component on some panel align to Client and resize panel

    2. if even this does not help switch to standard TStringGrid

      you can also try to cast you RevString to StringGrid first

      ((TStringGrid*)(RevString1))->Height=25;
      
    3. Borland compilers sometimes get weird

      few times (around 10) over the years I use BCB/BDS the compiler sometimes compile wrongly. The app is running but some code gets distorted or discarted so what helps?

      • close IDE or even restart Windows
      • delete all map,obj,tds temp files prior to compiling rebuilding
      • sometimes is needed that you add empty line of code or swap 2 lines of code
    4. Identifiers/Names collisions

      if you name your stuff in similar way to VCL functions then you ask for problems usual error is to name function Draw() ... (use draw() instead and you are fine)

    5. for big projects

      if you add your source code as new unit to project instead of just include it (it is present in Object Manager) then in big projects you will got big problems. It looks like units are compiled differently then normal included files in units are expected Formulars and other VCL stuff components so if you got your own non visual classes as units they sometimes stop working as expected creating weird behavior (even your error could be caused by it).

      I observe this on BCB5 and BDS2006. In BCB3,BCB4 I did not make big enough projects to spot this and BCB6 is so buggy so its unusable with big projects anyway. By big projects I mean > 1 MB of pure C++ code