Null or Zero variable

The use of specific JADE features and proposals for new feature suggestions
nzwta
Posts: 13
Joined: Thu Sep 10, 2009 12:46 pm
Location: Napier

Null or Zero variable

Postby nzwta » Thu Sep 01, 2011 10:21 am

We have fields on forms we want to change the display based on whether the user has entered a value into the variable. A valid entry includes a zero value but when we check the variable it is pre-initialised to be zero so we can't determine if the user entered zero on the form or not. It would be useful to be able to initialise the primative variables to blank or null instead of zero.

Anyone got any simple and effective work arounds for this or would this be a good feature to add to Jade?
Thanks
David

torrie
Posts: 92
Joined: Fri Aug 14, 2009 11:24 am

Re: Null or Zero variable

Postby torrie » Thu Sep 01, 2011 11:10 am

Take a look at the firstChange event. We use this to detect if users have updated a control.

If the control is a text box, you can check whether the text property is null.

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Null or Zero variable

Postby murray » Thu Sep 01, 2011 11:16 am

Jade always initialises primitive values to null. This equates to zero for a numeric primitive.
Can you provide more details? Are you using TextBoxes with dataType set to Numeric?
In this case the default behaviour is to display a null string and only allow numeric input.
Perhaps you have some code that is pre-setting the value in the TextBox?
Are you using getTextAsInteger() or getTextAsDecimal()? You could check the text property directly.
Alternatively you could use a JadeEditMask with a mask that allows entry of the desired characters.
Murray (N.Z.)

nzwta
Posts: 13
Joined: Thu Sep 10, 2009 12:46 pm
Location: Napier

Re: Null or Zero variable

Postby nzwta » Fri Sep 02, 2011 8:49 am

Jade always initialises primitive values to null. This equates to zero for a numeric primitive” is actually the exact problem. In languages where primitives such as integer are nullable, zero and null are quite distinct. The problem is not to detect on the form entry has occurred, but how to interpret that value when read later from database. Has it ever been set to zero, or has it always been null? It is impossible to tell because, as stated, zero is effectively the same as null.

This bit of code...

vars
i : Integer;
begin
write (i = 0).String;
write (i = null).String;
end;

.. will produce “true” both times, even though the variable has not explicitly been set to any value. We have situations where 0 is a valid entry but we need to know zero has actually been entered.

davidmellow
Posts: 42
Joined: Thu Aug 27, 2009 1:27 pm
Location: New Plymouth, New Zealand

Re: Null or Zero variable

Postby davidmellow » Fri Sep 02, 2011 10:05 am

Ha ha, just to confuse matters, I think this thread has started from a discussion I was having yesterday with david.

The gist of it is that I sometimes wish there were nullable primitives, whereby NULL is absolutely distinct from zero. As in SQL, or .NET Nullable<T>. So in his example, the problem is not in detecting whether a user has entered a value when they update a form, rather how to determine later whether any value has ever been explicitly set by any process. Probably something we experience a bit more than most, as we work with screeds of scientific data, and often ZERO is a valid result. Though we also need to identify whether ZERO has actually been recorded explicitly (or is it yet to be recorded). As with everything, always workarounds, but in .NET I use nullables a lot and kinda miss them.

Any thoughts? How do others generally handle this?

PS, Murray - What do you get up to in New Plymouth? I also do all my work from there.

Regards
David (#2)

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Null or Zero variable

Postby murray » Fri Sep 02, 2011 10:52 am

OK, I understand. Of course there are solutions/workarounds that can be considered when designing, but they come with their drawbacks.
For example: define a constant with a special value for "null" outside the required range (e.g. Min_Integer) which the constructor uses to intialise new values.

This reminds me of the situation in electronics where there is a significant difference between ZERO volts potential measured in a live circuit, and NO voltage due to absence of power (e.g. an unconnected wire).

David M, I'll send you a PM when I can figure out how to.
Murray (N.Z.)

User avatar
BeeJay
Posts: 311
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Null or Zero variable

Postby BeeJay » Fri Sep 02, 2011 11:15 am

It would be very nice if Jade had differentiated between 0 and NULL for the various number primitives way back when it first started. Unfortunately I suspect that this could not be changed now without serious risk of breaking the many existing Jade systems out there that are relying on the current behaviour etc. Actually, further to this there are times when it's good to know the difference between NULL and Uninitialized, so you can tell if it has never been set versus explicitly set to NULL.

On the rare occasions when I absolutely had to be able to differentiate between 0 and NULL, as well as the option of a "magic' number, like Max_Negative_Integer, I've also used the option of storing the value as a String rather than one of the number primitives. This can cause sorting problems though, if you need entries sorted numerically, and can potentially take more storage space so it's not always a viable option.

Cheers,
BeeJay.

allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: Null or Zero variable

Postby allistar » Fri Sep 02, 2011 11:57 am

I believe the new JADE 7 feature of soft entities allows for the differentiation between "not set yet" and "set to the default value".

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Null or Zero variable

Postby murray » Fri Sep 02, 2011 1:16 pm

I believe the new JADE 7 feature of soft entities allows for the differentiation between "not set yet" and "set to the default value".
If that's what I think it is, Allistar, I don't think it's included in Jade 7.

Another way I have dealt with this is a class that is record which attributes have been set, in the form of a bit map: I use a mapping method on each attribute to set the appropriate bit when the value is 'set'.

Code: Select all

anAttribute(set: Boolean; _value: Integer io) mapping, updating; begin if set then bitMap[32] := '1'; endif; end;
Murray (N.Z.)

davidmellow
Posts: 42
Joined: Thu Aug 27, 2009 1:27 pm
Location: New Plymouth, New Zealand

Re: Null or Zero variable

Postby davidmellow » Sun Sep 04, 2011 10:58 am

Murray - not sure there is a way of PMing on here, email addy is d.mellow@xtra.co.nz


Return to “Feature Discussions”

Who is online

Users browsing this forum: No registered users and 2 guests

cron