Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  4 308

•  TDictionary Custom Sort  6 355

•  Fast Watermark Sources  6 142

•  3D Designer  9 081

•  Sik Screen Capture  6 467

•  Patch Maker  6 882

•  Айболит (remote control)  6 863

•  ListBox Drag & Drop  5 739

•  Доска для игры Реверси  96 635

•  Графические эффекты  7 083

•  Рисование по маске  6 386

•  Перетаскивание изображений  5 255

•  Canvas Drawing  5 627

•  Рисование Луны  5 327

•  Поворот изображения  4 877

•  Рисование стержней  3 460

•  Paint on Shape  2 720

•  Генератор кроссвордов  3 596

•  Головоломка Paletto  2 889

•  Теорема Монжа об окружностях  3 696

•  Пазл Numbrix  2 430

•  Заборы и коммивояжеры  3 111

•  Игра HIP  2 071

•  Игра Go (Го)  1 999

•  Симулятор лифта  2 357

•  Программа укладки плитки  2 059

•  Генератор лабиринта  2 516

•  Проверка числового ввода  2 203

•  HEX View  2 532

•  Физический маятник  2 146

 
скрыть

  Форум  

Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Как присвоить все значения полей одного класса, другому такому же классу



Автор: Serge Gubenko

How can I assign all property values (or if it's not possible only published property values, or some of them) of one class (TComponent) to another instance of the same class? What I want to do is:

MyComponent1. {property1} := MyComponent2. {property1};
{...}
MyComponent2. {propertyN} := MyComponent2. {propertyN};

Is there a better and shorter way to do this? I tried this: MyComponent1 := MyComponent2; But it doesn't work. Why not? Can I point to the second component ?

MyComponent2 and MyComponent1 are pointers to your components, and this kind of assigment leads to MyComponent1 pointing to MyComponent2. But it will not copy its property values.

A better way is to override the assign method of your control, do all property assignment there and call it when you need to copy component attributes. Here's example:

procedure TMyComponent.Assign(Source: TPersistent);
begin
  if Source is TMyComponent then
  begin
    property1 := TMyComponent(Source).property1;
    { ... }
  end
  else
    inherited Assign(Source);
end;

To assign properties you'll need to set this line in the code:

MyComponent1.Assign(MyComponent2); 







Copyright © 2004-2025 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте