Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Class field lost value! (https://www.delphipraxis.net/148288-class-field-lost-value.html)

WojTec 26. Feb 2010 17:30


Class field lost value!
 
Delphi-Quellcode:
var
  Index: Integer;
  Layer: TCustomFilterLayer;
begin
  Index := lbLayers.ItemIndex;

  if Index <> - 1 then
  begin
    if lbLayers.Items.Objects[Index] is TCustomFilterLayer then
    begin
      Layer := lbLayers.Items.Objects[Index] as TCustomFilterLayer;

      if Layer is TBrightnessAdjustmentLayer then
      begin
        Layer := Layer as TBrightnessAdjustmentLayer;

        with TBrightnessLayerEditor.Create(Layer) do
        begin
          SetAmount(vtInteger, -100, 100, TBrightnessAdjustmentLayer(Layer).Amount); // Here is something wrong :(

          // Here is ok
        end;
      end;
    end;
  end;
end;
So, I create some graphic tool with layers.
There is problem with this procedure: when - TBrightnessAdjustmentLayer(Layer).Amount is always 0 (default from Create()), also if is setup other value... Other parameters from TCustomFilterLayer are ok. Why and how to fix? Layer is display with correct setting.

alzaimar 26. Feb 2010 18:14

Re: Class field lost value!
 
Please describe what the loop is doing... Or should be doing.

WojTec 26. Feb 2010 18:25

Re: Class field lost value!
 
When I click on list item should be open editor for object stored in this item.
Window is opening, but field in stored object don't have values setted early, but default form their class constructor.

In oder word I wanna edit object stored in list items.

alzaimar 27. Feb 2010 06:39

Re: Class field lost value!
 
So why do you create another TBrighnessAdjustmentLayer then?
This should work (I removed the 'create' and got rid of some begin/end and other unimportant stuff):
Delphi-Quellcode:
var
  Index: Integer;
  Layer: TCustomFilterLayer;
begin
  Index := lbLayers.ItemIndex;

  if Index <> - 1 then
    if lbLayers.Items.Objects[Index] is TCustomFilterLayer then begin
      Layer := lbLayers.Items.Objects[Index] as TCustomFilterLayer;
      if Layer is TBrightnessAdjustmentLayer then
          SetAmount(vtInteger, -100, 100, TBrightnessAdjustmentLayer(Layer).Amount);
    end;
end;

WojTec 27. Feb 2010 10:17

Re: Class field lost value!
 
Where I create 2nd object? T*Editor is just descendant of TForm (it's more complicated structure, but base class for any editor is TForm) where I have controls to edit layer ;)

I did that I set values in editor contructor.

But I still don't understand why this value is zero? Before constructor is ok, in constructor is ok, after constructor is not ok... (only this field is bad, fields from previous classes are ok at all).

mkinzler 27. Feb 2010 10:22

Re: Class field lost value!
 
Here you're creating a new instance
Delphi-Quellcode:
with TBrightnessLayerEditor.Create(Layer) do

WojTec 27. Feb 2010 11:15

Re: Class field lost value!
 
Why here is created new obj? In this form constructor is reintroduced and assigning parameter to private filed. So, how to change it to do not make new obj and just work on existing one?

mkinzler 27. Feb 2010 11:21

Re: Class field lost value!
 
In Delphi a constructor can be called als "normal" class method. But then you have to call it on an instance! You call it on a class, so the constructor creates a new instance.
Try
Delphi-Quellcode:
    Layer.Create(<Params>);

WojTec 27. Feb 2010 11:31

Re: Class field lost value!
 
But

Delphi-Quellcode:
with TBrightnessLayerEditor.Create(Layer) do
isn't Layer constructor, TBrightnessLayerEditor is just window.
In this case is also created next Layer instance?

mkinzler 27. Feb 2010 12:01

Re: Class field lost value!
 
Yes
Delphi-Quellcode:
TBrightnessLayerEditor.Create(Layer)
creates a new instance of TBrightnessLayerEditor because TBrightnessLayerEditor is a class and not an object!


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:52 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz