How in Delphi to check that the Integer variable was previously assigned any value?
- perhaps if myVar! = null, or write a question in more detail - HELO WORD
- In Integer, before assignment is not null, but some random value. - Dima Falcons
- If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
2 answers
Really - no way. Integer does not store anywhere any additional information, except for those 4 bytes that are allocated to it.
Of course, you can assign a variable to a "magic" value that is never found in your data (for example, -1 , or MaxInt , or 777777 ). And in a simple application, I would have done so. But one day this value can meet you and the program will fail.
You can also use another ready-made data type, such as Variant .
A better option is to store the assignment information in another variable, for example, IsIntegerAssigned: Boolean; and maintain its condition at each operation. But it is more cumbersome and inconvenient.
Modern version - you can put both variables in the record and redefine the assignment, like this:
TMyInteger = record private fValue: Integer; fIsChanged: Boolean; public // Присвоение Integer class operator Implicit(const A: Integer): TMyInteger; // Чтение значения. Не помню, можно ли так же через неявную конвертацию или нет property Value: Integer read fValue; property IsChanged: Boolean read fIsChanged; // Тут еще надо переопределить конструктор New, который будет заполнять поля нулями // А можно сделать fIsChanged: string; тогда поле будет пустым при первом обращении) end; .... class operator TMyInteger.Implicit(const A: Integer): TMyInteger; begin Result.fValue := A; Result.fIsChanged := True; end; I := TMyInteger.New; I := 45; Assert(I.IsChanged, 'I is changed'); And of course, the uber option is to put in the classroom
Try creating a second variable then compare something like this.
myInt: Integer; myVar: Integer; if myInt==myVar then begin // он не изменился else // он изменился сохраняем новое значение myVar := myInt; end;