Anyway to know if an integer variable has been assigned

Hi

I have an integer variable, but 0 is a normal value, but still I need to know if that variable was assigned or if it is still undefined...
anyway to do that?

ty

Comments

  • : Hi
    :
    : I have an integer variable, but 0 is a normal value, but still I
    : need to know if that variable was assigned or if it is still
    : undefined...
    : anyway to do that?
    :
    : ty

    You cannot see that with an integer variable. The variable itself is simply a memory space, which holds a value (wether explicitly given a value or having a default value).
    There are ways around it:
    Pointers are normally implicitly set to null. If a pointer has any other value, it has been assigned a value. Since a pointer can point to any location in the memory it can also point to an integer.
    YOu can also use objects for this. Here is a simple integer object, which knows if it has been given a value or not:
    [code]
    type
    TStatefulInteger = class(TObject)
    private
    FValue: integer;
    FHasBeenAssigned: boolean;
    procedure SetValue(const AValue: integer);
    public
    property Value: integer read FValue write SetValue;
    property HasBeenAssigned: boolean read FHasBeenAssigned;
    constructor Create(); overload;
    constructor Create(AValue: integer); overload;
    end;

    constructor TStatefulInteger.Create();
    begin
    Create(0);
    end;

    constructor TStatefulInteger.Create(AValue: integer);
    begin
    FValue := AValue;
    FHasBeenAssigned := false;
    end;

    procedure TStatefulInteger.SetValue(const AValue: integer);
    begin
    FValue := AValue;
    FHasBeenAssigned := true;
    end;
    [/code]

  • : You cannot see that with an integer variable. The variable itself is
    : simply a memory space, which holds a value (wether explicitly given
    : a value or having a default value).
    : There are ways around it:
    : Pointers are normally implicitly set to null. If a pointer has any
    : other value, it has been assigned a value. Since a pointer can point
    : to any location in the memory it can also point to an integer.
    : YOu can also use objects for this. Here is a simple integer object,
    : which knows if it has been given a value or not:
    : [code]:
    : type
    : TStatefulInteger = class(TObject)
    : private
    : FValue: integer;
    : FHasBeenAssigned: boolean;
    : procedure SetValue(const AValue: integer);
    : public
    : property Value: integer read FValue write SetValue;
    : property HasBeenAssigned: boolean read FHasBeenAssigned;
    : constructor Create(); overload;
    : constructor Create(AValue: integer); overload;
    : end;
    :
    : constructor TStatefulInteger.Create();
    : begin
    : Create(0);
    : end;
    :
    : constructor TStatefulInteger.Create(AValue: integer);
    : begin
    : FValue := AValue;
    : FHasBeenAssigned := false;
    : end;
    :
    : procedure TStatefulInteger.SetValue(const AValue: integer);
    : begin
    : FValue := AValue;
    : FHasBeenAssigned := true;
    : end;
    : [/code]:

    Thank you!
    xD
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories