Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

inheritance - Creating a TStringGrid class in delphi where the object array associated with the cells is specified as a more specific type

In Delphi, how can I create a class derived from the TStringGrid class such that the TObject array associated with the grids cells is of a more specific type for example TColor which would be used to specify the colour of the cell?

question from:https://stackoverflow.com/questions/66059384/creating-a-tstringgrid-class-in-delphi-where-the-object-array-associated-with-th

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

TStringGrid can hold a TObject for each cell. TColor doesn't inherit from TObject so it doesn't work.

You could cast a TColor to a TObject but this would be a bad solution prone to future issues. And this would not work for any type (Only those having at most the size of a pointer).

The best solution is to "box" your data into a TObject and save the instance of such an object into the StringGrid.

TMyBoxingColorObject = class
    Data : TColor;           // Or any other datatype
end;

Don't forget to create and free the object as needed!

You can also use generics if you have a lot of different types to box.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...